forked from golang-design/bench
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
382 lines (344 loc) · 8.85 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
381
382
// Copyright 2020 The golang.design Initiative Authors.
// All rights reserved. Use of this source code is governed
// by a GNU GPLv3 license that can be found in the LICENSE file.
package main
import (
"bytes"
"flag"
"fmt"
"io"
"io/ioutil"
"log"
"os"
"os/exec"
"os/signal"
"strings"
"syscall"
"time"
"golang.design/x/bench/internal/lock"
"golang.design/x/bench/internal/stat"
"golang.design/x/bench/internal/term"
)
func usage() {
fmt.Fprintf(os.Stderr, `usage: bench [options]
options for daemon usage:
-daemon
run bench service
-list
print current and pending commands
options for significant tests:
-delta-test test
significance test to apply to delta: utest, ttest, or none (default "utest")
-alpha α
consider change significant if p < α (default 0.05)
-geomean
print the geometric mean of each file (default false)
-split labels
split benchmarks by labels (default "pkg,goos,goarch")
-sort order
sort by order: [-]delta, [-]name, none (default "none")
options for running benchmarks:
-v go test
the -v flag from go test, (default false)
-name go test
the -bench flag from go test (default .) (default ".")
-count go test
the -count flag from go test (default 10) (default 10)
-time go test
the -benchtime flag from go test (default unset)
-cpuprocs go test
the -cpu flag to go test (default unset)
options for performance locking
-shared
acquire lock in shared mode (default exclusive mode)
-cpufreq percent
set CPU frequency to percent between the min and max (default 90)
while running command, or "none" for no adjustment
`)
os.Exit(2)
}
var (
flagDaemon *bool
flagList *bool
flagDeltaTest *string
flagAlpha *float64
flagGeomean *bool
flagSplit *string
flagSort *string
flagShared *bool
flagCPUFreq *lock.CpufreqFlag
flagVerbose *bool
flagName *string
flagCount *int
flagTime *string
flagCPUProcs *string
)
func main() {
log.SetPrefix("bench: ")
log.SetFlags(0)
flag.Usage = usage
// daemon args
flagDaemon = flag.Bool("daemon", false, "run bench service")
flagList = flag.Bool("list", false, "print current and pending commands")
// benchstat args
flagDeltaTest = flag.String("delta-test", "utest", "significance `test` to apply to delta: utest, ttest, or none")
flagAlpha = flag.Float64("alpha", 0.05, "consider change significant if p < `α`")
flagGeomean = flag.Bool("geomean", false, "print the geometric mean of each file")
flagSplit = flag.String("split", "pkg,goos,goarch", "split benchmarks by `labels`")
flagSort = flag.String("sort", "none", "sort by `order`: [-]delta, [-]name, none")
// perflock flags
flagShared = flag.Bool("shared", false, "acquire lock in shared mode (default exclusive mode)")
flagCPUFreq = &lock.CpufreqFlag{Percent: 90}
flag.Var(flagCPUFreq, "cpufreq", "set CPU frequency to `percent` between the min and max\n\twhile running command, or \"none\" for no adjustment")
// go test args
flagVerbose = flag.Bool("v", false, "the -v flag from `go test`, (default false)")
flagName = flag.String("name", ".", "the -bench flag from `go test` (default .)")
flagCount = flag.Int("count", 10, "the -count flag from `go test` (default 10)")
flagTime = flag.String("time", "", "the -benchtime flag from `go test` (default unset)")
flagCPUProcs = flag.String("cpuprocs", "", "the -cpu flag to `go test` (default unset)")
flag.Parse()
if *flagDaemon {
if flag.NArg() > 0 {
flag.Usage()
os.Exit(2)
}
lock.RunDaemon()
return
}
if *flagList {
if flag.NArg() > 0 {
flag.Usage()
os.Exit(2)
}
c := lock.NewClient()
if c == nil {
log.Fatal("Is the bench daemon running?")
}
list := c.List()
if len(list) == 0 {
log.Println("daemon is running but no running benchmarks.")
return
}
for _, l := range list {
log.Println(l)
}
return
}
if flag.NArg() > 0 {
runCompare()
return
}
// prepare go test command
args := []string{
"go",
"test",
"-run=^$",
}
if *flagVerbose {
args = append(args, "-v")
}
args = append(args, fmt.Sprintf("-bench=%s", *flagName))
if *flagCount <= 0 {
*flagCount = 10
}
args = append(args, fmt.Sprintf("-count=%d", *flagCount))
if *flagTime != "" {
args = append(args, fmt.Sprintf("-benchtime=%s", *flagTime))
}
if *flagCPUProcs != "" {
args = append(args, fmt.Sprintf("-cpu=%s", *flagCPUProcs))
}
// acquire lock
c := lock.NewClient()
if c == nil {
log.Printf(term.Red("run benchmarks without performance locking..."))
} else {
if !c.Acquire(*flagShared, true, strings.Join(args, " ")) {
list := c.List()
log.Printf("Waiting for lock...\n")
for _, l := range list {
log.Println(l)
}
c.Acquire(*flagShared, false, strings.Join(args, " "))
}
if !*flagShared && flagCPUFreq.Percent >= 0 {
c.SetCPUFreq(flagCPUFreq.Percent)
log.Print(term.Gray(fmt.Sprintf("run benchmarks under %d%% cpufreq...", flagCPUFreq.Percent)))
}
}
// Ignore SIGINT and SIGQUIT so they pass through to the
// child.
signal.Notify(make(chan os.Signal), os.Interrupt, syscall.SIGQUIT)
// run bench
runBench(args)
}
// shellEscape escapes a single shell token.
func shellEscape(x string) string {
if len(x) == 0 {
return "''"
}
for _, r := range x {
if 'a' <= r && r <= 'z' || 'A' <= r && r <= 'Z' || '0' <= r && r <= '9' || strings.ContainsRune("@%_-+:,./", r) {
continue
}
// Unsafe character.
return "'" + strings.Replace(x, "'", "'\"'\"'", -1) + "'"
}
return x
}
// shellEscapeList escapes a list of shell tokens.
func shellEscapeList(xs []string) string {
out := make([]string, len(xs))
for i, x := range xs {
out[i] = shellEscape(x)
}
return strings.Join(out, " ")
}
var deltaTestNames = map[string]stat.DeltaTest{
"none": stat.NoDeltaTest,
"u": stat.UTest,
"u-test": stat.UTest,
"utest": stat.UTest,
"t": stat.TTest,
"t-test": stat.TTest,
"ttest": stat.TTest,
}
func runCompare() {
c := &stat.Collection{
Alpha: *flagAlpha,
AddGeoMean: *flagGeomean,
DeltaTest: deltaTestNames[strings.ToLower(*flagDeltaTest)],
}
for _, file := range flag.Args() {
f, err := os.Open(file)
if err != nil {
log.Print(err)
flag.Usage()
os.Exit(2)
}
defer f.Close()
if err := c.AddFile(file, f); err != nil {
log.Fatal(err)
}
}
tables := c.Tables()
var buf bytes.Buffer
stat.FormatText(&buf, tables)
os.Stdout.Write(buf.Bytes())
}
func runBench(args []string) {
log.Print(strings.Join(args, " "))
cmd := exec.Command(args[0], args[1:]...)
stdout, err := cmd.StdoutPipe()
if err != nil {
log.Fatal(err)
}
stderr, err := cmd.StderrPipe()
if err != nil {
log.Fatal(err)
}
doneCh := make(chan []byte)
go func() {
data := []byte{}
buf := make([]byte, 1024)
for {
n, err := stdout.Read(buf)
if err != nil {
if err != io.EOF {
fmt.Fprintf(os.Stderr, "%v", err)
}
doneCh <- data
close(doneCh)
return
}
fmt.Fprintf(os.Stdout, string(buf[:n]))
data = append(data, buf[:n]...)
}
}()
errCh := make(chan error)
go func() {
buf := make([]byte, 1024)
for {
n, err := stderr.Read(buf)
if err != nil {
if err == io.EOF {
errCh <- nil
} else {
errCh <- err
}
close(errCh)
return
}
fmt.Fprintf(os.Stderr, string(buf[:n]))
}
}()
err = cmd.Start()
switch err := err.(type) {
case *exec.ExitError:
status := err.Sys().(syscall.WaitStatus)
if status.Exited() {
os.Exit(status.ExitStatus())
}
log.Fatal(err)
}
var results []byte
select {
case err = <-errCh:
if err != nil { // benchmark was interrupted or not success, exit.
os.Exit(2)
return
}
case results = <-doneCh:
}
// do nothing if no tests were ran.
if strings.Index(string(results), "no Go files") != -1 ||
strings.Index(string(results), "no test files") != -1 {
return
}
// Note that we should avoid using : in filename, because it is not
// supported on Windows file systems.
fname := "bench-" + time.Now().Format("2006-01-02-15-04-05") + ".txt"
err = ioutil.WriteFile(fname, results, 0644)
if err != nil {
// try again, maybe the user was too fast?
err = ioutil.WriteFile(fname, results, 0644)
if err != nil {
log.Fatal("cannot save benchmark result to your disk.")
}
}
log.Printf("results are saved to file: ./%s\n\n", fname)
computeStat(results)
}
var sortNames = map[string]stat.Order{
"none": nil,
"name": stat.ByName,
"delta": stat.ByDelta,
}
func computeStat(data []byte) {
sortName := *flagSort
reverse := false
if strings.HasPrefix(sortName, "-") {
reverse = true
sortName = sortName[1:]
}
order, _ := sortNames[sortName]
c := &stat.Collection{
Alpha: *flagAlpha,
AddGeoMean: *flagGeomean,
DeltaTest: deltaTestNames[strings.ToLower(*flagDeltaTest)],
}
if *flagSplit != "" {
c.SplitBy = strings.Split(*flagSplit, ",")
}
if order != nil {
if reverse {
order = stat.Reverse(order)
}
c.Order = order
}
c.AddData("", data)
tables := c.Tables()
var buf bytes.Buffer
stat.FormatText(&buf, tables)
fmt.Print(buf.String())
}