Created
April 28, 2017 00:45
-
-
Save danielwhite/f7400845fd971a407e3ae83a6398650c to your computer and use it in GitHub Desktop.
Benchmarking Golang with benchstat
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# | |
# Generate benchmark files in a Git repository. | |
# | |
# Usage: TIMES=1 go-bench [prefix] | |
# | |
# Number of times to run the benchmark; useful for benchstat. | |
TIMES=${TIMES:-1} | |
# Prefix of the output file; useful for placing in a subdirectory. | |
PREFIX=${1:-benchmark.} | |
# Assume we're in a git repository, so we can remember which commit | |
# was the result of a benchmark. | |
REF=$(git rev-parse --short HEAD) | |
# Generate a file with a timestamp so that we can easily sort the | |
# benchmarks during comparison. | |
FILE="${PREFIX}$(date '+%Y%m%d%H%M')-${REF}.txt" | |
function do_bench { | |
for i in $(seq 1 $TIMES); do | |
go test -run=NONE -bench=. -benchmem ./... | |
done | |
} | |
do_bench > ${FILE} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ go get golang.org/x/perf/cmd/benchstat | |
$ TIMES=5 go-bench out/ | |
... make changes to code ... | |
$ TIMES=5 go-bench out/ | |
$ find out -type f | tail -2 | xargs benchstat | |
name old time/op new time/op delta | |
Parse-4 124µs ± 2% 122µs ± 1% ~ (p=0.421 n=5+5) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment