Benchmark Testing Implementation in Go
14-day free trial
Try SeekerPro free for 14 days.
Full access while you try it · then $15.99/mo · cancel anytime
Start your 14-day free SeekerPro trial →30-day money-back guarantee · Cancel anytime
Not checking a site today? Turn this topic into a ready to paste AI prompt, free →
Key Takeaways
- ✓Benchmark Testing runs in O(n) time and O(n) space, in Go as in every other language.
- ✓The testing package is the entire harness: write func BenchmarkXxx(b *testing.B) in a _test.go file and run go test -bench=.
- ✓Assign the result to a package-level sink variable, or rely on b.Loop's guarantee that the call survives.
- ✓The Go trap: The sink you add to defeat dead-code elimination can change the thing you are measuring. Go decides stack versus heap by escape analysis, and storing a pointer into a package-level variable makes the value escape, so a struct that production allocates on the stack is heap-allocated inside your benchmark and drags the concurrent collector in with it.
- ✓Compiled ahead of time with no warm-up and no re-optimisation (profile-guided optimisation exists since 1.21 but only when you feed it a profile), so numbers are stable from the first iteration and drift comes from the collector or the scheduler.
What Benchmark Testing actually is
Benchmark Testing in Go: what you actually reach for
Go implementation
package bench
import "testing"
// Package-level sink: the compiler cannot prove a store here is dead.
var sink uint64
func sumSquares(xs []uint64) uint64 {
var s uint64
for _, x := range xs {
s += x * x
}
return s
}
func BenchmarkSumSquares(b *testing.B) {
data := make([]uint64, 10_000)
for i := range data {
data[i] = uint64(i)
}
b.ReportAllocs()
var acc uint64
// Go 1.24+: everything above is excluded from the timing and the call
// below is kept alive. Pre-1.24: for i := 0; i < b.N; i++ preceded by
// b.ResetTimer().
for b.Loop() {
acc += sumSquares(data)
}
sink = acc
}
// go test -bench=. -benchmem -count=10 > new.txt
// benchstat old.txt new.txt
// go test -bench=. -gcflags=-m # confirm nothing escaped that should not
Think Your Code Is Clean? Let NexusBro QA It in 20 Seconds.
Paste your code. Click QA. Get an instant expert-level audit with fixes.
QA My Code FreeThe Go-specific trap
Performance in Go, beyond the O(n)
Real-World Applications
Interview Tips & Practice
Frequently Asked Questions
Which Go type or library should I use for Benchmark Testing?
The testing package is the entire harness: write func BenchmarkXxx(b *testing.B) in a _test.go file and run go test -bench=. -benchmem. Since Go 1.24 the loop is written for b.Loop(), which keeps everything before it out of the timed region and stops the compiler optimising away the call inside it; the older form is for i := 0; i < b.N; i++ paired with b.ResetTimer() after setup. b.ReportAllocs, b.ReportMetric, b.RunParallel with pb.Next and b.StopTimer/StartTimer complete the surface. For comparing two runs honestly, golang.org/x/perf/cmd/benchstat consumes -count=10 output.
What is the most common Go mistake when implementing Benchmark Testing?
The sink you add to defeat dead-code elimination can change the thing you are measuring. Go decides stack versus heap by escape analysis, and storing a pointer into a package-level variable makes the value escape, so a struct that production allocates on the stack is heap-allocated inside your benchmark and drags the concurrent collector in with it. Verify with go test -gcflags=-m. The second trap is the inlining budget: the compiler refuses to inline a callee whose body exceeds its cost limit, and adding a fmt call or a closure to the loop body can push it over and move the result by more than the change you were testing.
How does Benchmark Testing actually perform in Go?
Compiled ahead of time with no warm-up and no re-optimisation (profile-guided optimisation exists since 1.21 but only when you feed it a profile), so numbers are stable from the first iteration and drift comes from the collector or the scheduler. The dominant constants are allocations: -benchmem prints B/op and allocs/op, and allocs/op is usually the more predictive of the two. Interface method calls are indirect and not inlined. The collector runs concurrently on other Ps, so an allocation-heavy benchmark steals CPU from itself and the theft does not appear in its own wall clock. Time complexity is O(n) and space is O(n).
Related Articles
Rather try before you buy?
Get the rest of the vault: every cheat sheet, guide and audit in SeekerPro, free for 14 days, then $15.99/mo. Cancel anytime. Daily usage limits apply.
Start my 14-day SeekerPro trialMost AI and dev tools have a student plan. See which ones and how to claim them on Noizz.
Is your site built to last?
Run a free QA audit and get your Site Health Score in seconds.
Check Your Site FreeNo signup required
Reader comments
No account needed. Share what worked, what did not, and what you would add.
Add your take below. A concrete tip, a result or a question helps the next reader most.