-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmain.go
270 lines (227 loc) · 7.83 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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
package main
import (
"context"
"crypto/tls"
"flag"
"fmt"
"io"
"log"
"net/http"
// Register the pprof endpoints under the web server root at /debug/pprof
_ "net/http/pprof"
"os"
"strconv"
"time"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/trace"
"cloud.google.com/go/profiler"
"cloud.google.com/go/storage"
"cloud.google.com/go/storage/experimental"
"github.com/googleapis/gax-go/v2"
"go.opencensus.io/stats"
"golang.org/x/oauth2"
"golang.org/x/sync/errgroup"
"google.golang.org/api/option"
)
var (
grpcConnPoolSize = 1
maxConnsPerHost = 100
maxIdleConnsPerHost = 100
// MB means 1024 Kb.
MB = 1024 * 1024
numOfWorker = flag.Int("worker", 48, "Number of concurrent worker to read")
numOfReadCallPerWorker = flag.Int("read-call-per-worker", 1000000, "Number of read call per worker")
maxRetryDuration = 30 * time.Second
retryMultiplier = 2.0
bucketName = flag.String("bucket", "princer-working-dirs", "GCS bucket name.")
// ProjectName denotes gcp project name.
ProjectName = flag.String("project", "gcs-fuse-test", "GCP project name.")
clientProtocol = flag.String("client-protocol", "http", "Network protocol.")
// Object name = objectNamePrefix + {thread_id} + objectNameSuffix
objectNamePrefix = flag.String("obj-prefix", "princer_100M_files/file_", "Object prefix")
objectNameSuffix = flag.String("obj-suffix", "", "Object suffix")
tracerName = "princer-storage-benchmark"
enableTracing = flag.Bool("enable-tracing", false, "Enable tracing with Cloud Trace export")
enablePprof = flag.Bool("enable-pprof", false, "Enable pprof server")
traceSampleRate = flag.Float64("trace-sample-rate", 1.0, "Sampling rate for Cloud Trace")
// Cloud profiler.
enableCloudProfiler = flag.Bool("enable-cloud-profiler", false, "Enable cloud profiler")
enableHeap = flag.Bool("heap", false, "enable heap profile collection")
enableCPU = flag.Bool("cpu", true, "enable cpu profile collection")
enableHeapAlloc = flag.Bool("heap_alloc", false, "enable heap allocation profile collection")
enableThread = flag.Bool("thread", false, "enable thread profile collection")
enableContention = flag.Bool("contention", false, "enable contention profile collection")
projectID = flag.String("project_id", "", "project ID to run profiler with; only required when running outside of GCP.")
version = flag.String("version", "original", "version to run profiler with")
// Enable read stall retry.
enableReadStallRetry = flag.Bool("enable-read-stall-retry", false, "Enable read stall retry")
eG errgroup.Group
)
// CreateHTTPClient create http storage client.
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 {
transport = &http.Transport{
MaxConnsPerHost: maxConnsPerHost,
MaxIdleConnsPerHost: maxIdleConnsPerHost,
// 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",
}
if *enableReadStallRetry {
return storage.NewClient(ctx, option.WithHTTPClient(httpClient),
experimental.WithReadStallTimeout(&experimental.ReadStallTimeoutConfig{
Min: time.Second,
TargetPercentile: 0.99,
}))
}
return storage.NewClient(ctx, option.WithHTTPClient(httpClient))
}
// CreateGrpcClient creates grpc client.
func CreateGrpcClient(ctx context.Context) (client *storage.Client, err error) {
tokenSource, err := GetTokenSource(ctx, "")
if err != nil {
return nil, err
}
return storage.NewGRPCClient(ctx, option.WithGRPCConnectionPool(grpcConnPoolSize), option.WithTokenSource(tokenSource), storage.WithDisabledClientMetrics())
}
// ReadObject creates reader object corresponding to workerID with the help of bucketHandle.
func ReadObject(ctx context.Context, workerID int, bucketHandle *storage.BucketHandle) (err error) {
objectName := *objectNamePrefix + strconv.Itoa(workerID) + *objectNameSuffix
for i := 0; i < *numOfReadCallPerWorker; i++ {
var span trace.Span
traceCtx, span := otel.GetTracerProvider().Tracer(tracerName).Start(ctx, "ReadObject")
span.SetAttributes(
attribute.KeyValue{Key: "bucket", Value: attribute.StringValue(*bucketName)},
)
start := time.Now()
object := bucketHandle.Object(objectName)
rc, err := object.NewReader(traceCtx)
if err != nil {
return fmt.Errorf("while creating reader object: %v", err)
}
firstByteTime := time.Since(start)
stats.Record(ctx, firstByteReadLatency.M(float64(firstByteTime.Milliseconds())))
// Calls Reader.WriteTo implicitly.
_, err = io.Copy(io.Discard, rc)
if err != nil {
return fmt.Errorf("while reading and discarding content: %v", err)
}
duration := time.Since(start)
stats.Record(ctx, readLatency.M(float64(duration.Milliseconds())))
err = rc.Close()
span.End()
if err != nil {
return fmt.Errorf("while closing the reader object: %v", err)
}
}
return
}
func main() {
flag.Parse()
ctx := context.Background()
if *enableTracing {
cleanup := enableTraceExport(ctx, *traceSampleRate)
defer cleanup()
}
// Start a pprof server.
// Example usage (run the following command while the script is running):
// go tool pprof http://localhost:8080/debug/pprof/profile?seconds=60
if *enablePprof {
go func() {
if err := http.ListenAndServe("localhost:8080", nil); err != nil {
log.Fatalf("error starting http server for pprof: %v", err)
}
}()
}
if *enableCloudProfiler {
if err := profiler.Start(profiler.Config{
Service: "custom-go-benchmark",
ServiceVersion: *version,
ProjectID: *projectID,
NoCPUProfiling: !*enableCPU,
NoHeapProfiling: !*enableHeap,
NoAllocProfiling: !*enableHeapAlloc,
NoGoroutineProfiling: !*enableThread,
MutexProfiling: *enableContention,
DebugLogging: true,
}); err != nil {
log.Fatalf("Failed to start profiler: %v", err)
}
}
var client *storage.Client
var err error
if *clientProtocol == "http" {
client, err = CreateHTTPClient(ctx, false)
} else {
client, err = CreateGrpcClient(ctx)
}
if err != nil {
fmt.Printf("while creating the client: %v", err)
os.Exit(1)
}
client.SetRetry(
storage.WithBackoff(gax.Backoff{
Max: maxRetryDuration,
Multiplier: retryMultiplier,
}),
storage.WithPolicy(storage.RetryAlways))
// assumes bucket already exist
bucketHandle := client.Bucket(*bucketName)
// Enable stack-driver exporter.
registerLatencyView()
registerFirstByteLatencyView()
err = enableSDExporter()
if err != nil {
fmt.Printf("while enabling stackdriver exporter: %v", err)
os.Exit(1)
}
defer closeSDExporter()
// Run the actual workload
for i := 0; i < *numOfWorker; i++ {
idx := i
eG.Go(func() error {
err = ReadObject(ctx, idx, bucketHandle)
if err != nil {
err = fmt.Errorf("while reading object %v: %w", *objectNamePrefix+strconv.Itoa(idx)+*objectNameSuffix, err)
return err
}
return err
})
}
err = eG.Wait()
if err == nil {
fmt.Println("Read benchmark completed successfully!")
} else {
fmt.Fprintf(os.Stderr, "Error while running benchmark: %v", err)
os.Exit(1)
}
}