-
Notifications
You must be signed in to change notification settings - Fork 568
/
main.go
380 lines (349 loc) · 10.6 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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
// Command benchmark-uploads uploads data to Pachyderm and reports the observed upload throughput.
// You should have a Pachyderm context already configured; we use that.
//
// It is possible to upload over protocols other than gRPC. We still use the Pachyderm context for
// some setup/cleanup and to get your auth token. Make sure that -addr and -scheme and the context
// refer to the same server; we have no way to check that for you.
//
// Run `go run ./etc/benchmark-uploads -h` for help.
//nolint:wrapcheck
package main
import (
"bytes"
"context"
"crypto/tls"
"encoding/json"
"flag"
"fmt"
"io"
"log"
"mime/multipart"
"net"
"net/http"
"net/http/httputil"
"os"
"os/signal"
"strconv"
"time"
"github.com/docker/go-units"
"github.com/dustin/go-humanize"
"github.com/minio/minio-go/v7"
"github.com/minio/minio-go/v7/pkg/credentials"
"github.com/pachyderm/pachyderm/v2/src/client"
"github.com/pkg/errors"
uuid "github.com/satori/go.uuid"
"golang.org/x/crypto/blake2b"
"golang.org/x/net/http2"
)
var (
size = flag.String("size", "50MB", "size of the file to upload")
kind = flag.String("kind", "grpc", "type of benchmark to perform; grpc, http (requires experimental pachd), s3, s3-multipart, console")
scheme = flag.String("scheme", "http", "url scheme for http-based protocols")
addr = flag.String("addr", "localhost", "for http, console, and s3; the address to connect to (always requires a valid pach context, though)")
h2c = flag.Bool("h2c", false, "if true, use http2 over cleartext (only works against proxy)")
iters = flag.Int("n", 10, "number of times to run the benchmark; reusing connections between tests where allowed in the protocol")
checkIntegrity = flag.Bool("check", false, "if true, check the integrity of uploaded files by downloading them")
)
type R struct {
b byte
Len uint64
EOFAt time.Time
}
func (r *R) Read(p []byte) (int, error) {
var n int
if r.Len == 0 {
fmt.Printf("0\n")
r.EOFAt = time.Now()
return 0, io.EOF
}
for i := range p {
if r.Len > 0 {
p[i] = r.b
r.Len--
n++
} else {
fmt.Printf(".\n")
r.EOFAt = time.Now()
return n, io.EOF
}
}
r.b++
fmt.Printf(".")
return n, nil
}
var _ io.Reader = new(R)
// CW is an io.Writer that counts the bytes written to it.
type CW int
func (n *CW) Write(p []byte) (int, error) {
*n += CW(len(p))
return len(p), nil
}
var _ io.Writer = new(CW)
var hashes = make(map[string][]byte)
func bench(f func(name string, r io.Reader, length uint64) error) error {
for i := 0; i < *iters; i++ {
r := new(R)
var length uint64
if x, err := humanize.ParseBytes(*size); err != nil {
log.Fatalf("parse size: %v", err)
} else {
r.Len = x
length = x
}
n, err := uuid.NewV4()
if err != nil {
log.Fatalf("new uuid: %v", err)
}
name := n.String()
h, _ := blake2b.New256(nil) // Can't return an error.
hr := io.TeeReader(r, h)
start := time.Now()
if err := f(name, hr, length); err != nil {
return err
}
log.Printf("total time: %v", time.Since(start).String())
log.Printf(" = %s/s", humanize.Bytes(uint64(float64(length)/float64(time.Since(start).Seconds()))))
log.Printf("read time: %v", time.Since(start)-time.Since(r.EOFAt))
log.Printf("flush time: %v", time.Since(r.EOFAt).String())
log.Printf("blake2b checksum: %x", h.Sum(nil))
hashes[name] = h.Sum(nil)
}
return nil
}
func main() {
flag.Parse()
// Cancel context on first SIGINT.
ctx, cancel := context.WithCancel(context.Background())
cancelCh := make(chan os.Signal, 1)
signal.Notify(cancelCh, os.Interrupt)
go func() {
<-cancelCh
cancel()
signal.Stop(cancelCh)
close(cancelCh)
}()
// Setup pach context for creating destination repo, etc.
c, err := client.NewOnUserMachineContext(ctx, "")
if err != nil {
log.Fatal(err)
}
c = c.WithCtx(ctx)
if err := c.CreateProjectRepo("default", "benchmark-upload"); err != nil {
log.Printf("create repo: %v", err)
}
hc := &http.Client{}
if *h2c {
hc = &http.Client{
Transport: &http2.Transport{
AllowHTTP: true,
DialTLSContext: func(ctx context.Context, network, addr string, cfg *tls.Config) (net.Conn, error) {
return (&net.Dialer{}).DialContext(ctx, network, addr)
},
},
}
}
// Run benchmark.
commit := client.NewProjectCommit("default", "benchmark-upload", "master", "")
var benchErr error
switch *kind {
case "grpc":
benchErr = bench(func(name string, r io.Reader, _ uint64) error {
return c.PutFile(commit, name, r)
})
case "http":
benchErr = bench(func(name string, r io.Reader, _ uint64) error {
req, err := http.NewRequestWithContext(ctx, "PUT", *scheme+"://"+*addr+"/upload/"+name, r)
if err != nil {
return err
}
res, err := hc.Do(req)
if err != nil {
return err
}
defer res.Body.Close()
d, err := httputil.DumpResponse(res, true)
if err != nil {
return err
}
log.Printf("%s", d)
if got, want := res.StatusCode, http.StatusAccepted; got != want {
return fmt.Errorf("unexpected status: got %d want %d: %v", got, want, res.Status)
}
return nil
})
case "console":
benchErr = bench(func(name string, r io.Reader, length uint64) error {
// Start
start, err := json.Marshal(map[string]any{
"branch": "master",
"path": "/",
"repo": "benchmark-upload",
})
if err != nil {
return errors.Wrap(err, "marshal start")
}
req, err := http.NewRequestWithContext(ctx, "POST", *scheme+"://"+*addr+"/upload/start", bytes.NewReader(start))
if err != nil {
return err
}
req.Header.Add("content-type", "application/json")
req.AddCookie(&http.Cookie{
Name: "dashAuthToken",
Value: c.AuthToken(),
})
res, err := hc.Do(req)
if err != nil {
return errors.Wrap(err, "start")
}
body, err := io.ReadAll(res.Body)
res.Body.Close()
if err != nil {
return errors.Wrap(err, "read start body")
}
if got, want := res.StatusCode, http.StatusOK; got != want {
return fmt.Errorf("start: unexpected status code: got %d want %d %v", got, want, res.Status)
}
var idInfo struct {
UploadId string `json:"uploadId"`
}
if err := json.Unmarshal(body, &idInfo); err != nil {
return errors.Wrap(err, "unmarshal start reply")
}
// Upload
ckSize := 50 * units.MB
nCk := (int(length) + ckSize - 1) / ckSize
buf := make([]byte, units.MB)
for i := 1; i <= nCk; i++ { // chunks start at 1, not 0
uploadR, uploadW := io.Pipe()
w := multipart.NewWriter(uploadW)
go func() {
defer uploadW.Close()
defer w.Close()
_ = w.WriteField("uploadId", idInfo.UploadId)
_ = w.WriteField("fileName", name)
_ = w.WriteField("chunkTotal", strconv.FormatInt(int64(nCk), 10))
_ = w.WriteField("currentChunk", strconv.FormatInt(int64(i), 10))
fw, _ := w.CreateFormFile("file", name)
if _, err := io.CopyBuffer(fw, io.LimitReader(r, int64(ckSize)), buf); err != nil {
uploadW.CloseWithError(fmt.Errorf("copy: %w", err))
}
}()
req, err := http.NewRequestWithContext(ctx, "POST", *scheme+"://"+*addr+"/upload", uploadR)
if err != nil {
return err
}
req.Header.Add("content-type", w.FormDataContentType())
req.AddCookie(&http.Cookie{
Name: "dashAuthToken",
Value: c.AuthToken(),
})
res, err := hc.Do(req)
if err != nil {
return errors.Wrapf(err, "chunk %d/%d", i, nCk)
}
res.Body.Close()
if got, want := res.StatusCode, http.StatusOK; got != want {
return fmt.Errorf("chunk %d/%d: unexpected status code: got %d want %d %v", i, nCk, got, want, res.Status)
}
fmt.Printf(",")
}
n, err := r.Read(buf)
if n != 0 {
return fmt.Errorf("%d bytes remain unexpectedly", n)
}
if err == nil || err != io.EOF {
return fmt.Errorf("reader finished with unexpected error: %w", err)
}
fmt.Printf("\n")
// Finish
finish, err := json.Marshal(idInfo) // could reuse "body" from start section
if err != nil {
return errors.Wrap(err, "marshal finish request")
}
req, err = http.NewRequestWithContext(ctx, "POST", *scheme+"://"+*addr+"/upload/finish", bytes.NewReader(finish))
if err != nil {
return err
}
req.Header.Add("content-type", "application/json")
req.AddCookie(&http.Cookie{
Name: "dashAuthToken",
Value: c.AuthToken(),
})
res, err = hc.Do(req)
if err != nil {
return errors.Wrap(err, "finish")
}
res.Body.Close()
if got, want := res.StatusCode, http.StatusOK; got != want {
return fmt.Errorf("finish: unexpected status code: got %d want %d %v", got, want, res.Status)
}
return nil
})
case "s3":
mc, err := minio.New(*addr, &minio.Options{
Creds: credentials.NewStaticV4(c.AuthToken(), c.AuthToken(), ""),
Secure: *scheme == "https",
})
if err != nil {
log.Fatalf("make minio client: %v", err)
}
benchErr = bench(func(name string, r io.Reader, length uint64) error {
res, err := mc.PutObject(ctx, "master.benchmark-upload", name, r, int64(length), minio.PutObjectOptions{
DisableMultipart: true,
})
log.Printf("minio reply: %#v", res)
return err
})
case "s3-multipart":
mc, err := minio.New(*addr, &minio.Options{
Creds: credentials.NewStaticV4(c.AuthToken(), c.AuthToken(), ""),
Secure: *scheme == "https",
})
if err != nil {
log.Fatalf("minio: %v", err)
}
benchErr = bench(func(name string, r io.Reader, length uint64) error {
res, err := mc.PutObject(ctx, "master.benchmark-upload", name, r, int64(length), minio.PutObjectOptions{
DisableMultipart: false,
})
log.Printf("minio response: %#v", res)
return err
})
default:
log.Fatalf("unknown benchmark kind %v", *kind)
}
// Exit if benchmark returned an error.
if benchErr != nil {
log.Fatal(benchErr)
}
// Check integrity and delete files that uploaded ok.
if *checkIntegrity {
fmt.Println()
for name, want := range hashes {
h, _ := blake2b.New256(nil) // cannot error
cw := new(CW)
w := io.MultiWriter(h, cw)
start := time.Now()
// we have to GetFile because the hash in the FileInfo depends on some internals
err := c.GetFile(commit, name, w)
d := time.Since(start)
if err != nil {
log.Fatalf("get %v: %v", name, err)
}
log.Printf("download %v: %v in %v = %v/s", name, humanize.Bytes(uint64(*cw)), d.String(), humanize.Bytes(uint64(float64(*cw)/float64(d.Seconds()))))
if got := h.Sum(nil); !bytes.Equal(got, want) {
log.Printf("WARNING: %v: checksum mismatch:\n got: %x\nwant: %x", name, got, want)
continue
}
log.Printf("%v: integrity ok; deleting", name)
if err := c.DeleteFile(commit, name); err != nil {
log.Printf("delete %v: %v", name, err)
}
}
}
// Cleanup.
if err := c.Close(); err != nil {
log.Fatalf("close: %v", err)
}
cancel()
}