Go的基准测试是如何工作的?

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

Go Benchmark how does it work

问题

以下是翻译好的内容:

我已经让我的Go基准测试与我的API调用一起工作了,但是我不太确定下面的输出是什么意思:

$ go test intapi -bench=. -benchmem -cover -v -cpuprofile=cpu.out
=== RUN TestAuthenticate
--- PASS: TestAuthenticate (0.00 seconds)
PASS
BenchmarkAuthenticate      20000            105010 ns/op            3199 B/op         49 allocs/op
coverage: 0.0% of statements
ok      intapi  4.349s

它是如何知道要进行多少次调用的?我确实有一个循环,循环的大小是b.N,但是Golang是如何知道要运行多少次的?

另外,我现在有了CPU分析文件。我该如何使用它来查看呢?

英文:

I've got my Go benchmark working with my API calls but I'm not exactly sure what it means below:

$ go test intapi -bench=. -benchmem -cover -v -cpuprofile=cpu.out
=== RUN TestAuthenticate
--- PASS: TestAuthenticate (0.00 seconds)
PASS
BenchmarkAuthenticate      20000            105010 ns/op            3199 B/op         49 allocs/op
coverage: 0.0% of statements
ok      intapi  4.349s

How does it know how many calls it should make? I do have a loop with b.N as size of the loop but how does Golang know how many to run?

Also I now have cpu profile file. How can I use this to view it?

答案1

得分: 1

从TFM:

基准函数必须运行目标代码 b.N 次。基准包将会调整 b.N 的值,直到基准函数运行的时间足够长,可以可靠地计时。

英文:

From TFM:

> The benchmark function must run the target code b.N times. The benchmark package will vary b.N until the benchmark function lasts long enough to be timed reliably.

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

发表评论

匿名网友

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

确定