这里对Golang来说很新,并且在使用goroutines进行基准测试时遇到了问题。
我的代码在这里:
type store struct{}
func (n *store) WriteSpan(span interface{}) error {
return nil
}
func smallTest(times int, b *testing.B) {
writer := store{}
var wg sync.WaitGroup
numGoroutines := times
wg.Add(numGoroutines)
b.ResetTimer()
b.ReportAllocs()
for n := 0; n < numGoroutines; n++ {
go func() {
writer.WriteSpan(nil)
wg.Done()
}()
}
wg.Wait()
}
func BenchmarkTest1(b *testing.B) {
smallTest(1000000, b)
}
func BenchmarkTest2(b *testing.B) {
smallTest(10000000, b)
}
它看起来两个场景的运行时和分配应该是相似的,但运行它们会给我以下结果,这些结果有很大的不同。不知道为什么会这样?那些额外的分配来自哪里?
BenchmarkTest1-12 1000000000 0.26 ns / on 0 B / on 0 allocs / op
BenchmarkTest2-12 1 2868129398 ns / at 31872 B / at 83 allocs / op
通过
我还注意到如果我多次向writeSpan
添加一个内部循环,运行时和分配类型与numGoroutines * multiple times
有关。如果这不是人们使用goroutine进行基准测试的方式,那么还有其他标准方法可以测试吗?提前致谢。
无意义的微基准测试产生无意义的结果。
如果这不是人们使用goroutine进行基准测试的方式,那么还有其他标准方法可以测试吗?
这不是对任何东西进行基准测试的方法。基准真实问题。
在运行调度程序,计算机和其他资源之前,您运行了大量的goroutine,它们什么都不做。这仅仅证明,如果你运行足够多次,你可以将机器踩到膝盖。