如何在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测试和它的标志或命令行?
在旁注上,go help testflag
的文档确实谈到了-bench regex
, benchmem
和benchtime 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
编写错误的命令是获得此工具帮助的唯一方法吗?没有帮助标志之类的吗?
我晚了5年,但是要指定运行多少N次。使用选项-benchtime Nx
的例子:
go test -bench=. -benchtime 100x
BenchmarkTest 100…ns/op
请在这里阅读更多关于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=.'.
-check.b
的工作原理与-test.bench
相同。
。运行所有基准测试:
go test -check.b=.
运行一个特定的基准测试:
go test -check.b=BenchmarkLogic
关于在Go中测试的更多信息可以在这里找到