Search code examples
databasegotestingbenchmarking

Golang benchmark setup database


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?


Solution

  • You can use sub-tests for this case, using b.Run

    func BenchmarkXxx(b *testing.B) {
        fmt.Println("Setup")
        setup() // this require a lot of time
        fmt.Println("Start Test")
    
        b.Run("mytest", func(b *testing.B) {
            b.ResetTimer()
            for i := 0; i < b.N; i++ {
                fmt.Println("Loop")
                testMyStuffs()
            }
        })
    
    }
    

    A subbenchmark is like any other benchmark. A benchmark that calls Run at least once will not be measured itself and will be called once with N=1.

    Thus BenchmarkXxx is called once, to do setup.