skip to content
Alvin Lucillo

Repeated benchmarks

/ 2 min read

Running a benchmark once is limited as benchmarks might be affected the factors we can’t control such as the opreating system processes, resulting to a single benchmark result that could be at the extreme end. To give us more perspective, we can specify to run it multiple times with -count. In the example below, we can see that there are multiple iterations of the benchmark, and benchmark results vary.

func BenchmarkEncodeJSON(b *testing.B) {
	var data []byte
	var err error
	b.ReportAllocs()
	b.SetBytes(int64(len(benchmarkEvent.Message)))

	for b.Loop() {
		data, err = EncodeJSON(benchmarkEvent)
		if err != nil {
			b.Fatal(err)
		}
	}
	sinkBytes = data
}

func BenchmarkEncodeBinary(b *testing.B) {
	var data []byte
	b.ReportAllocs()
	b.SetBytes(int64(len(benchmarkEvent.Message)))

	for b.Loop() {
		data = EncodeBinary(benchmarkEvent)
	}
	sinkBytes = data
}
go test -run '^$' -bench '^BenchmarkEncode' -count=10
goos: linux
goarch: arm64
pkg: codec01
BenchmarkEncodeJSON-12           7960141               158.3 ns/op       309.61 MB/s         104 B/op          2 allocs/op
BenchmarkEncodeJSON-12           7898463               151.2 ns/op       324.14 MB/s         104 B/op          2 allocs/op
BenchmarkEncodeJSON-12           7718530               151.3 ns/op       323.91 MB/s         104 B/op          2 allocs/op
BenchmarkEncodeJSON-12           7976576               152.1 ns/op       322.12 MB/s         104 B/op          2 allocs/op
BenchmarkEncodeJSON-12           7606076               150.3 ns/op       325.93 MB/s         104 B/op          2 allocs/op
BenchmarkEncodeJSON-12           7603059               157.2 ns/op       311.67 MB/s         104 B/op          2 allocs/op
BenchmarkEncodeJSON-12           8004039               149.5 ns/op       327.73 MB/s         104 B/op          2 allocs/op
BenchmarkEncodeJSON-12           7835644               150.4 ns/op       325.83 MB/s         104 B/op          2 allocs/op
BenchmarkEncodeJSON-12           7851830               150.4 ns/op       325.81 MB/s         104 B/op          2 allocs/op
BenchmarkEncodeJSON-12           8059700               149.7 ns/op       327.27 MB/s         104 B/op          2 allocs/op
BenchmarkEncodeBinary-12        41783167                30.53 ns/op     1604.76 MB/s          64 B/op          1 allocs/op
BenchmarkEncodeBinary-12        38407419                29.41 ns/op     1666.13 MB/s          64 B/op          1 allocs/op
BenchmarkEncodeBinary-12        42239902                30.93 ns/op     1584.03 MB/s          64 B/op          1 allocs/op
BenchmarkEncodeBinary-12        41217201                30.77 ns/op     1592.30 MB/s          64 B/op          1 allocs/op
BenchmarkEncodeBinary-12        38959696                30.43 ns/op     1610.25 MB/s          64 B/op          1 allocs/op
BenchmarkEncodeBinary-12        39073821                30.61 ns/op     1600.66 MB/s          64 B/op          1 allocs/op
BenchmarkEncodeBinary-12        35632840                30.70 ns/op     1595.89 MB/s          64 B/op          1 allocs/op
BenchmarkEncodeBinary-12        40272526                31.02 ns/op     1579.83 MB/s          64 B/op          1 allocs/op
BenchmarkEncodeBinary-12        38172176                31.19 ns/op     1570.96 MB/s          64 B/op          1 allocs/op
BenchmarkEncodeBinary-12        38789005                31.17 ns/op     1571.90 MB/s          64 B/op          1 allocs/op
PASS
ok      codec01 24.065s