golang banchmarks with benchstat

Here I will show how to run a benchmark in go1.24 and compare 2 implementations.

Imagine we have 2 files with 2 implementations we want to compare.

bu_hash_test.go (old implementation)

package main

import (
	"hash/fnv"
	"testing"
)

func BenchmarkUserHash(b *testing.B) {
	str := "user_id=72001002582"
	for b.Loop() {
		h := fnv.New64a()
		_, _ = h.Write([]byte(str))
		_ = h.Sum64()
	}
}

func BenchmarkVisitorHash(b *testing.B) {
	str := "visitor_id=12345678-ABCD-1234-ABCD-1234567890AB"
	for b.Loop() {
		h := fnv.New64a()
		_, _ = h.Write([]byte(str))
		_ = h.Sum64()
	}
}

And

bu_fast_hash_test.go (new impl)

package main

import (
	"testing"

	"github.com/segmentio/fasthash/fnv1"
)

func BenchmarkUserHash(b *testing.B) {
	str := "user_id=72001002582"
	for b.Loop() {
		_ = fnv1.HashString64(str)
	}
}

func BenchmarkVisitorHash(b *testing.B) {
	str := "visitor_id=12345678-ABCD-1234-ABCD-1234567890AB"
	for b.Loop() {
		_ = fnv1.HashString64(str)
	}
}

First, we run
go test --count=10 -bench=./bu_hash_test.go -benchmem -test.v -test.paniconexit0 -test.bench . | tee bu_test_res_hash.txt.
Then do the same for the second file:
go test --count=10 -bench=./bu_fast_hash_test.go -benchmem -test.v -test.paniconexit0 -test.bench . | tee bu_test_res_fast_hash.txt

Then we compare results with benchstat: benchstat bu_test_res_hash.txt bu_test_res_fast_hash.txt. (If it’s not installed, you would want to run go install golang.org/x/perf/cmd/benchstat@latest).

As a result, we’ll see something like this:

cpu: Apple M2 Pro
               │ bu_test_res_hash.txt │      bu_test_res_fast_hash.txt      │
               │        sec/op        │   sec/op     vs base                │
UserHash-12              33.940n ± 1%   6.544n ± 3%  -80.72% (p=0.000 n=10)
VisitorHash-12            69.06n ± 1%   23.80n ± 1%  -65.54% (p=0.000 n=10)
geomean                   48.41n        12.48n       -74.23%

               │ bu_test_res_hash.txt │       bu_test_res_fast_hash.txt        │
               │         B/op         │   B/op     vs base                     │
UserHash-12                32.00 ± 0%   0.00 ± 0%  -100.00% (p=0.000 n=10)
VisitorHash-12             56.00 ± 0%   0.00 ± 0%  -100.00% (p=0.000 n=10)
geomean                    42.33                   ?                       ¹ ²
¹ summaries must be >0 to compute geomean
² ratios must be >0 to compute geomean

LEAVE A COMMENT