40

In my Go package there are several benchmark files like map1_benchmark_test.go and map2_benchmark_test.go. In every *_benchmark_test.go file, there is more than one benchmark function like func BenchmarkMapTravel(b *testing.B) and func BenchmarkMapGet(b *testing.B).

Question is, how can I test just one benchmark function?

I attempted to read some manuals, and got nothing about benchmark by running go help test.

peterSO
  • 158,998
  • 31
  • 281
  • 276
hardPass
  • 19,033
  • 19
  • 40
  • 42

3 Answers3

71

Description of testing flags

-test.bench pattern
    Run benchmarks matching the regular expression.
    By default, no benchmarks run.

-test.run pattern
    Run only those tests and examples matching the regular
    expression.

For convenience, each of these -test.X flags of the test binary is also available as the flag -X in 'go test' itself.

For help,

$ go help testflag

For example,

go test -test.bench MapTravel
go test -test.bench MapGet

or

go test -bench MapTravel
go test -bench MapGet

To bypass test functions, include a -test.run pattern that filters out every single test. For example,

go test -test.bench MapTravel -test.run=thisexpressionwontmatchanytest

or

go test -bench MapTravel -run=^$
Kevin Burke
  • 61,194
  • 76
  • 188
  • 305
peterSO
  • 158,998
  • 31
  • 281
  • 276
  • 5
    Only one benchmark runs, but other testing mehonds like `func TestMapGet(t *testing.T)` which are not benchmark run too. So, how to run a benchmark without any other test run? – hardPass Apr 23 '13 at 06:15
  • 6
    Note that you don't need the `test.` prefix in the flags, you can just write `go test -bench MapGet -run XXX` which is a bit less wordy to type. (From the docs: for convenience, each of these `-test.X` flags of the test binary is also available as the flag `-X` in `go test` itself.) – Nick Craig-Wood Apr 23 '13 at 06:55
  • On a native Windows command line, beware that `^` is an escape character (like \ elsewhere). Therefore it needs to be typed `-run=^^$` but easier is `-run==` (since the equals sign is not a regex special and not part of any test name). – Stein Aug 24 '16 at 17:48
  • 1
    Yes "-run=^$" is the key, if you didn't specify this, go still will run all of the tests. – iwind Apr 22 '22 at 10:28
5

There is no flag you can provide, that will run only benchmarks (or only one benchmark). The only flags related to these are:

-bench regexp Run benchmarks matching the regular expression. By default, no benchmarks run. To run all benchmarks, use '-bench .' or '-bench=.'.

-run regexp Run only those tests and examples matching the regular expression.

So if you want only to run one benchmark, you can do this:

go test -bench=nameOfYourBenchmark -run=^a

This will cause to run only tests that starts with a. And because each test should be named Test<something>, there will be no tests to run.

To run only benchmarks:

go test -bench=. -run=^a
Salvador Dali
  • 214,103
  • 147
  • 703
  • 753
0

Test only TestFuncOne

$>> go test -run TestFuncOne

stuff_to_test.go

TestFuncOne() {
}

TestFuncTwo() {
}
Rusty Rob
  • 16,489
  • 8
  • 100
  • 116