-
Notifications
You must be signed in to change notification settings - Fork 0
/
benchcheck.go
305 lines (265 loc) · 7.81 KB
/
benchcheck.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
package benchcheck
import (
"encoding/json"
"fmt"
"io"
"os/exec"
"strconv"
"strings"
"golang.org/x/perf/benchstat"
)
// CheckerFmt represents the expected string format of a checker.
const CheckerFmt = "<metric>=(+|-)<number>%"
// Module represents a Go module.
type Module struct {
path string
}
// StatResult is the full result showing performance
// differences between two benchmark runs (set of benchmark functions)
// for a specific metric, like time/op or speed.
type StatResult struct {
// Metric is the name of metric
Metric string
// BenchDiffs has the performance diff of all function for a given metric.
BenchDiffs []BenchDiff
}
// BenchResults represents a single Go benchmark run. Each
// string is the result of a single Benchmark like this:
// - "BenchmarkName 50 31735022 ns/op 61.15 MB/s"
type BenchResults []string
// BenchDiff is the result showing performance differences
// for a single benchmark function.
type BenchDiff struct {
// Name of the benchmark function
Name string
// Old is the performance summary of the old benchmark.
Old string
// New is the performance summary of the new benchmark.
New string
// Delta between the old and new performance summaries.
Delta float64
}
// Checker performs checks on StatResult.
type Checker struct {
metric string
threshold float64
repr string
}
// CmdError represents an error running a specific command.
type CmdError struct {
Cmd *exec.Cmd
Err error
Output string
}
// Error returns the string representation of the error.
func (c *CmdError) Error() string {
return fmt.Sprintf(
"fail to run: %v on dir %s: %v",
c.Cmd,
c.Cmd.Dir,
c.Err,
)
}
// String returns the string representation of the checker.
func (c Checker) String() string {
return c.repr
}
// Do performs the check on the given StatResult. Returns true
// if it passed the check, false otherwise.
func (c Checker) Do(stat StatResult) bool {
if c.metric != stat.Metric {
return true
}
for _, bench := range stat.BenchDiffs {
if c.threshold >= 0.0 && bench.Delta > c.threshold {
return false
}
if c.threshold < 0.0 && bench.Delta < c.threshold {
return false
}
}
return true
}
// Path is the absolute path of the module on the filesystem.
func (m Module) Path() string {
return m.path
}
// String provides the string representation of the module.
func (m Module) String() string {
return fmt.Sprintf("go module at %q", m.path)
}
// String provides the string representation of a bench result
func (b BenchDiff) String() string {
return fmt.Sprintf(
"%s: old %s: new %s: delta: %.2f%%",
b.Name, b.Old, b.New, b.Delta,
)
}
// Add will add a new bench result. If the string doesn't represent
// a benchmark result it will be ignored.
func (b *BenchResults) Add(res string) {
if !strings.HasPrefix(res, "Benchmark") {
return
}
*b = append(*b, res)
}
// GetModule will download a specific version of a module and
// return a directory where you can find the module code.
// It uses "go get" to do the job, so the returned directory
// should be considered read only (it is part of the Go cache).
// The returned path is an absolute path.
//
// Any errors running "go" can be inspected in detail by
// checking if the returned error is a *CmdError.
func GetModule(name string, version string) (Module, error) {
// Reference: https://golang.org/ref/mod#go-mod-download
cmd := exec.Command("go", "mod", "download", "-json", fmt.Sprintf("%s@%s", name, version))
output, err := cmd.CombinedOutput()
if err != nil {
return Module{}, &CmdError{
Cmd: cmd,
Err: err,
Output: string(output),
}
}
parsedResult := struct {
Dir string // absolute path to cached source root directory
}{}
err = json.Unmarshal(output, &parsedResult)
if err != nil {
return Module{}, fmt.Errorf("error parsing %q : %v", string(output), err)
}
return Module{path: parsedResult.Dir}, nil
}
// RunBench will run all benchmarks present at the given module
// return the benchmark results.
//
// This function relies on running the "go" command to run benchmarks.
//
// Any errors running "go" can be inspected in detail by
// checking if the returned is a *CmdError.
func RunBench(mod Module) (BenchResults, error) {
cmd := exec.Command("go", "test", "-bench=.", "./...")
cmd.Dir = mod.Path()
out, err := cmd.CombinedOutput()
if err != nil {
return nil, &CmdError{
Cmd: cmd,
Err: err,
Output: string(out),
}
}
benchOut := strings.Split(string(out), "\n")
results := BenchResults{}
for _, b := range benchOut {
results.Add(b)
}
return results, nil
}
// Stat compares two benchmark results providing a set of stats results.
func Stat(oldres BenchResults, newres BenchResults) ([]StatResult, error) {
// We are using benchstat defaults:
// - https://cs.opensource.google/go/x/perf/+/master:cmd/benchstat/main.go;l=117
const (
alpha = 0.05
geomean = false
)
c := &benchstat.Collection{
Alpha: alpha,
AddGeoMean: geomean,
DeltaTest: benchstat.UTest,
}
if err := c.AddFile("old", resultsReader(oldres)); err != nil {
return nil, fmt.Errorf("parsing old results: %v", err)
}
if err := c.AddFile("new", resultsReader(newres)); err != nil {
return nil, fmt.Errorf("parsing new results: %v", err)
}
return newStatResults(c.Tables()), nil
}
// StatModule will:
//
// - Download the specific versions of the given module.
// - Run benchmarks on each of them.
// - Compare old vs new version benchmarks and return a stat results.
//
// This function relies on running the "go" command to run benchmarks.
//
// Any errors running "go" can be inspected in detail by
// checking if the returned error is a CmdError.
func StatModule(name string, oldversion, newversion string) ([]StatResult, error) {
oldresults, err := benchModule(name, oldversion)
if err != nil {
return nil, fmt.Errorf("running bench for old module: %v", err)
}
newresults, err := benchModule(name, newversion)
if err != nil {
return nil, fmt.Errorf("running bench for new module: %v", err)
}
return Stat(oldresults, newresults)
}
// ParseChecker will parse the given string into a Check.
func ParseChecker(val string) (Checker, error) {
parsed := strings.Split(val, "=")
metric := parsed[0]
if len(parsed) != 2 {
return Checker{}, fmt.Errorf("checker on wrong format, expect: %q", CheckerFmt)
}
threshold, err := strconv.ParseFloat(strings.TrimSuffix(parsed[1], "%"), 64)
if err != nil {
return Checker{}, fmt.Errorf("parsing checker %q threshold", err)
}
return Checker{
repr: val,
metric: metric,
threshold: threshold,
}, nil
}
func newStatResults(tables []*benchstat.Table) []StatResult {
res := make([]StatResult, len(tables))
for i, table := range tables {
res[i] = StatResult{
Metric: table.Metric,
BenchDiffs: newBenchResults(table.Rows),
}
}
return res
}
func newBenchResults(rows []*benchstat.Row) []BenchDiff {
res := make([]BenchDiff, len(rows))
for i, row := range rows {
if len(row.Metrics) != 2 {
// Since we always compare 2 sets of benchmarks there should be
// 2 metrics per row always.
panic(fmt.Errorf("should always have 2 metrics, instead got: %d", len(row.Metrics)))
}
res[i] = BenchDiff{
Name: row.Benchmark,
Old: row.Metrics[0].Format(row.Scaler),
New: row.Metrics[1].Format(row.Scaler),
Delta: row.PctDelta,
}
}
return res
}
func resultsReader(res BenchResults) io.Reader {
return strings.NewReader(strings.Join(res, "\n"))
}
func benchModule(name string, version string) (BenchResults, error) {
mod, err := GetModule(name, version)
if err != nil {
return nil, err
}
// benchstat requires multiple runs of the same benchmarks
// so it can assess statistically for abnormalities, etc.
const benchruns = 5
results := BenchResults{}
for i := 0; i < benchruns; i++ {
res, err := RunBench(mod)
if err != nil {
return nil, err
}
results = append(results, res...)
}
return results, nil
}