Benchmarks
Benchmarks assess the performance of your code by measuring the execution time of specific functions. The testing
package provides support for benchmarks, allowing developers to identify performance bottlenecks, identify if developers are achieving their project’s service-level indicators (SLIs) or service-level objectives (SLOs), and gain insights into their application.
Similar to how fuzz testing had a slightly different syntax, but followed our usual Go test setup expectations, benchmark tests also look different, but very similar to what we’re used to. For example, benchmark tests start with the word Benchmark and accept b *testing.B
. The testing runner executes each benchmark function several times, increasing the value of b.N
on each run.
Here’s a simple example benchmark function for our addition function:
func BenchmarkAdd(b *testing.B) { for i := 0; i < b.N; i++ { add(1, 2) } }
To...