如何只测试一个基准函数?

huangapple go评论79阅读模式
英文:

How to test only one benchmark function?

问题

在我的Go包中,有几个基准测试文件,比如map1_benchmark_test.gomap2_benchmark_test.go。在每个*_benchmark_test.go文件中,有多个基准测试函数,比如func BenchmarkMapTravel(b *testing.B)func BenchmarkMapGet(b *testing.B)

问题是,我如何只测试一个基准测试函数?

我尝试阅读一些手册,但在运行go help test时没有找到关于基准测试的内容。

英文:

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.

答案1

得分: 71

> 测试标志的描述
>
> -test.bench pattern
> 运行与正则表达式匹配的基准测试。
> 默认情况下,不运行任何基准测试。
>
> -test.run pattern
> 仅运行与正则表达式匹配的测试和示例。
> 为了方便起见,测试二进制文件的每个-test.X标志在go test本身中也可用作标志-X

获取帮助信息,

$ go help testflag

例如,

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

或者

go test -bench MapTravel
go test -bench MapGet

要绕过测试函数,请包含一个-test.run模式,该模式过滤掉每个单独的测试。例如,

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

或者

go test -bench MapTravel -run=^$
英文:

> 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=^$

答案2

得分: 4

没有任何标志可以提供,只能运行基准测试(或只运行一个基准测试)。与此相关的唯一标志是:

> -bench regexp
> 运行与正则表达式匹配的基准测试。
> 默认情况下,不运行任何基准测试。要运行所有基准测试,
> 使用“-bench .”或“-bench=.”。

> -run regexp
> 仅运行与正则表达式匹配的测试和示例。

所以,如果你只想运行一个基准测试,可以这样做:

go test -bench=nameOfYourBenchmark -run=^a

这将只运行以a开头的测试。因为每个测试应该被命名为Test<something>,所以没有测试可以运行。

要只运行基准测试:

go test -bench=. -run=^a
英文:

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

答案3

得分: 0

测试只有 TestFuncOne

$>> go test -run TestFuncOne

> stuff_to_test.go

TestFuncOne() {
}

TestFuncTwo() {
}
英文:

Test only TestFuncOne

$>> go test -run TestFuncOne

> stuff_to_test.go

TestFuncOne() {
}

TestFuncTwo() {
}

huangapple
  • 本文由 发表于 2013年4月23日 12:46:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/16161142.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定