-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
format.go
148 lines (130 loc) · 4.59 KB
/
format.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
// Copyright 2019 The Cockroach Authors.
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.
package cli
import (
"fmt"
"time"
"github.com/cockroachdb/cockroach/pkg/workload/histogram"
)
// outputFormat is the interface used to output results incrementally
// during a workload run.
type outputFormat interface {
// rampDone is called once when the ramp-up period completes, if
// configured.
rampDone()
// outputError is called when an error is encountered.
outputError(err error)
// outputTick is called when the main loop considers it useful
// to emit one row of results.
outputTick(startElapsed time.Duration, t histogram.Tick)
// outputTotal is called at the end, using the main histogram
// collector.
outputTotal(startElapsed time.Duration, t histogram.Tick)
// outputResult is called at the end, using the result histogram
// collector.
outputResult(startElapsed time.Duration, t histogram.Tick)
}
// textFormatter produces output meant for quick parsing by humans. The
// data is printed as fixed-width columns. Summary rows
// are printed at the end.
type textFormatter struct {
i int
numErr int
}
func (f *textFormatter) rampDone() {
f.i = 0
}
func (f *textFormatter) outputError(_ error) {
f.numErr++
}
func (f *textFormatter) outputTick(startElapsed time.Duration, t histogram.Tick) {
if f.i%20 == 0 {
fmt.Println("_elapsed___errors__ops/sec(inst)___ops/sec(cum)__p50(ms)__p95(ms)__p99(ms)_pMax(ms)")
}
f.i++
fmt.Printf("%7.1fs %8d %14.1f %14.1f %8.1f %8.1f %8.1f %8.1f %s\n",
startElapsed.Seconds(),
f.numErr,
float64(t.Hist.TotalCount())/t.Elapsed.Seconds(),
float64(t.Cumulative.TotalCount())/startElapsed.Seconds(),
time.Duration(t.Hist.ValueAtQuantile(50)).Seconds()*1000,
time.Duration(t.Hist.ValueAtQuantile(95)).Seconds()*1000,
time.Duration(t.Hist.ValueAtQuantile(99)).Seconds()*1000,
time.Duration(t.Hist.ValueAtQuantile(100)).Seconds()*1000,
t.Name,
)
}
const totalHeader = "\n_elapsed___errors_____ops(total)___ops/sec(cum)__avg(ms)__p50(ms)__p95(ms)__p99(ms)_pMax(ms)"
func (f *textFormatter) outputTotal(startElapsed time.Duration, t histogram.Tick) {
f.outputFinal(startElapsed, t, "__total")
}
func (f *textFormatter) outputResult(startElapsed time.Duration, t histogram.Tick) {
f.outputFinal(startElapsed, t, "__result")
}
func (f *textFormatter) outputFinal(
startElapsed time.Duration, t histogram.Tick, titleSuffix string,
) {
fmt.Println(totalHeader + titleSuffix)
if t.Cumulative == nil {
return
}
if t.Cumulative.TotalCount() == 0 {
return
}
fmt.Printf("%7.1fs %8d %14d %14.1f %8.1f %8.1f %8.1f %8.1f %8.1f %s\n",
startElapsed.Seconds(),
f.numErr,
t.Cumulative.TotalCount(),
float64(t.Cumulative.TotalCount())/startElapsed.Seconds(),
time.Duration(t.Cumulative.Mean()).Seconds()*1000,
time.Duration(t.Cumulative.ValueAtQuantile(50)).Seconds()*1000,
time.Duration(t.Cumulative.ValueAtQuantile(95)).Seconds()*1000,
time.Duration(t.Cumulative.ValueAtQuantile(99)).Seconds()*1000,
time.Duration(t.Cumulative.ValueAtQuantile(100)).Seconds()*1000,
t.Name,
)
}
// jsonFormatter produces output that is machine-readable. The time is
// printed using absolute timestamps. No summary row is printed at the
// end.
type jsonFormatter struct {
numErr int
}
func (f *jsonFormatter) rampDone() {}
func (f *jsonFormatter) outputError(_ error) {
f.numErr++
}
func (f *jsonFormatter) outputTick(startElapsed time.Duration, t histogram.Tick) {
// Note: we use fmt.Printf here instead of json.Marshal to ensure
// that float values do not get printed with a uselessly large
// number of decimals.
fmt.Printf(`{Time:"%s",`+
`Errors:%d,`+
`OpsPerSec:%.1f,`+
`AvgLatMillis:%.1f,`+
`P50LatMillis:%.1f,`+
`P95LatMillis:%.1f,`+
`P99LatMillis:%.1f,`+
`PMaxLatMillis:%.1f,`+
`OpName:%q`+
"}\n",
t.Now.UTC().Format(time.RFC3339),
f.numErr,
float64(t.Hist.TotalCount())/t.Elapsed.Seconds(),
float64(t.Cumulative.TotalCount())/startElapsed.Seconds(),
time.Duration(t.Hist.ValueAtQuantile(50)).Seconds()*1000,
time.Duration(t.Hist.ValueAtQuantile(95)).Seconds()*1000,
time.Duration(t.Hist.ValueAtQuantile(99)).Seconds()*1000,
time.Duration(t.Hist.ValueAtQuantile(100)).Seconds()*1000,
t.Name,
)
}
func (f *jsonFormatter) outputTotal(startElapsed time.Duration, t histogram.Tick) {}
func (f *jsonFormatter) outputResult(startElapsed time.Duration, t histogram.Tick) {}