如何使用基准测试标志来进行gocheck测试框架的Go(Golang)基准测试?

13
如何使用gocheck测试框架的标志选项进行基准测试?在我提供的链接中,似乎唯一提供的示例是通过运行go test -check.b来完成的,但是,他们并没有提供有关其工作原理的其他注释,因此很难使用它。 当我执行go help testgo help testflag时,我甚至找不到-check在go文档中的说明。特别是我想了解如何更好地使用基准测试框架,并控制其运行时间或迭代次数等等。例如,在他们提供的示例中:
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: Run benchmarks
  -check.btime=1s: approximate run time for each benchmark
  -check.f="": 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="": 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="": 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="": write a goroutine blocking profile to the named file after execution
  -test.blockprofilerate=1: if >= 0, calls runtime.SetBlockProfileRate()
  -test.coverprofile="": write a coverage profile to the named file after execution
  -test.cpu="": comma-separated list of number of CPUs to use for each test
  -test.cpuprofile="": write a cpu profile to the named file during execution
  -test.memprofile="": write a memory profile to the named file after execution
  -test.memprofilerate=0: if >=0, sets runtime.MemProfileRate
  -test.outputdir="": directory in which to write profiles
  -test.parallel=1: maximum test parallelism
  -test.run="": 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

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

2个回答

12

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

示例:

-benchtime Nx选项可用于指定运行的次数。例如:

go test -bench=. -benchtime 100x

基准测试 100 ... 毫微秒/每操作

请在此处详细了解所有Go测试标志。


4
请查看测试标记说明
-bench regexp
    Run benchmarks matching the regular expression.
    By default, no benchmarks run. To run all benchmarks,
    use '-bench .' or '-bench=.'.

-check.b的工作方式与-test.bench相同。

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

go test -check.b=.

运行特定基准测试:

go test -check.b=BenchmarkLogic

更多关于在 Go 中进行测试的信息可以在这里找到


请将 go test -check.b=. 修改为 go test --check.bgo test --gocheck.b - hoenir
2
你没有回答这个问题:如何指定N。 - Arkady
这确实回答了“编写错误命令是获取此工具帮助的唯一方式吗?”这是我感兴趣的部分。 - Johan Walles

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接