-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.go
174 lines (137 loc) · 4.01 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package main
import (
"context"
"crypto/tls"
"flag"
"fmt"
"log"
"net/http"
"os"
"strconv"
"sync"
"time"
"cloud.google.com/go/storage"
"github.com/googleapis/gax-go/v2"
"golang.org/x/oauth2"
"google.golang.org/api/option"
// Install google-c2p resolver, which is required for direct path.
_ "google.golang.org/grpc/xds/googledirectpath"
)
var (
GrpcConnPoolSize = 1
MaxConnsPerHost = 100
MaxIdelConnsPerHost = 100
NumOfWorker = 48
NumOfReadCallPerWorker = 800
MaxRetryDuration = 30 * time.Second
RetryMultiplier = 2.0
BucketName = "golang-grpc-test-princer-gcsfuse-us-central"
ProjectName = "gcs-fuse-test"
// ObjectNamePrefix<worker_id>ObjectNameSuffix is the object name format.
// Here, worker id goes from <0 to NumberOfWorker.
ObjectNamePrefix = "50mb/1_thread."
ObjectNameSuffix = ".0"
wg sync.WaitGroup
)
func CreateHttpClient(ctx context.Context, isHttp2 bool) (client *storage.Client, err error) {
var transport *http.Transport
// Using http1 makes the client more performant.
if isHttp2 == false {
transport = &http.Transport{
MaxConnsPerHost: MaxConnsPerHost,
MaxIdleConnsPerHost: MaxIdelConnsPerHost,
// This disables HTTP/2 in transport.
TLSNextProto: make(
map[string]func(string, *tls.Conn) http.RoundTripper,
),
}
} else {
// For http2, change in MaxConnsPerHost doesn't affect the performance.
transport = &http.Transport{
DisableKeepAlives: true,
MaxConnsPerHost: MaxConnsPerHost,
ForceAttemptHTTP2: true,
}
}
tokenSource, err := GetTokenSource(ctx, "")
if err != nil {
return nil, fmt.Errorf("while generating tokenSource, %v", err)
}
// Custom http client for Go Client.
httpClient := &http.Client{
Transport: &oauth2.Transport{
Base: transport,
Source: tokenSource,
},
Timeout: 0,
}
// Setting UserAgent through RoundTripper middleware
httpClient.Transport = &userAgentRoundTripper{
wrapped: httpClient.Transport,
UserAgent: "prince",
}
return storage.NewClient(ctx, option.WithHTTPClient(httpClient))
}
func CreateGrpcClient(ctx context.Context) (client *storage.Client, err error) {
if err := os.Setenv("STORAGE_USE_GRPC", "gRPC"); err != nil {
log.Fatalf("error setting grpc env var: %v", err)
}
if err := os.Setenv("GOOGLE_CLOUD_ENABLE_DIRECT_PATH_XDS", "true"); err != nil {
log.Fatalf("error setting direct path env var: %v", err)
}
client, err = storage.NewClient(ctx, option.WithGRPCConnectionPool(GrpcConnPoolSize))
if err := os.Unsetenv("STORAGE_USE_GRPC"); err != nil {
log.Fatalf("error while unsetting grpc env var: %v", err)
}
if err := os.Unsetenv("GOOGLE_CLOUD_ENABLE_DIRECT_PATH_XDS"); err != nil {
log.Fatalf("error while unsetting direct path env var: %v", err)
}
return
}
func ReadObject(ctx context.Context, workerId int, bucketHandle *storage.BucketHandle) (err error) {
defer wg.Done()
objectName := ObjectNamePrefix + strconv.Itoa(workerId) + ObjectNameSuffix
for i := 0; i < NumOfReadCallPerWorker; i++ {
start := time.Now()
object := bucketHandle.Object(objectName)
rc, err := object.NewReader(ctx)
if err != nil {
return fmt.Errorf("while creating reader object: %v", err)
}
duration := time.Since(start)
fmt.Println(duration)
rc.Close()
}
return
}
func main() {
clientProtocol := flag.String("client-protocol", "http", "# of iterations")
flag.Parse()
ctx := context.Background()
var client *storage.Client
var err error
if *clientProtocol == "http" {
client, err = CreateHttpClient(ctx, false)
} else {
client, err = CreateGrpcClient(ctx)
}
client.SetRetry(
storage.WithBackoff(gax.Backoff{
Max: MaxRetryDuration,
Multiplier: RetryMultiplier,
}),
storage.WithPolicy(storage.RetryAlways))
if err != nil {
fmt.Errorf("while creating the client: %v", err)
}
bucketHandle := client.Bucket(BucketName)
err = bucketHandle.Create(ctx, ProjectName, nil)
if err != nil {
fmt.Errorf("while creating the bucket: %v", err)
}
wg.Add(NumOfWorker)
for i := 0; i < NumOfWorker; i++ {
go ReadObject(ctx, i, bucketHandle)
}
wg.Wait()
}