|
1 | 1 | # bench |
2 | | -refined performance measurement in Go |
| 2 | + |
| 3 | +Reliable performance measurement for Go programs. All in one design. |
| 4 | + |
| 5 | +``` |
| 6 | +$ go get golang.design/x/bench |
| 7 | +``` |
| 8 | + |
| 9 | +## Features |
| 10 | + |
| 11 | +- Combine [benchstat](https://pkg.go.dev/golang.org/x/perf/cmd/benchstat), [perflock](https://github.com/aclements/perflock) and more... |
| 12 | +- Short command and only run benchmarks |
| 13 | +- Automatic performance locking for benchmarks |
| 14 | +- Automatic statistic analysis for benchmark results |
| 15 | +- Color indications for benchmark results |
| 16 | + |
| 17 | +## Usage |
| 18 | + |
| 19 | +### Enable `bench` Daemon (optional, Linux only) |
| 20 | + |
| 21 | +```sh |
| 22 | +$ cd $GOPATH/src/golang.design/x/bench |
| 23 | +$ ./install.bash |
| 24 | +``` |
| 25 | + |
| 26 | +If your init system is supported, this will also configure `bench` to start automatically on boot. |
| 27 | + |
| 28 | +Or you can install and run `bench` daemon manually: |
| 29 | + |
| 30 | +```sh |
| 31 | +$ sudo install $GOPATH/bin/bench /usr/bin/bench |
| 32 | +$ sudo -b bench -daemon |
| 33 | +``` |
| 34 | + |
| 35 | +### Default Behavior |
| 36 | + |
| 37 | +```sh |
| 38 | +$ bench |
| 39 | +``` |
| 40 | + |
| 41 | +The detault behavior of `bench` run benchmarks under your current |
| 42 | +working directory, and each benchmark will be ran 10 times for further |
| 43 | +statistical analysis. It will also try to acquire performance lock from |
| 44 | +`bench` daemon to gain more stable results. Furthermore, the benchmark |
| 45 | +results are saved as a text file to the working directory and named as |
| 46 | +`<timestamp>.txt`. |
| 47 | + |
| 48 | +Example: |
| 49 | + |
| 50 | +``` |
| 51 | +$ cd example |
| 52 | +$ bench |
| 53 | +bench: run benchmarks under 90% cpufreq... |
| 54 | +bench: go test -run=^$ -bench=. -count=10 |
| 55 | +goos: linux |
| 56 | +goarch: amd64 |
| 57 | +pkg: golang.design/x/bench/example |
| 58 | +BenchmarkDemo-16 21114 57340 ns/op |
| 59 | +... |
| 60 | +BenchmarkDemo-16 21004 57097 ns/op |
| 61 | +PASS |
| 62 | +ok golang.design/x/bench/example 17.791s |
| 63 | +bench: results are saved to file: ./bench-2020-11-07-19:59:51.txt |
| 64 | +
|
| 65 | +name time/op |
| 66 | +Demo-16 57.0µs ±1% |
| 67 | +
|
| 68 | +$ # ... do some changes to the benchmark ... |
| 69 | +
|
| 70 | +$ bench |
| 71 | +bench: run benchmarks under 90% cpufreq... |
| 72 | +bench: go test -run=^$ -bench=. -count=10 |
| 73 | +goos: linux |
| 74 | +goarch: amd64 |
| 75 | +pkg: golang.design/x/bench/example |
| 76 | +BenchmarkDemo-16 213145 5625 ns/op |
| 77 | +... |
| 78 | +BenchmarkDemo-16 212959 5632 ns/op |
| 79 | +PASS |
| 80 | +ok golang.design/x/bench/example 12.536s |
| 81 | +bench: results are saved to file: ./bench-2020-11-07-20:00:16.txt |
| 82 | +
|
| 83 | +name time/op |
| 84 | +Demo-16 5.63µs ±0% |
| 85 | +
|
| 86 | +$ bench bench-2020-11-07-19:59:51.txt bench-2020-11-07-20:00:16.txt |
| 87 | +name old time/op new time/op delta |
| 88 | +Demo-16 57.0µs ±1% 5.6µs ±0% -90.13% (p=0.000 n=10+8) |
| 89 | +``` |
| 90 | + |
| 91 | +### Options |
| 92 | + |
| 93 | +Options for checking daemon status: |
| 94 | + |
| 95 | +```sh |
| 96 | +bench -list |
| 97 | +``` |
| 98 | + |
| 99 | +Options for statistic tests: |
| 100 | + |
| 101 | +```sh |
| 102 | +bench old.txt [new.txt] # same from benchstat |
| 103 | +bench -delta-test |
| 104 | +bench -alpha |
| 105 | +bench -geomean |
| 106 | +bench -split |
| 107 | +bench -sort |
| 108 | +``` |
| 109 | + |
| 110 | +Options for running benchmarks: |
| 111 | + |
| 112 | +```sh |
| 113 | +bench -v # enable verbose outputs |
| 114 | +bench -shared # enable shared execution |
| 115 | +bench -cpufreq 90 # cpu frequency (default: 90) |
| 116 | +bench -name BenchmarkXXX # go test `-bench` flag (default: .) |
| 117 | +bench -count 20 # go test `-count` flag (default: 10) |
| 118 | +bench -time 100x # go test `-benchtime` flag (default: unset) |
| 119 | +bench -cpuproc 1,2,4,8,16,32,128 # go test `-cpu` flag (default: unset) |
| 120 | +``` |
| 121 | + |
| 122 | +## License |
| 123 | + |
| 124 | +© 2020 The golang.design Authors |
0 commit comments