Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to test only one benchmark function?

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.

like image 500
hardPass Avatar asked Apr 23 '13 04:04

hardPass


People also ask

How do you benchmark a function in go?

Running a benchmark in Go To run a benchmark in Go, we'll append the -bench flag to the go test command. The argument to -bench is a regular expression that specifies which benchmarks should be run, which is helpful when you want to run a subset of your benchmark functions.

What type of test is a benchmark?

Benchmark Testing is a set of standards, metrics, or a reference point, against which, the performance quality of a product or a service is assessed or evaluated.


Video Answer


1 Answers

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=^$ 
like image 144
peterSO Avatar answered Sep 26 '22 23:09

peterSO