gobenchmarking

Can golang benchmark give a custom output?


I'm benchmarking code with different list sizes (lists of size S) Go benchmark shows ns/op but what I want is (ns/op)/S.

In other words, the output of go test -bench=. is:

BenchmarkMy10-4         100000000           15.7 ns/op
BenchmarkMy20-4         50000000            33.8 ns/op
BenchmarkMy30-4         30000000            43.8 ns/op
BenchmarkMy40-4         30000000            49.3 ns/op
BenchmarkMy50-4         30000000            56.6 ns/op
BenchmarkMy1000-4        2000000           686 ns/op
BenchmarkMy10000-4        200000          6685 ns/op
BenchmarkMy100000-4        20000         65425 ns/op

The "10" in "My10" represents a list of 10 items (S=10).

While it is useful to know the ns/op for different list sizes, I would also like to know the ns/op/S (time per item in the list).

Right now I'm pasting the results into a spreadsheet and doing the math there. However I'd like to have "go test" output this information for me.

My main_test.go file looks like:

import "testing"

var result int

func benchmarkMy(i int, b *testing.B) {
  var r int
  mylist := MakeList(i)
  b.ResetTimer()
  for n := 0; n < b.N; n++ {
    r = My(mylist)
  }
  result = r
}

func BenchmarkMy10(b *testing.B)         { benchmarkMy(10, b) }
func BenchmarkMy20(b *testing.B)         { benchmarkMy(20, b) }
func BenchmarkMy30(b *testing.B)         { benchmarkMy(30, b) }
func BenchmarkMy40(b *testing.B)         { benchmarkMy(40, b) }
func BenchmarkMy50(b *testing.B)         { benchmarkMy(50, b) }
func BenchmarkMy1000(b *testing.B)       { benchmarkMy(1000, b) }
func BenchmarkMy10000(b *testing.B)      { benchmarkMy(10000, b) }
func BenchmarkMy100000(b *testing.B)     { benchmarkMy(100000, b) }

It seems like the test.BenchmarkResult structure has the information I need, but I don't see how to use this structure.


Solution

  • You can write a custom benchmark using Benchmark function from package testing. And get a BenchmarkResult instance you mention.

    package main
    
    import (
        "fmt"
        "testing"
    )
    
    func benchmarkMy(i int) {
        fn := func(b *testing.B) {
             // Code you want benchmarked
        }
        r := testing.Benchmark(fn)
    
        fmt.Printf("%d ns/op\n", int(r.T)/r.N)
        fmt.Printf("%d ns/op/i\n", int(r.T)/r.N/i)
    }
    
    func main() {
        benchmarkMy(10)
    }
    

    You'll have to put this in a different package and run with go run instead of go test.

    Check an example in the playground.