Where to put setup/teardown in Go Benchmarks?

Viewed 1323

I have a go test file in which I wrote a benchmark function as follows:

func BenchmarkStuff(b *testing.B) {
    for i := 0; i < b.N; i++ {
        stuff()
    }
}

However the stuff() function requires some setup to occur every time before it runs and cleanup to occur every time after it runs. I have functions setup() and cleanup() that do this respectively. But I don't want to benchmark the setup and cleanup functions.

So where should they be called? If I call them inside BenchmarkStuff, they will be added to the results measurements. But without them, stuff() will fail.

1 Answers

The benchmarking package provides ResetTimer, StopTimer and StartTimer methods to avoid timing initialization needed for the benchmark.

If initialization is needed once, use ResetTimer before starting your loop:

func BenchmarkStuff(b *testing.B) {
    setup()
    b.ResetTimer()
    for i := 0; i < b.N; i++ {
        stuff()
    }
}

If you need to re-initialize during the loop, you can use StopTimer and StartTimer to avoid timing that portion:

func BenchmarkStuff(b *testing.B) {
    for i := 0; i < b.N; i++ {
        b.StopTimer()
        setup()
        b.StartTimer()
        stuff()
    }
}
Related