benchstat, compare two runs

Statistical significance, not vibes.

6 minmedium

Run the same benchmark twice and you get two different numbers. Run it before and after a change and you get two different numbers, which may or may not be about the change. benchstat is the tool that tells the difference, and it is the one thing standing between performance work and a vibe.

Install it, then use counts

go install golang.org/x/perf/cmd/benchstat@latest
cd apps/api
go test -run XXX -bench BenchmarkListEnvelope -benchmem \
-count=10 ./internal/handlers/ > before.txt
# make the change, then
go test -run XXX -bench BenchmarkListEnvelope -benchmem \
-count=10 ./internal/handlers/ > after.txt
benchstat before.txt after.txt

-count=10 is the point of the exercise. One run gives benchstat nothing to work with: with a single sample there is no spread, so there is no way to say whether a difference is larger than the noise. Ten is the usual minimum, and more for a benchmark that is naturally variable.

Reading the output

│ before.txt │ after.txt │
│ sec/op │ sec/op vs base │
ListEnvelope-14 14.79µ ± 3% 9.12µ ± 2% -38.34% (p=0.000 n=10)
│ before.txt │ after.txt │
│ B/op │ B/op vs base │
ListEnvelope-14 11.39Ki ± 0% 6.21Ki ± 0% -45.48% (p=0.000 n=10)
│ before.txt │ after.txt │
│ allocs/op │ allocs/op vs base │
ListEnvelope-14 36.00 ± 0% 22.00 ± 0% -38.89% (p=0.000 n=10)
  • ± 3% is the spread across the ten runs. A large spread means the measurement is unstable, and the first thing to fix is the measurement.
  • p=0.000 is the probability of seeing this difference if the change did nothing. Below 0.05 it is reported; above it, benchstat prints ~.
  • n=10 is how many samples survived. If benchstat discarded some as outliers it says so, and that is worth reading.

A ~ in the vs base column means "no difference was detected". It does not mean the change is free and it does not mean it helped by a small amount. It means this experiment cannot tell, and the honest responses are more samples or a revert, not a sentence in the pull request saying it is slightly faster.

Making the machine quiet enough

A ± of 15% makes any change under 15% unmeasurable, so it is worth spending a minute on:

# Close the other work: browsers, dev servers, Docker, the other build.
# Then pin the parallelism so it does not vary with what else is running.
go test -run XXX -bench . -benchmem -count=10 -cpu=4 ./internal/handlers/
# A laptop on battery clocks down mid-run. Plug it in.

Two benchmarks in the same package also interfere: one leaves the caches warm and the allocator in a different state for the next. Use -bench with an exact name when the number matters.

Why CI does not do this

Grit's scaffolded ci.yml runs the benchmarks with -benchtime 1x and asserts nothing about their timing, with a comment saying why: timing a shared CI runner tells you about the runner. What CI asserts instead is the allocation budgets, which do not move with the machine.

Timing comparisons belong on a machine you control, run deliberately, with the result written down.

Try it

One. Run a benchmark with -count=10 twice, changing nothing, into a.txt and b.txt. Run benchstat a.txt b.txt.

Two. Note the ± figure and whether benchstat reports a difference between two identical versions.

Three. Repeat with a browser and a dev server running. Compare the ± figures.

Quick check

benchstat reports -4.2% with p=0.21 and n=10. What do you conclude?

Spot a typo? Have an idea?

Help us improve this lesson. One click opens a GitHub issue with the lesson URL pre-filled, suggest clearer wording, report a bug, or request more depth. The course keeps improving thanks to learners like you.

Suggest an improvement on GitHub