douzhongjian0752 2016-03-08 07:57
浏览 27
已采纳

Golang配置文件-top10仅显示100%的一行

I try to profiling my go library, to find out what is the cause of being so much slower than same thing in c++.

I have simple benchmark

func BenchmarkFile(t *testing.B) {
    tmpFile, err := ioutil.TempFile("", TMP_FILE_PREFIX)

    fw, err := NewFile(tmpFile.Name())
    text := []byte("testing")
    for i := 0; i < b.N; i++ {
        _, err = fw.Write(text)
    }
    fw.Close()
}

NewFile return my custom Writer which encodes data to our binary representation, even compress them, and write to file system.

Running go test -bench . -memprofile mem.out -cpuprofile cpu.out I get

PASS
BenchmarkFile-16    2000000000           0.20 ns/op
ok      .../writer/iowriter 9.074s

Than analysing it

# go tool pprof cpu.out 
Entering interactive mode (type "help" for commands)
(pprof) top10
930ms of 930ms total (  100%)
      flat  flat%   sum%        cum   cum%
     930ms   100%   100%      930ms   100%  
(pprof) 

I even try to write example.go app which is using my writer, and add pprof.StartCPUProfile(f) as is shown in http://blog.golang.org/profiling-go-programs but with same result.

What am I doing wrong, and how can I determine what is bottleneck of my lib? Thank you in advance

  • 写回答

2条回答 默认 最新

  • 普通网友 2016-03-08 08:09
    关注

    Ok it's easy, I miss to add binary to go tool pprof, si it has to be

    # go tool pprof write cpu.out 
    Entering interactive mode (type "help" for commands)
    (pprof) top10
    7.02s of 7.38s total (95.12%)
    Dropped 14 nodes (cum <= 0.04s)
    Showing top 10 nodes out of 32 (cum >= 0.19s)
          flat  flat%   sum%        cum   cum%
         6.55s 88.75% 88.75%      6.76s 91.60%  syscall.Syscall
        ...
    

    and when using benchmark tests, binary is created there and using it gives same result.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部