你如何使用go (golang) gocheck测试框架的基准标志?

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

How do you use the benchmark flags for the go (golang) gocheck testing framework?

问题

如何在gocheck测试框架中使用标志选项进行基准测试?在我提供的链接中,他们似乎只提供了一个示例,即运行go test -check.b,但是他们没有提供关于它如何工作的其他注释,所以很难使用它。当我运行go help test或者go help testflag时,甚至在go文档中都找不到-check。特别是我想知道如何更好地使用基准测试框架,并控制它运行的时间或迭代次数等等。例如,在他们提供的示例中:

func (s *MySuite) BenchmarkLogic(c *C) { 
    for i := 0; i < c.N; i++ { 
        // Logic to benchmark 
    } 
}

这里有一个变量c.N。如何指定这个变量?是通过实际的程序本身,还是通过go test及其标志或命令行?

另外,go help testflag中的文档确实提到了-bench regexbenchmembenchtime t选项,但没有提到-check.b选项。然而,我尝试按照文档中描述的运行这些选项,但似乎没有任何明显的效果。gocheck是否与go test的原始选项兼容?

我看到的主要问题是没有清晰的文档说明如何使用gocheck工具或其命令。我不小心给它传递了一个错误的标志,它给了我一个错误消息,建议我需要的有用命令(有限的描述):

  -check.b=false: 运行基准测试
  -check.btime=1s: 每个基准测试的大致运行时间
  -check.f="": 选择要运行的测试和/或套件的正则表达式
  -check.list=false: 列出将运行的所有测试的名称
  -check.v=false: 详细模式
  -check.vv=false: 超详细模式(禁用输出缓存)
  -check.work=false: 显示并且不删除测试工作目录
  -gocheck.b=false: 运行基准测试
  -gocheck.btime=1s: 每个基准测试的大致运行时间
  -gocheck.f="": 选择要运行的测试和/或套件的正则表达式
  -gocheck.list=false: 列出将运行的所有测试的名称
  -gocheck.v=false: 详细模式
  -gocheck.vv=false: 超详细模式(禁用输出缓存)
  -gocheck.work=false: 显示并且不删除测试工作目录
  -test.bench="": 选择要运行的基准测试的正则表达式
  -test.benchmem=false: 打印基准测试的内存分配情况
  -test.benchtime=1s: 每个基准测试的大致运行时间
  -test.blockprofile="": 在执行后将goroutine阻塞分析写入指定的文件
  -test.blockprofilerate=1: 如果>= 0,则调用runtime.SetBlockProfileRate()
  -test.coverprofile="": 在执行后将覆盖率分析写入指定的文件
  -test.cpu="": 用于每个测试的CPU数量的逗号分隔列表
  -test.cpuprofile="": 在执行期间将CPU分析写入指定的文件
  -test.memprofile="": 在执行后将内存分析写入指定的文件
  -test.memprofilerate=0: 如果>=0,则设置runtime.MemProfileRate
  -test.outputdir="": 写入配置文件的目录
  -test.parallel=1: 最大测试并行度
  -test.run="": 选择要运行的测试和示例的正则表达式
  -test.short=false: 运行较小的测试套件以节省时间
  -test.timeout=0: 如果为正数,则为所有测试设置一个总时间限制
  -test.v=false: 详细模式:打印额外的输出

写入错误的命令是唯一的获取此工具帮助的方式吗?它没有帮助标志或其他方式吗?

英文:

How does one use the flag options for benchmarks with the gocheck testing framework? In the link that I provided it seems to be that the only example they provide is by running go test -check.b, however, they do not provide additional comments on how it works so its hard to use it. I could not even find the -check in the go documentation when I did go help test nor when I did go help testflag. In particular I want to know how to use the benchmark testing framework better and control how long it runs for or for how many iterations it runs for etc etc. For example in the example they provide:

func (s *MySuite) BenchmarkLogic(c *C) { 
    for i := 0; i &lt; c.N; i++ { 
        // Logic to benchmark 
    } 
}

