What does the "-6" after the name of a Benchmark function mean?

Viewed 253

I have a go test file in which I wrote a benchmark function as follows:

func BenchmarkStuff(b *testing.B) {
    for i := 0; i < b.N; i++ {
        stuff()
    }
}

When I run this with go test -benchtime 1x, I get this:

BenchmarkStuff-6               1     847751900 ns/op

What does the -6 mean? It seems unnecessarily cryptic.

1 Answers

It's indicating the number of CPUs that were used to run the benchmark - so in your case 6.


EDIT:

The Benchmark name format does not appear to be formally documented on the Go website, but you can see in the standard library source how the name is formulated here and here:

runtime.GOMAXPROCS(procs)
benchName := benchmarkName(b.name, procs)

func benchmarkName(name string, n int) string {
    if n != 1 {
        return fmt.Sprintf("%s-%d", name, n)
    }
    return name
}

FYI: from the go help command-line docs:

go help testflag
    -cpu 1,2,4
        Specify a list of GOMAXPROCS values for which the tests or
        benchmarks should be executed. The default is the current value
        of GOMAXPROCS.

So if you wanted to coerce your benchmarking to use a lower number of CPUs use the env var GOMAXPROCS:

$ GOMAXPROCS=2  go test -bench=.

...

BenchmarkStuff-2    1000000000           0.2642 ns/op

Or you can benchmark multiple CPU core settings like so:

$ go test -bench=. -cpu=1,2,4

...

BenchmarkStuff      1000000000           0.2516 ns/op
BenchmarkStuff-2    1000000000           0.2531 ns/op
BenchmarkStuff-4    1000000000           0.2488 ns/op
Related