I think you are doing everything right. Doc of testing.Benchmark()
says:
Benchmark benchmarks a single function. Useful for creating custom benchmarks that do not use the "go test" command.
If f calls Run, the result will be an estimate of running all its subbenchmarks that don't call Run in sequence in a single benchmark.
Looking into the implementation (Go 1.7.4):
func Benchmark(f func(b *B)) BenchmarkResult {
b := &B{
common: common{
signal: make(chan bool),
w: discard{},
},
benchFunc: f,
benchTime: *benchTime,
}
if !b.run1() {
return BenchmarkResult{}
}
return b.run()
}
This line:
if !b.run1() {
return BenchmarkResult{}
}
b.run1()
is supposed to run your passed function once, and detect if it has sub-benchmarks. Yours has. It returns a bool
whether more runs are needed. Inside run1()
:
if b.hasSub || b.finished {
// ...
return true
}
It properly tells it has sub-benchmark, and Benchmark()
–with noble simplicity– just returns an empty BenchmarkResult
:
if !b.run1() {
return BenchmarkResult{}
}
I do believe that either this is a bug (or rather "incomplete" feature), or doc is incorrect. I suggest to file an issue here: https://github.com/golang/go/issues