按函数名称进行Go基准测试

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

Go benchmark by function name

问题

我有这个基准测试函数:

BenchmarkMyTest(b *testing.B) {
}

我想只运行这个函数,而不运行其他所有测试,但是命令从来没有对我起作用过。

go test -bench='BenchmarkMyTest'
或者
go test -run='BenchmarkMyTest'

在Go中,运行单个基准测试函数的正确方法是什么?
它说要使用正则表达式,但我找不到任何文档。

谢谢,

英文:

I have this Benchmark function:

BenchmarkMyTest(b *testing.B) {
}

And I would like to run only this function not running all other tests, but the command never worked for me.

go test -bench='BenchmarkMyTest'
or
go test -run='BenchmarkMyTest'

What's the correct way of running one single benchmark function in Go?
It says to use regex but I can't find any documentation.

Thanks,

答案1

得分: 5

Command Go: Description of testing flags中描述了以下内容:

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

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

因此,语法是你必须用空格或等号(没有撇号)分隔它,你指定的是一个正则表达式:

go test -bench BenchmarkMyTest
go test -run TestMyTest

或者:

go test -bench=BenchmarkMyTest
go test -run=TestMyTest

指定一个函数

由于指定的表达式是一个正则表达式,这也将匹配函数名包含指定名称的函数(例如,另一个以此开头的函数,例如“BenchmarkMyTestB”)。如果你只想匹配“BenchmarkMyTest”,请在正则表达式后面添加单词边界'\b':

go test -bench BenchmarkMyTest\b
go test -run TestMyTest\b

请注意,只需要将其附加到末尾就足够了,因为如果函数名不以“Benchmark”开头,它不被视为基准测试函数,同样,如果函数名不以“Test”开头,它不被视为测试函数(也不会被捕获)。

英文:

Described at Command Go: Description of testing flags:

-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 the syntax is that you have to separate it with a space or with the equal sign (with no apostrophe marks), and what you specify is a regexp:

go test -bench BenchmarkMyTest
go test -run TestMyTest

Or:

go test -bench=BenchmarkMyTest
go test -run=TestMyTest

Specifying exactly 1 function

As the specified expression is a regexp, this will also match functions whose name contains the specified name (e.g. another function whose name starts with this, for example "BenchmarkMyTestB"). If you only want to match "BenchmarkMyTest", append the regexp word boundary '\b':

go test -bench BenchmarkMyTest\b
go test -run TestMyTest\b

Note that it's enough to append it only to the end as if the function name doesn't start with "Benchmark", it is not considered to be a benchmark function, and similarly if it doesn't start with "Test", it is not considered to be a test function (and will not be picked up anyway).

答案2

得分: 1

我发现那些答案不完整,所以这里有更多关于这个主题的内容...

以下命令运行以BenchmarkMyTest开头的所有基准测试(BenchmarkMyTest1,BenchmarkMyTest2等),并跳过所有带有-run=^$的测试。

您还可以使用-benchtime 5s指定测试持续时间,或者使用-benchmem强制执行b.ReportAllocs(),以获取类似以下的值:

BenchmarkLogsWithBytesBufferPool-48     46416456                26.91 ns/op            0 B/op          0 allocs/op

最终的命令将是:

go test -bench=^BenchmarkMyTest . -run=^$ . -v -benchtime 5s -benchmem
英文:

I found those answers incomplete, so here is more to the topic...

The following command runs all Benchmarks starting with BenchmarkMyTest (BenchmarkMyTest1, BenchmarkMyTest2, etc...) and also skip all tests with -run=^$ .

You can also specify a test duration with -benchtime 5s or you can force b.ReportAllocs() with -benchmem in order to get values like:

BenchmarkLogsWithBytesBufferPool-48     46416456                26.91 ns/op            0 B/op          0 allocs/op

the final command would be:

go test -bench=^BenchmarkMyTest . -run=^$ . -v -benchtime 5s -benchmem

huangapple
  • 本文由 发表于 2015年10月21日 14:20:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/33252269.html
匿名

发表评论

匿名网友

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

确定