我正在尝试对需要昂贵准备的操作进行基准测试,但我不使用 StopTimer() 和 StartTimer()。具体来说,我正在将第 n 个项目插入到排序列表中进行基准测试。
示例代码:
n := 100
// Run the process b.N times
for i := 0; i < b.N; i++ {
// Stop the timer for our expensive preparation work (inserting n-1 items)
b.StopTimer()
// ...
// Insert n-1 items
for j := 1; j < n; j++ {
m.InsertItem(o)
}
// ...
// Resume the timer
b.StartTimer()
// Insert the nth item
m.InsertItem(o)
}
Run Code Online (Sandbox Code Playgroud)
问题是 Go 的基准测试启发式是根据基准时间而不是总时间来限制 bN。它最终要求第 100 个订单插入的 5MM(5000000)次迭代,这需要比合理的更多时间(我想对第 10 百万个项目插入进行基准测试)。
有没有办法在 Go 的基准测试工具中为特定基准指定最大 bN?我自己在文档中没有找到任何内容。
was*_*mup 12
设置显式迭代次数(100 次):
go test -bench=. -benchtime=100x
Run Code Online (Sandbox Code Playgroud)
指定时间(10 秒):
go test -bench=. -benchtime=10s
Run Code Online (Sandbox Code Playgroud)
b.N在 Go Benchmarking 中限制迭代 ( ):
自Go 1.12 起:
该
-benchtime标志现在支持设置显式迭代计数,而不是值以“x”结尾的时间。例如,-benchtime=100x运行基准测试 100 次。