duanning9110 2016-06-26 12:58
浏览 46
已采纳

Golang基准测试能否提供自定义输出?

I'm benchmarking code with different list sizes (lists of size S) Go benchmark shows ns/op but what I want is (ns/op)/S.

In other words, the output of go test -bench=. is:

BenchmarkMy10-4         100000000           15.7 ns/op
BenchmarkMy20-4         50000000            33.8 ns/op
BenchmarkMy30-4         30000000            43.8 ns/op
BenchmarkMy40-4         30000000            49.3 ns/op
BenchmarkMy50-4         30000000            56.6 ns/op
BenchmarkMy1000-4        2000000           686 ns/op
BenchmarkMy10000-4        200000          6685 ns/op
BenchmarkMy100000-4        20000         65425 ns/op

The "10" in "My10" represents a list of 10 items (S=10).

While it is useful to know the ns/op for different list sizes, I would also like to know the ns/op/S (time per item in the list).

Right now I'm pasting the results into a spreadsheet and doing the math there. However I'd like to have "go test" output this information for me.

My main_test.go file looks like:

import "testing"

var result int

func benchmarkMy(i int, b *testing.B) {
  var r int
  mylist := MakeList(i)
  b.ResetTimer()
  for n := 0; n < b.N; n++ {
    r = My(mylist)
  }
  result = r
}

func BenchmarkMy10(b *testing.B)         { benchmarkMy(10, b) }
func BenchmarkMy20(b *testing.B)         { benchmarkMy(20, b) }
func BenchmarkMy30(b *testing.B)         { benchmarkMy(30, b) }
func BenchmarkMy40(b *testing.B)         { benchmarkMy(40, b) }
func BenchmarkMy50(b *testing.B)         { benchmarkMy(50, b) }
func BenchmarkMy1000(b *testing.B)       { benchmarkMy(1000, b) }
func BenchmarkMy10000(b *testing.B)      { benchmarkMy(10000, b) }
func BenchmarkMy100000(b *testing.B)     { benchmarkMy(100000, b) }

It seems like the test.BenchmarkResult structure has the information I need, but I don't see how to use this structure.

  • 写回答

1条回答 默认 最新

  • douwulu2576 2016-06-26 14:26
    关注

    You can write a custom benchmark using Benchmark function from package testing. And get a BenchmarkResult instance you mention.

    package main
    
    import (
        "fmt"
        "testing"
    )
    
    func benchmarkMy(i int) {
        fn := func(b *testing.B) {
             // Code you want benchmarked
        }
        r := testing.Benchmark(fn)
    
        fmt.Printf("%d ns/op
    ", int(r.T)/r.N)
        fmt.Printf("%d ns/op/i
    ", int(r.T)/r.N/i)
    }
    
    func main() {
        benchmarkMy(10)
    }
    

    You'll have to put this in a different package and run with go run instead of go test.

    Check an example in the playground.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Tides 远程写代码 anaconda jupyter python3
  • ¥15 我的R语言提示去除连锁不平衡时clump_data报错,图片以下所示,卡了好几天了,苦恼不知道如何解决,有人帮我看看怎么解决吗?
  • ¥15 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?
  • ¥15 有偿四位数,节约算法和扫描算法
  • ¥15 VUE项目怎么运行,系统打不开
  • ¥50 pointpillars等目标检测算法怎么融合注意力机制
  • ¥20 Vs code Mac系统 PHP Debug调试环境配置