forked from minio/warp
/
analyze.go
597 lines (546 loc) · 18.1 KB
/
analyze.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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
/*
* Warp (C) 2019-2020 MinIO, Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package cli
import (
"encoding/json"
"errors"
"fmt"
"io"
"os"
"path/filepath"
"strings"
"time"
"github.com/fatih/color"
"github.com/klauspost/compress/zstd"
"github.com/minio/cli"
"github.com/minio/mc/pkg/probe"
"github.com/minio/pkg/console"
"github.com/suykerbuyk/warp/api"
"github.com/suykerbuyk/warp/pkg/aggregate"
"github.com/suykerbuyk/warp/pkg/bench"
)
var analyzeFlags = []cli.Flag{
cli.StringFlag{
Name: "analyze.dur",
Value: "",
Usage: "Split analysis into durations of this length. Can be '1s', '5s', '1m', etc.",
},
cli.StringFlag{
Name: "analyze.out",
Value: "",
Usage: "Output aggregated data as to file",
},
cli.StringFlag{
Name: "analyze.op",
Value: "",
Usage: "Only output for this op. Can be GET/PUT/DELETE, etc.",
},
cli.StringFlag{
Name: "analyze.host",
Value: "",
Usage: "Only output for this host.",
},
cli.DurationFlag{
Name: "analyze.skip",
Usage: "Additional duration to skip when analyzing data.",
Hidden: false,
Value: 0,
},
cli.IntFlag{
Name: "analyze.limit",
Usage: "Max operations to load for analysis.",
Hidden: true,
Value: 0,
},
cli.IntFlag{
Name: "analyze.offset",
Usage: "Skip this number of operations for analysis",
Hidden: true,
Value: 0,
},
cli.BoolFlag{
Name: "analyze.v",
Usage: "Display additional analysis data.",
},
cli.StringFlag{
Name: serverFlagName,
Usage: "When running benchmarks open a webserver to fetch results remotely, eg: localhost:7762",
},
}
var analyzeCmd = cli.Command{
Name: "analyze",
Usage: "analyze existing benchmark data",
Action: mainAnalyze,
Before: setGlobalsFromContext,
Flags: combineFlags(globalFlags, analyzeFlags),
CustomHelpTemplate: `NAME:
{{.HelpName}} - {{.Usage}}
USAGE:
{{.HelpName}} [FLAGS] benchmark-data-file
-> see https://github.com/minio/warp#analysis
Use - as input to read from stdin.
FLAGS:
{{range .VisibleFlags}}{{.}}
{{end}}`,
}
// mainAnalyze is the entry point for analyze command.
func mainAnalyze(ctx *cli.Context) error {
checkAnalyze(ctx)
args := ctx.Args()
if len(args) == 0 {
console.Fatal("No benchmark data file supplied")
}
if len(args) > 1 {
console.Fatal("Only one benchmark file can be given")
}
var zstdDec, _ = zstd.NewReader(nil)
defer zstdDec.Close()
monitor := api.NewBenchmarkMonitor(ctx.String(serverFlagName))
defer monitor.Done()
log := console.Printf
if globalQuiet {
log = nil
}
for _, arg := range args {
var input io.Reader
if arg == "-" {
input = os.Stdin
} else {
f, err := os.Open(arg)
fatalIf(probe.NewError(err), "Unable to open input file")
defer f.Close()
input = f
}
err := zstdDec.Reset(input)
fatalIf(probe.NewError(err), "Unable to read input")
ops, err := bench.OperationsFromCSV(zstdDec, true, ctx.Int("analyze.offset"), ctx.Int("analyze.limit"), log)
fatalIf(probe.NewError(err), "Unable to parse input")
printAnalysis(ctx, ops)
monitor.OperationsReady(ops, strings.TrimSuffix(filepath.Base(arg), ".csv.zst"), commandLine(ctx))
}
return nil
}
func printMixedOpAnalysis(ctx *cli.Context, aggr aggregate.Aggregated, details bool) {
console.SetColor("Print", color.New(color.FgWhite))
console.Printf("Mixed operations.")
if aggr.MixedServerStats == nil {
console.Errorln("No mixed stats")
}
for _, ops := range aggr.Operations {
console.Println("")
console.SetColor("Print", color.New(color.FgHiWhite))
pct := 0.0
if aggr.MixedServerStats.Operations > 0 {
pct = 100.0 * float64(ops.Throughput.Operations) / float64(aggr.MixedServerStats.Operations)
}
duration := ops.EndTime.Sub(ops.StartTime).Truncate(time.Second)
if !details {
console.Printf("Operation: %v, %d%%, Concurrency: %d, Duration: %v.\n", ops.Type, int(pct+0.5), ops.Concurrency, duration)
} else {
console.Printf("Operation: %v - total: %v, %.01f%%, Concurrency: %d, Duration: %v, starting %v\n", ops.Type, ops.Throughput.Operations, pct, ops.Concurrency, duration, ops.StartTime.Truncate(time.Millisecond))
}
console.SetColor("Print", color.New(color.FgWhite))
if ops.Skipped {
console.Println("Skipping", ops.Type, "too few samples. Longer benchmark run required for reliable results.")
continue
}
if ops.Errors > 0 {
console.SetColor("Print", color.New(color.FgHiRed))
console.Println("Errors:", ops.Errors)
if details {
for _, err := range ops.FirstErrors {
console.Println(err)
}
}
console.SetColor("Print", color.New(color.FgWhite))
}
eps := ops.ThroughputByHost
if len(eps) == 1 || !details {
console.Println(" * Throughput:", ops.Throughput.StringDetails(details))
}
if len(eps) > 1 && details {
console.SetColor("Print", color.New(color.FgWhite))
console.Println("\nThroughput by host:")
for ep, totals := range eps {
console.SetColor("Print", color.New(color.FgWhite))
console.Print(" * ", ep, ": Avg: ", totals.StringDetails(details), ".")
if totals.Errors > 0 {
console.SetColor("Print", color.New(color.FgHiRed))
console.Print(" Errors: ", totals.Errors)
}
console.Println("")
}
}
if details {
printRequestAnalysis(ctx, ops, details)
console.SetColor("Print", color.New(color.FgWhite))
}
}
console.SetColor("Print", color.New(color.FgHiWhite))
dur := time.Duration(aggr.MixedServerStats.MeasureDurationMillis) * time.Millisecond
dur = dur.Round(time.Second)
console.Printf("\nCluster Total: %v over %v.\n", aggr.MixedServerStats.StringDetails(details), dur)
if aggr.MixedServerStats.Errors > 0 {
console.SetColor("Print", color.New(color.FgHiRed))
console.Print("Total Errors:", aggr.MixedServerStats.Errors, ".\n")
}
console.SetColor("Print", color.New(color.FgWhite))
if eps := aggr.MixedThroughputByHost; len(eps) > 1 && details {
for ep, ops := range eps {
console.Println(" * "+ep+":", ops.StringDetails(details))
}
}
}
func printAnalysis(ctx *cli.Context, o bench.Operations) {
details := ctx.Bool("analyze.v")
var wrSegs io.Writer
prefiltered := false
if fn := ctx.String("analyze.out"); fn != "" {
if fn == "-" {
wrSegs = os.Stdout
} else {
f, err := os.Create(fn)
fatalIf(probe.NewError(err), "Unable to create create analysis output")
defer console.Println("Aggregated data saved to", fn)
defer f.Close()
wrSegs = f
}
}
if onlyHost := ctx.String("analyze.host"); onlyHost != "" {
o2 := o.FilterByEndpoint(onlyHost)
if len(o2) == 0 {
hosts := o.Endpoints()
console.Println("Host not found, valid hosts are:")
for _, h := range hosts {
console.Println("\t* %s", h)
}
return
}
prefiltered = true
o = o2
}
if wantOp := ctx.String("analyze.op"); wantOp != "" {
prefiltered = prefiltered || o.IsMixed()
o = o.FilterByOp(wantOp)
}
durFn := func(total time.Duration) time.Duration {
if total <= 0 {
return 0
}
return analysisDur(ctx, total)
}
aggr := aggregate.Aggregate(o, aggregate.Options{
Prefiltered: prefiltered,
DurFunc: durFn,
SkipDur: ctx.Duration("analyze.skip"),
})
if wrSegs != nil {
for _, ops := range aggr.Operations {
writeSegs(ctx, wrSegs, o.FilterByOp(ops.Type), aggr.Mixed || prefiltered, details)
}
}
if globalJSON {
b, err := json.MarshalIndent(aggr, "", " ")
fatalIf(probe.NewError(err), "Unable to marshal data.")
if err != nil {
console.Errorln(err)
}
os.Stdout.Write(b)
return
}
if aggr.Mixed {
printMixedOpAnalysis(ctx, aggr, details)
return
}
for _, ops := range aggr.Operations {
typ := ops.Type
console.Println("\n----------------------------------------")
opo := ops.ObjectsPerOperation
console.SetColor("Print", color.New(color.FgHiWhite))
hostsString := ""
if ops.Hosts > 1 {
hostsString = fmt.Sprintf(" Hosts: %d.", ops.Hosts)
}
if ops.Clients > 1 {
hostsString = fmt.Sprintf("%s Warp Instances: %d.", hostsString, ops.Clients)
}
if opo > 1 {
if details {
console.Printf("Operation: %v (%d). Objects per operation: %d. Concurrency: %d.%s\n", typ, ops.N, opo, ops.Concurrency, hostsString)
} else {
console.Printf("Operation: %v\n", typ)
}
} else {
if details {
console.Printf("Operation: %v (%d). Concurrency: %d.%s\n", typ, ops.N, ops.Concurrency, hostsString)
} else {
console.Printf("Operation: %v\n", typ)
}
}
if ops.Errors > 0 {
console.SetColor("Print", color.New(color.FgHiRed))
console.Println("Errors:", ops.Errors)
if details {
console.SetColor("Print", color.New(color.FgWhite))
console.Println("First Errors:")
for _, err := range ops.FirstErrors {
console.Println(" *", err)
}
console.Println("")
}
}
if ops.Skipped {
console.SetColor("Print", color.New(color.FgHiWhite))
console.Println("Skipping", typ, "too few samples. Longer benchmark run required for reliable results.")
continue
}
if details {
printRequestAnalysis(ctx, ops, details)
console.SetColor("Print", color.New(color.FgHiWhite))
console.Println("\nThroughput:")
}
console.SetColor("Print", color.New(color.FgWhite))
console.Println("* Average:", ops.Throughput.StringDetails(details))
if eps := ops.ThroughputByHost; len(eps) > 1 {
console.SetColor("Print", color.New(color.FgHiWhite))
console.Println("\nThroughput by host:")
for _, ep := range ops.HostNames {
ops := eps[ep]
console.SetColor("Print", color.New(color.FgWhite))
console.Print(" * ", ep, ":")
if !details {
console.Print(" Avg: ", ops.StringDetails(details), "\n")
} else {
console.Print("\n")
}
if ops.Errors > 0 {
console.SetColor("Print", color.New(color.FgHiRed))
console.Println("Errors:", ops.Errors)
}
if details {
seg := ops.Segmented
console.SetColor("Print", color.New(color.FgWhite))
if seg == nil || len(seg.Segments) <= 1 {
console.Println("Skipping", typ, "host:", ep, " - Too few samples. Longer benchmark run needed for reliable results.")
continue
}
console.SetColor("Print", color.New(color.FgWhite))
console.Println("\t- Average: ", ops.StringDetails(false))
console.Println("\t- Fastest:", aggregate.BPSorOPS(seg.FastestBPS, seg.FastestOPS))
console.Println("\t- 50% Median:", aggregate.BPSorOPS(seg.MedianBPS, seg.MedianOPS))
console.Println("\t- Slowest:", aggregate.BPSorOPS(seg.SlowestBPS, seg.SlowestOPS))
}
}
}
segs := ops.Throughput.Segmented
dur := time.Millisecond * time.Duration(segs.SegmentDurationMillis)
console.SetColor("Print", color.New(color.FgHiWhite))
console.Print("\nThroughput, split into ", len(segs.Segments), " x ", dur, ":\n")
console.SetColor("Print", color.New(color.FgWhite))
console.Println(" * Fastest:", aggregate.SegmentSmall{BPS: segs.FastestBPS, OPS: segs.FastestOPS, Start: segs.FastestStart}.StringLong(dur, details))
console.Println(" * 50% Median:", aggregate.SegmentSmall{BPS: segs.MedianBPS, OPS: segs.MedianOPS, Start: segs.MedianStart}.StringLong(dur, details))
console.Println(" * Slowest:", aggregate.SegmentSmall{BPS: segs.SlowestBPS, OPS: segs.SlowestOPS, Start: segs.SlowestStart}.StringLong(dur, details))
}
}
func writeSegs(ctx *cli.Context, wrSegs io.Writer, ops bench.Operations, allThreads, details bool) {
if wrSegs == nil {
return
}
totalDur := ops.Duration()
segs := ops.Segment(bench.SegmentOptions{
From: time.Time{},
PerSegDuration: analysisDur(ctx, totalDur),
AllThreads: allThreads && !ops.HasError(),
})
segs.SortByTime()
err := segs.CSV(wrSegs)
errorIf(probe.NewError(err), "Error writing analysis")
// Write segments per endpoint
eps := ops.Endpoints()
if details && len(eps) > 1 {
for _, ep := range eps {
ops := ops.FilterByEndpoint(ep)
segs := ops.Segment(bench.SegmentOptions{
From: time.Time{},
PerSegDuration: analysisDur(ctx, totalDur),
AllThreads: false,
})
if len(segs) <= 1 {
continue
}
totals := ops.Total(false)
if totals.TotalBytes > 0 {
segs.SortByThroughput()
} else {
segs.SortByObjsPerSec()
}
segs.SortByTime()
err := segs.CSV(wrSegs)
errorIf(probe.NewError(err), "Error writing analysis")
}
}
}
func printRequestAnalysis(ctx *cli.Context, ops aggregate.Operation, details bool) {
console.SetColor("Print", color.New(color.FgHiWhite))
if ops.SingleSizedRequests != nil {
reqs := *ops.SingleSizedRequests
// Single type, require one operation per thread.
console.Print("\nRequests considered: ", reqs.Requests, ":\n")
console.SetColor("Print", color.New(color.FgWhite))
if reqs.Skipped {
fmt.Println(reqs)
console.Println("Not enough requests")
return
}
console.Print(
" * Avg: ", time.Duration(reqs.DurAvgMillis)*time.Millisecond,
", 50%: ", time.Duration(reqs.DurMedianMillis)*time.Millisecond,
", 90%: ", time.Duration(reqs.Dur90Millis)*time.Millisecond,
", 99%: ", time.Duration(reqs.Dur99Millis)*time.Millisecond,
", Fastest: ", time.Duration(reqs.FastestMillis)*time.Millisecond,
", Slowest: ", time.Duration(reqs.SlowestMillis)*time.Millisecond,
"\n")
if reqs.FirstByte != nil {
console.Println(" * First Byte:", reqs.FirstByte)
}
if reqs.FirstAccess != nil {
reqs := reqs.FirstAccess
console.Print(
" * First Access: Avg: ", time.Duration(reqs.DurAvgMillis)*time.Millisecond,
", 50%: ", time.Duration(reqs.DurMedianMillis)*time.Millisecond,
", 90%: ", time.Duration(reqs.Dur90Millis)*time.Millisecond,
", 99%: ", time.Duration(reqs.Dur99Millis)*time.Millisecond,
", Fastest: ", time.Duration(reqs.FastestMillis)*time.Millisecond,
", Slowest: ", time.Duration(reqs.SlowestMillis)*time.Millisecond,
"\n")
if reqs.FirstByte != nil {
console.Print(" * First Access TTFB: ", reqs.FirstByte)
}
console.Println("")
}
if eps := reqs.ByHost; len(eps) > 1 && details {
console.SetColor("Print", color.New(color.FgHiWhite))
console.Println("\nRequests by host:")
for _, ep := range reqs.HostNames {
reqs := eps[ep]
if reqs.Requests <= 1 {
continue
}
console.SetColor("Print", color.New(color.FgWhite))
console.Println(" *", ep, "-", reqs.Requests, "requests:",
"\n\t- Avg:", time.Duration(reqs.DurAvgMillis)*time.Millisecond,
"Fastest:", time.Duration(reqs.FastestMillis)*time.Millisecond,
"Slowest:", time.Duration(reqs.SlowestMillis)*time.Millisecond,
"50%:", time.Duration(reqs.DurMedianMillis)*time.Millisecond,
"90%:", time.Duration(reqs.Dur90Millis)*time.Millisecond)
if reqs.FirstByte != nil {
console.Println("\t- First Byte:", reqs.FirstByte)
}
}
}
return
}
// Multi sized
if ops.MultiSizedRequests == nil {
console.Fatalln("Neither single-sized nor multi-sized requests found")
}
reqs := *ops.MultiSizedRequests
console.Print("\nRequests considered: ", reqs.Requests, ". Multiple sizes, average ", reqs.AvgObjSize, " bytes:\n")
console.SetColor("Print", color.New(color.FgWhite))
if reqs.Skipped {
console.Println("Not enough requests")
}
sizes := reqs.BySize
for _, s := range sizes {
console.SetColor("Print", color.New(color.FgHiWhite))
console.Print("\nRequest size ", s.MinSizeString, " -> ", s.MaxSizeString, ". Requests - ", s.Requests, ":\n")
console.SetColor("Print", color.New(color.FgWhite))
console.Print(""+
" * Throughput: Average: ", bench.Throughput(s.BpsAverage),
", 50%: ", bench.Throughput(s.BpsMedian),
", 90%: ", bench.Throughput(s.Bps90),
", 99%: ", bench.Throughput(s.Bps99),
", Fastest: ", bench.Throughput(s.BpsFastest),
", Slowest: ", bench.Throughput(s.BpsSlowest),
"\n")
if s.FirstByte != nil {
console.Println(" * First Byte:", s.FirstByte)
}
if s.FirstAccess != nil {
s := s.FirstAccess
console.Print(""+
" * First Access: Average: ", bench.Throughput(s.BpsAverage),
", 50%: ", bench.Throughput(s.BpsMedian),
", 90%: ", bench.Throughput(s.Bps90),
", 99%: ", bench.Throughput(s.Bps99),
", Fastest: ", bench.Throughput(s.BpsFastest),
", Slowest: ", bench.Throughput(s.BpsSlowest),
"\n")
if s.FirstByte != nil {
console.Print(" * First Access TTFB: ", s.FirstByte, "\n")
}
}
}
if eps := reqs.ByHost; len(eps) > 1 && details {
console.SetColor("Print", color.New(color.FgHiWhite))
console.Println("\nRequests by host:")
for ep, s := range reqs.ByHost {
if s.Requests <= 1 {
continue
}
console.SetColor("Print", color.New(color.FgWhite))
console.Println(" *", ep, "-", s.Requests, "requests:",
"\n\t- Avg:", bench.Throughput(s.BpsAverage),
"Fastest:", bench.Throughput(s.BpsFastest),
"Slowest:", bench.Throughput(s.BpsSlowest),
"50%:", bench.Throughput(s.BpsMedian),
"90%:", bench.Throughput(s.Bps90))
if s.FirstByte != nil {
console.Println(" * First Byte:", s.FirstByte)
}
}
}
}
// analysisDur returns the analysis duration or 0 if un-parsable.
func analysisDur(ctx *cli.Context, total time.Duration) time.Duration {
dur := ctx.String("analyze.dur")
if dur == "" {
if total == 0 {
return 0
}
// Find appropriate duration
// We want the smallest segmentation duration that produces at most this number of segments.
const wantAtMost = 400
// Standard durations to try:
stdDurations := []time.Duration{time.Second, 5 * time.Second, 15 * time.Second, time.Minute, 5 * time.Minute, 15 * time.Minute, time.Hour, 3 * time.Hour}
for _, d := range stdDurations {
dur = d.String()
if total/d <= wantAtMost {
break
}
}
}
d, err := time.ParseDuration(dur)
fatalIf(probe.NewError(err), "Invalid -analyze.dur value")
return d
}
func checkAnalyze(ctx *cli.Context) {
if analysisDur(ctx, time.Minute) == 0 {
err := errors.New("-analyze.dur cannot be 0")
fatal(probe.NewError(err), "Invalid -analyze.dur value")
}
}