-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreporter.go
495 lines (445 loc) · 12.1 KB
/
reporter.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
package main
import (
"cmp"
"encoding/csv"
"encoding/json"
"fmt"
"log"
"os"
"slices"
"strconv"
"strings"
"sync"
"time"
)
// Reporter is used to report on the outcome of a benchmark run
// by printing out the results to the console and a set of output
// files.
type Reporter struct {
sync.Mutex
outputDir string
printInterval time.Duration
lastReportTime time.Time
// Individual reporters
queries *queryReporter
upserts *upsertReporter
}
// Report is a JSON-serializable report of the benchmark run.
type Report map[string]any
// MergeOther merges another report into this one.
func (r Report) MergeOther(other Report) {
for k, v := range other {
if _, ok := r[k]; ok {
panic(fmt.Sprintf("duplicate key in report: %s", k))
}
r[k] = v
}
}
// PrintWithDepth prints a report with the given depth.
// Recursively prints sub-reports.
func (r Report) PrintWithDepth(depth int) {
keys := make([]string, 0, len(r))
for k := range r {
keys = append(keys, k)
}
slices.Sort(keys)
for _, k := range keys {
v := r[k]
if sub, ok := v.(Report); ok {
fmt.Printf("%s%s:\n", strings.Repeat(" ", depth), k)
sub.PrintWithDepth(depth + 1)
} else {
fmt.Printf("%s%s: %v\n", strings.Repeat(" ", depth), k, v)
}
}
}
// NewReporter starts a new reporter.
func StartReporter() (*Reporter, error) {
out := *outputDir
if out != "" {
if _, err := os.Stat(out); err == nil {
return nil, fmt.Errorf("output directory already exists: %s", out)
}
if err := os.MkdirAll(out, 0755); err != nil {
return nil, fmt.Errorf("failed to create output directory: %w", err)
}
} else {
log.Printf("no -output-dir specified, results will not be written to disk")
}
queries, err := newQueryReporter(outputDir)
if err != nil {
return nil, fmt.Errorf("failed to create query reporter: %w", err)
}
upserts, err := newUpsertReporter(outputDir)
if err != nil {
return nil, fmt.Errorf("failed to create upsert reporter: %w", err)
}
return &Reporter{
outputDir: out,
printInterval: time.Second * 10,
lastReportTime: time.Time{},
queries: queries,
upserts: upserts,
}, nil
}
// Stop stops the reporter and flushes any remaining data.
// Should be called at the end of a benchmark run.
func (r *Reporter) Stop() error {
r.Lock()
defer r.Unlock()
if err := r.queries.flush(); err != nil {
return fmt.Errorf("failed to flush queries.csv: %w", err)
}
if err := r.upserts.flush(); err != nil {
return fmt.Errorf("failed to flush upserts.csv: %w", err)
}
var (
finalReport = make(Report)
overAllPeriods = true
)
if report := r.queries.generateReport(overAllPeriods); len(report) > 0 {
finalReport.MergeOther(report)
}
if report := r.upserts.generateReport(overAllPeriods); len(report) > 0 {
finalReport.MergeOther(report)
}
fmt.Println("")
fmt.Printf("Cumulative report for the entire benchmark:\n")
finalReport.PrintWithDepth(0)
if r.outputDir != "" {
f, err := os.Create(fmt.Sprintf("%s/report.json", r.outputDir))
if err != nil {
return fmt.Errorf("failed to create report.json: %w", err)
}
defer f.Close()
if err := json.NewEncoder(f).Encode(finalReport); err != nil {
return fmt.Errorf("failed to write report.json: %w", err)
}
log.Printf("results written to: %s", r.outputDir)
}
return nil
}
// ReportQuery reports the results of a single query.
func (r *Reporter) ReportQuery(
namespace string,
size int,
clientDuration time.Duration,
serverTiming *ServerTiming,
) error {
r.Lock()
defer r.Unlock()
if err := r.queries.reportQuery(namespace, size, clientDuration, serverTiming); err != nil {
return fmt.Errorf("failed to report query: %w", err)
}
r.maybePrintReport()
return nil
}
// ReportUpsert reports the results of a single upsert.
func (r *Reporter) ReportUpsert(
namespace string,
numDocuments int,
totalBytes int,
clientDuration time.Duration,
) error {
r.Lock()
defer r.Unlock()
if err := r.upserts.reportUpsert(namespace, numDocuments, totalBytes, clientDuration); err != nil {
return fmt.Errorf("failed to report upsert: %w", err)
}
r.maybePrintReport()
return nil
}
// maybePrintReport prints a report if the print interval has elapsed.
// Requires the lock to be held.
func (r *Reporter) maybePrintReport() {
if time.Since(r.lastReportTime) < r.printInterval {
return
} else if r.lastReportTime.IsZero() {
r.lastReportTime = time.Now()
return
}
var (
report = make(Report)
overAllPeriods = false
)
if qr := r.queries.generateReport(overAllPeriods); len(qr) > 0 {
report.MergeOther(qr)
}
if ur := r.upserts.generateReport(overAllPeriods); len(ur) > 0 {
report.MergeOther(ur)
}
fmt.Println("")
fmt.Printf("Report for the last %s:\n", r.printInterval)
report.PrintWithDepth(0)
r.lastReportTime = time.Now()
r.queries.advanceReportingWindow()
r.upserts.advanceReportingWindow()
}
// QueryReporter is used to report benchmark results for queries.
type queryReporter struct {
queryLatencies map[CacheTemperature][][]time.Duration
outputFile *csv.Writer
}
func newQueryReporter(outputDir *string) (*queryReporter, error) {
var outputFile *csv.Writer
if outputDir != nil {
f, err := os.Create(fmt.Sprintf("%s/queries.csv", *outputDir))
if err != nil {
return nil, fmt.Errorf("failed to create queries.csv: %w", err)
}
outputFile = csv.NewWriter(f)
headers := []string{
"namespace",
"size",
"cache_temperature",
"client_duration_ms",
"server_duration_ms",
"exhaustive_count",
}
if err := outputFile.Write(headers); err != nil {
return nil, fmt.Errorf("failed to write header to queries.csv: %w", err)
}
}
ret := &queryReporter{
queryLatencies: nil, // Initialized by advanceReportingWindow
outputFile: outputFile,
}
ret.advanceReportingWindow()
return ret, nil
}
func (qr *queryReporter) advanceReportingWindow() {
if qr.queryLatencies == nil {
qr.queryLatencies = make(map[CacheTemperature][][]time.Duration)
}
for _, temp := range AllCacheTemperatures() {
qr.queryLatencies[temp] = append(qr.queryLatencies[temp], nil)
}
}
func (qr *queryReporter) reportQuery(
namespace string,
size int,
clientDuration time.Duration,
serverTiming *ServerTiming,
) error {
var (
temp = *serverTiming.CacheTemperature
latencies = qr.queryLatencies[temp]
window = len(latencies) - 1
)
latencies[window] = append(latencies[window], clientDuration)
if qr.outputFile != nil {
var exhaustiveCount int64
if serverTiming.ExhaustiveCount != nil {
exhaustiveCount = *serverTiming.ExhaustiveCount
}
if err := qr.outputFile.Write([]string{
namespace,
strconv.FormatInt(int64(size), 10),
string(temp),
strconv.FormatInt(clientDuration.Milliseconds(), 10),
strconv.FormatUint(*serverTiming.ProcessingTimeMs, 10),
strconv.FormatInt(exhaustiveCount, 10),
}); err != nil {
return fmt.Errorf("failed to write query to queries.csv: %w", err)
}
}
return nil
}
func (qr *queryReporter) generateReport(allReportingPeriods bool) Report {
report := make(Report)
for temp, latencies := range qr.queryLatencies {
// Generate a combined set of latencies
// By default, we only report on the last reporting window
var combined []time.Duration
if allReportingPeriods {
combined = combineLatencies(latencies)
} else {
combined = latencies[len(latencies)-1]
}
if len(combined) == 0 {
continue
}
slices.Sort(combined)
percentile := func(p float64) int64 {
var idx int
if p == 0.0 {
idx = 0
} else if p == 1.0 {
idx = len(combined) - 1
} else {
idx = int(float64(len(combined)) * p)
}
return combined[idx].Milliseconds()
}
var latencies strings.Builder
latencies.WriteString(fmt.Sprintf("min=%dms", percentile(0.0)))
for _, p := range []float64{10.0, 25.0, 50.0, 75.0, 90.0, 95.0, 99.0} {
latencies.WriteString(fmt.Sprintf(", p%d=%dms", int(p), percentile(p/100.0)))
}
latencies.WriteString(fmt.Sprintf(", max=%dms", percentile(1.0)))
report[fmt.Sprintf("%s_queries", temp)] = Report{
"count": int64(len(combined)),
"latencies": latencies.String(),
}
}
return report
}
func (qr *queryReporter) flush() error {
if qr.outputFile != nil {
qr.outputFile.Flush()
if err := qr.outputFile.Error(); err != nil {
return fmt.Errorf("failed to flush queries.csv: %w", err)
}
}
return nil
}
// UpsertStats contains statistics about a single upsert operation.
type UpsertStats struct {
NumDocuments int
TotalBytes int
ClientTime time.Duration
}
// UpsertReporter is used to report benchmark results for upserts.
type upsertReporter struct {
upserts [][]UpsertStats
outputFile *csv.Writer
}
func newUpsertReporter(outputDir *string) (*upsertReporter, error) {
var outputFile *csv.Writer
if outputDir != nil {
f, err := os.Create(fmt.Sprintf("%s/upserts.csv", *outputDir))
if err != nil {
return nil, fmt.Errorf("failed to create upserts.csv: %w", err)
}
outputFile = csv.NewWriter(f)
headers := []string{
"namespace",
"num_documents",
"request_bytes",
"client_duration_ms",
}
if err := outputFile.Write(headers); err != nil {
return nil, fmt.Errorf("failed to write header to upserts.csv: %w", err)
}
}
ret := &upsertReporter{
upserts: [][]UpsertStats{nil},
outputFile: outputFile,
}
return ret, nil
}
func (ur *upsertReporter) advanceReportingWindow() {
ur.upserts = append(ur.upserts, nil)
}
func (ur *upsertReporter) reportUpsert(
namespace string,
numDocuments int,
totalBytes int,
clientDuration time.Duration,
) error {
window := len(ur.upserts) - 1
ur.upserts[window] = append(ur.upserts[window], UpsertStats{
NumDocuments: numDocuments,
TotalBytes: totalBytes,
ClientTime: clientDuration,
})
if ur.outputFile != nil {
if err := ur.outputFile.Write([]string{
namespace,
strconv.FormatInt(int64(numDocuments), 10),
strconv.FormatInt(int64(totalBytes), 10),
strconv.FormatInt(clientDuration.Milliseconds(), 10),
}); err != nil {
return fmt.Errorf("failed to write upsert to upserts.csv: %w", err)
}
}
return nil
}
func (ur *upsertReporter) generateReport(allReportingPeriods bool) Report {
var combined []UpsertStats
if allReportingPeriods {
combined = combineUpsertStats(ur.upserts)
} else if len(ur.upserts) > 1 {
combined = ur.upserts[len(ur.upserts)-1]
}
if len(combined) == 0 {
return nil
}
slices.SortFunc(combined, func(a, b UpsertStats) int {
return cmp.Compare(a.ClientTime, b.ClientTime)
})
percentile := func(p float64) int64 {
var idx int
if p == 0.0 {
idx = 0
} else if p == 1.0 {
idx = len(combined) - 1
} else {
idx = int(float64(len(combined)) * p)
}
return combined[idx].ClientTime.Milliseconds()
}
var (
totalDocs int64
totalBytes int64
)
for _, upsert := range combined {
totalDocs += int64(upsert.NumDocuments)
totalBytes += int64(upsert.TotalBytes)
}
var latencies strings.Builder
latencies.WriteString(fmt.Sprintf("min=%dms", percentile(0.0)))
for _, p := range []float64{10.0, 25.0, 50.0, 75.0, 90.0, 95.0, 99.0} {
latencies.WriteString(fmt.Sprintf(", p%d=%dms", int(p), percentile(p/100.0)))
}
latencies.WriteString(fmt.Sprintf(", max=%dms", percentile(1.0)))
return Report{
"upserts": Report{
"num_requests": int64(len(combined)),
"num_documents": totalDocs,
"total_bytes": totalBytes,
"latencies": latencies.String(),
},
}
}
func (ur *upsertReporter) flush() error {
if ur.outputFile != nil {
ur.outputFile.Flush()
if err := ur.outputFile.Error(); err != nil {
return fmt.Errorf("failed to flush upserts.csv: %w", err)
}
}
return nil
}
func combineLatencies(latencies [][]time.Duration) []time.Duration {
if len(latencies) == 0 {
return nil
} else if len(latencies) == 1 {
return latencies[0]
}
var total int
for _, l := range latencies {
total += len(l)
}
combined := make([]time.Duration, 0, total)
for _, l := range latencies {
combined = append(combined, l...)
}
return combined
}
func combineUpsertStats(stats [][]UpsertStats) []UpsertStats {
if len(stats) == 0 {
return nil
} else if len(stats) == 1 {
return stats[0]
}
var total int
for _, s := range stats {
total += len(s)
}
combined := make([]UpsertStats, 0, total)
for _, s := range stats {
combined = append(combined, s...)
}
return combined
}