In https://golang.org/pkg/testing/ described that we can use testing.B.RunParallel()
function to run benchmark in a parallel setting. I tried to write simple testing code:
func BenchmarkFunctionSome(b *testing.B) {
for i := 0; i < b.N; i++ {
SomeFunction()
}
}
and then i changed it to use RunParallel()
func BenchmarkFunctionSome(b *testing.B) {
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
SomeFunction()
}
})
}
And the one which used RunParallel()
is slower than the first benchmark.
Actually what is the meaning of parallel setting in benchmarking? Why it became slower if i used RunParallel()
?
The for loop in the first benchmark has all tests run sequentially, one at a time, and the performance is the time divided by iterations.
The RunParallel benchmark divides the iterations among available threads. The performance is calculated similarly, probably averaging each group. The point of this is that several (exact # based upon your GOMAXPROCS setting) test iterations are run concurrently. This is especially helpful in testing functions with shared resources and locking, which may run fine solo but introduce performance issues when run concurrently.
It depends on whats there inside
SomeFunction()
If its just an empty function or a simple calculation, serial benchmark will be faster. But if it's a heavy computation or IO,
RunParallel()
benchmark will be faster.