douzhongjian0752 2016-03-08 15: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 16: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条)

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