doumu6997 2013-09-28 06:14
浏览 46
已采纳

有没有人在go可执行文件上使用简单的pprof?

I have looked at the article about profiling go programs, and I simple do not understand it. Do someone have a simple code example were the performance of code snippet is logged in text file by a profile-"object"?

  • 写回答

1条回答 默认 最新

  • doucan9079 2014-05-01 16:11
    关注

    Here are the commands I use for a simple CPU and memory profiling to get you started.

    Let's say you made a benchmark function like this :

    File something_test.go :

    func BenchmarkProfileMe(b *testing.B) {
     // execute the significant portion of the code you want to profile b.N times
    }
    

    In a shell script:

    # -test XXX is a trick so you don't trigger other tests by asking a non existent specific test called literally XXX
    # you can adapt the benchtime depending on the type of code you want to profile.
    
    go test -v -bench ProfileMe -test.run XXX -cpuprofile cpu.pprof -memprofile mem.pprof -benchtime 10s
    
    go tool pprof --text ./something.test cpu.pprof           ## To get a CPU profile per function
    
    go tool pprof --text ./something.test cpu.pprof --lines   ## To get a CPU profile per line
    
    go tool pprof --text ./something.test mem.pprof           ## To get the memory profile
    

    It will present you the hottests spots in each cases on the console.

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

报告相同问题?

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

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

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

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

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

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

客服 返回
顶部