Skip to content

Instantly share code, notes, and snippets.

@danielwhite
Created April 28, 2017 00:45
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danielwhite/f7400845fd971a407e3ae83a6398650c to your computer and use it in GitHub Desktop.
Save danielwhite/f7400845fd971a407e3ae83a6398650c to your computer and use it in GitHub Desktop.
Benchmarking Golang with benchstat
#!/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}
$ 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