There is the variable c.N. How does one specify that variable? Is it through the actual program itself or is it through go test and its flags or the command line?

On the side note, the documentation from go help testflag did talk about -bench regex, benchmem and benchtime t options, however, it does not talk about the -check.b option. However I did try to run these options as described there but it didn't really do anything I could notice. Does gocheck work with the original options for go test?

The main problem I see is that there is no clear documentation for how to use the gocheck tool or its commands. I accidentally gave it a wrong flag and it threw me a error message suggesting useful commands that I need (which limited description):

  -check.b=false: Run benchmarks
  -check.btime=1s: approximate run time for each benchmark
  -check.f=&quot;&quot;: Regular expression selecting which tests and/or suites to run
  -check.list=false: List the names of all tests that will be run
  -check.v=false: Verbose mode
  -check.vv=false: Super verbose mode (disables output caching)
  -check.work=false: Display and do not remove the test working directory
  -gocheck.b=false: Run benchmarks
  -gocheck.btime=1s: approximate run time for each benchmark
  -gocheck.f=&quot;&quot;: Regular expression selecting which tests and/or suites to run
  -gocheck.list=false: List the names of all tests that will be run
  -gocheck.v=false: Verbose mode
  -gocheck.vv=false: Super verbose mode (disables output caching)
  -gocheck.work=false: Display and do not remove the test working directory
  -test.bench=&quot;&quot;: regular expression to select benchmarks to run
  -test.benchmem=false: print memory allocations for benchmarks
  -test.benchtime=1s: approximate run time for each benchmark
  -test.blockprofile=&quot;&quot;: write a goroutine blocking profile to the named file after execution
  -test.blockprofilerate=1: if &gt;= 0, calls runtime.SetBlockProfileRate()
  -test.coverprofile=&quot;&quot;: write a coverage profile to the named file after execution
  -test.cpu=&quot;&quot;: comma-separated list of number of CPUs to use for each test
  -test.cpuprofile=&quot;&quot;: write a cpu profile to the named file during execution
  -test.memprofile=&quot;&quot;: write a memory profile to the named file after execution
  -test.memprofilerate=0: if &gt;=0, sets runtime.MemProfileRate
  -test.outputdir=&quot;&quot;: directory in which to write profiles
  -test.parallel=1: maximum test parallelism
  -test.run=&quot;&quot;: regular expression to select tests and examples to run
  -test.short=false: run smaller test suite to save time
  -test.timeout=0: if positive, sets an aggregate time limit for all tests
  -test.v=false: verbose: print additional output

is writing wrong commands the only way to get some help with this tool? it doesn't have a help flag or something?

答案1

得分: 11

我5年晚了,但是要指定运行多少次N次,请使用选项-benchtime Nx

示例:

go test -bench=. -benchtime 100x

> BenchmarkTest 100 ... ns/op

请在这里阅读更多关于所有Go测试标志的信息。

英文:

I'm 5 years late, but to specify how many N times to run. Use the option -benchtime Nx.

Example:

go test -bench=. -benchtime 100x

> BenchmarkTest 100 ... ns/op

Please read more about all go testing flags here.

答案2

得分: 3

请参考测试标志的描述

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

<code>-check.b</code> 的工作方式与 <code>-test.bench</code> 相同。

例如,运行所有基准测试:

go test -check.b=.

运行特定的基准测试:

go test -check.b=BenchmarkLogic

有关在Go中进行测试的更多信息,请参阅此处

英文:

see the Description_of_testing_flags:

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

<code>-check.b</code> works the same way as <code>-test.bench</code>.

E.g. to run all benchmarks:

go test -check.b=.

to run a specific benchmark:

go test -check.b=BenchmarkLogic

more information about testing in Go can be found here

huangapple
  • 本文由 发表于 2014年6月26日 10:35:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/24421535.html
匿名

发表评论

匿名网友

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

确定