I have to write some benchmarks that require a specific database setup. Something like this:
func BenchmarkXxx(b *testing.B) {
fmt.Println("Setup")
dropRecords()
createDatabaseRecords() // this require a lot of time
fmt.Println("Start Test")
b.ResetTimer()
for i := 0; i < b.N; i++ {
fmt.Println("Loop")
TestMyStuffs()
}
}
Running this benchmark I can see in the console that the "Setup" and "Start Test" are printing many times, so the BenchmarkXxx function seems to be called many times. Is there a way to run a setup code (createDatabaseRecords in this example) only one time and only for a specific benchmark?
Is there any sort of "best practice" to do this?