duanli0162 2015-10-21 06:20
浏览 40
已采纳

按功能名称进行基准测试

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条回答 默认 最新

  • dqy13020940 2015-10-21 06:35
    关注

    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).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#java#的问题:找一份能快速看完mooc视频的代码
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!