Search code examples
gobenchmarking

How to use time value of benchmark


I have written a benchmark for my chess engine in Go:

func BenchmarkStartpos(b *testing.B) {
    board := ParseFen(startpos)
    for i := 0; i < b.N; i++ {
        Perft(&board, 5)
    }
}

I see this output when it runs:

goos: darwin
goarch: amd64
BenchmarkStartpos-4           10     108737398 ns/op
PASS
ok      _/Users/dylhunn/Documents/go-chess  1.215s

I want to use the time per execution (in this case, 108737398 ns/op) to compute another value, and also print it as a result of the benchmark. Specifically, I want to output nodes per second, which is given as the result of the Perft call divided by the time per call.

How can I access the time the benchmark took to execute, so I can print my own derived results?


Solution

  • You may use the testing.Benchmark() function to manually measure / benchmark "benchmark" functions (that have the signature of func(*testing.B)), and you get the result as a value of testing.BenchmarkResult, which is a struct with all the details you need:

    type BenchmarkResult struct {
        N         int           // The number of iterations.
        T         time.Duration // The total time taken.
        Bytes     int64         // Bytes processed in one iteration.
        MemAllocs uint64        // The total number of memory allocations.
        MemBytes  uint64        // The total number of bytes allocated.
    }
    

    The time per execution is returned by the BenchmarkResult.NsPerOp() method, you can do whatever you want to with that.

    See this simple example:

    func main() {
        res := testing.Benchmark(BenchmarkSleep)
        fmt.Println(res)
        fmt.Println("Ns per op:", res.NsPerOp())
        fmt.Println("Time per op:", time.Duration(res.NsPerOp()))
    }
    
    func BenchmarkSleep(b *testing.B) {
        for i := 0; i < b.N; i++ {
            time.Sleep(time.Millisecond * 12)
        }
    }
    

    Output is (try it on the Go Playground):

         100      12000000 ns/op
    Ns per op: 12000000
    Time per op: 12ms