Skip to Content

golang pprof

Posted on 4 mins read

Golang 性能分析工具

http://studygolang.com/articles/9340

Pprof

pprof 起初是谷歌性能工具 gperftools 中的一个组件。Golang 中实现了 runtime/pprofnet/http/pprof 两个包来实现相关的支持,而 net/http/pprof 包则是对 runtime/pprof 的封装,暴露一个 http 服务来方便的进行性能分析。

Golang pprof 用法

Golang HTTP Web 服务

基于 net/http 包开发的 Web 服务,只需在入口导入包 import _ "net/http/pprof" 即可通过访问 http://host:port/debug/pprof/ 查看当前服务的各种 profiling 数据。

Golang 后端服务

基于 Golang 开发的后端服务进程也可以通过导入包 import "net/http" 开启 HTTP 服务端口(比如:http.ListenAndServe("localhost:6060", nil)),同时导入包 import _ "net/http/pprof" 同样可以通过访问 http://host:6060/debug/pprof/ 来查看各种 profiling 数据。

Golang 应用程序

如果只是想对某段 Golang 应用程序代码进行调优,无法使用 net/http/pprof 包来通过 HTTP 服务查看,这时我们就需要使用更底层的 runtime/pprof 包。下面引用 Golang Blog 中的例子来看下 runtime/pprof 包的使用。

var cpuprofile = flag.String("cpuprofile", "", "write cpu profile to file")

func main() {
    flag.Parse()
    if *cpuprofile != "" {
        f, err := os.Create(*cpuprofile)
        if err != nil {
            log.Fatal(err)
        }
        pprof.StartCPUProfile(f)
        defer pprof.StopCPUProfile()
    }

例中通过解析参数 cpuprofile 获取了 profiling 结果信息导出的文件名,通过调用 pprof.StartCPUProfile(f),开始对程序 CPU 使用情况进行采样分析,并最终通过调用 defer pprof.StopCPUProfile() 在程序运行结束前将结果导出到文件 f 中,最终我们就可以通过运行 go tool pprof 命令来解析生成的 profiling 文件。

$ go tool pprof havlak1 havlak1.prof
Welcome to pprof!  For help, type 'help'.
(pprof)
(pprof) top10
Total: 2525 samples
     298  11.8%  11.8%      345  13.7% runtime.mapaccess1_fast64
     268  10.6%  22.4%     2124  84.1% main.FindLoops
     251   9.9%  32.4%      451  17.9% scanblock
     178   7.0%  39.4%      351  13.9% hash_insert
     131   5.2%  44.6%      158   6.3% sweepspan
     119   4.7%  49.3%      350  13.9% main.DFS
      96   3.8%  53.1%       98   3.9% flushptrbuf
      95   3.8%  56.9%       95   3.8% runtime.aeshash64
      95   3.8%  60.6%      101   4.0% runtime.settype_flush
      88   3.5%  64.1%      988  39.1% runtime.mallocgc

**

http://colobu.com/2017/03/02/a-short-survey-of-golang-pprof/

4899 subl /usr/local/go/test 4900 subl /usr/local/go/src/testing 4901 $MCODE/z/git/idevz.org 4902 tree ./content 4903 hugox new life/confront-the-life.md 4904 subl /usr/local/go/ 4905 ll 4906 $GOPATH/src/github.com/idevz 4907 ll 4908 golearning 4909 l 4910 go tool cover -html=cover.out 4911 go tool pprof golearning.test cpu.out 4912 telnet 10.211.55.15 6060 4913 $GOPATH/src/motan-go/main 4914 ll 4915 ggr magent.go 4916 go run magent.go 4917* losf -i:6060 4918* lsof -i:6060 4919 telnet 10.211.55.15 6060 4920* lsof -i:6060 4921 telnet 10.211.55.15 6060 4922 curl -i -vvv http://10.211.55.15:6060/debug/pprof/ 4923 curl -i -vvv http://10.211.55.15:6060/debug/pprof/block?debug=1 4924 curl -i -vvv ‘http://10.211.55.15:6060/debug/pprof/block?debug=1' 4925 go run magent.go 4926* $GOPATH/src/motan-go/main 4927* $GOPATH/src/github.com/idevz 4928* ll 4929* go tool pprof -web http://127.0.0.1:6060/debug/pprof/profile 4930* ll /Users/idevz/pprof/pprof.127.0.0.1:6060.samples.cpu.001.pb.gz 4931* go tool pprof http://127.0.0.1:6060/debug/pprof/profile 4932* zc1 4933* ll 4934* ping idevz.local 4935* go tool pprof http://127.0.0.1:6060/debug/pprof/profile 4936* tree content 4937* hugox new tech/golang/golang-pprof.md 4938* hugox new tech/golang/golang-testing.md 4939* go get github.com/uber/go-torch 4940* go get github.com/rakyll/gom/cmd/gom 4941* go get github.com/rakyll/gom/ 4942* go get github.com/rakyll/gom/cmd/gom 4943 $GOPATH/src/github.com/rakyll/gom/cmd/gom 4944 ll 4945 go install 4946 mv $GOPATH/bin/gom $GOPATH/bin/gomx 4947 /Volumes/g/idevz/code/z/git/go/src/motan-go/main/ 4948 go run magent.go 4949* gomx –target http://localhost:6060

10032 $GOPATH/src/github.com/idevz 10033 ll 10034 motan-agent 10035 l 10036 ggr showbatch.go 10037 zc1 10038 $GOPATH/src/google.golang.org/grpc/benchmark 10039 ll 10040 ./client 10041 ll 10042 go test main.go 10043 ggr main.go 10044 ggr main.go -h 10045 ggr main.go 10046 pwd 10047 /media/psf/g/idevz/code/z/git/go/src/google.golang.org/grpc/benchmark/server 10048 netstat -natpl 10049 sudo netstat -natpl 10050 ggr main.go 10051 sudo netstat -natpl 10052 ggr main.go 10053 sudo netstat -natpl 10054 ggr main.go 10055 ggr main.go -server=:41581 10056 sudo netstat -natpl 10057 ipc 10058 ipcs -q 10059 ggr main.go -server=:41581 -duration=10 10060 . 10061 .. 10062 lll 10063 ll 10064 go test -v 10065 go help test 10066 go help testflag 10067 go test -run Main 10068 pwd 10069 $GOPATH/src/github.com/idevz/go-learning 10070 ll 10071 go test -v 10072 ../golearning 10073 go test -v 10074 go test -v -bench 10075 go test -v -bench . 10076 go test -v -bench . -benchmem 10077 go test -v -bench . -benchmem -cpu 1,2,3,4 10078 go test -v -bench . -benchmem -cpu 1,2,3,4 -benchtime 5s 10079 go test -v 10080 go addt_test.go 10081 go test 10082 ll 10083 go test -cover 10084 go test -cover -covermode=count 10085 go test -cover -covermode=count -coverprofile=cover.out 10086 go tool cover -func=cover.out 10087 go tool cover -html=cover.out 10088 mv /tmp/cover709222752/coverage.html . 10089 go test -v -test.bench . -cpuprofile cpu.out -memprofile mem.out 10090 ll 10091 go tool pprof golearning.test mem.out 10092 ll 10093 cat mem.out 10094 cat golearning.test 10095 ll 10096 go tool pprof golearning.test mem.out 10097 go tool pprof golearning.test cpu.out 10098 go tool pprof golearning.test mem.out 10099 cat mem.out 10100 cat cpu.out 10101 go tool pprof golearning.test cpu.out 10102 go tool pprof golearning.test cpu.out callgrind>xxx 10103 go tool pprof golearning.test cpu.out 10104 sudo netstat -natpl 10105 ll 10106 $GOPATH/src/motan-go/main 10107 ll 10108 ll magent* 10109 ll magent.z.* 10110 rm -r magent.z.* 10111 ll 10112 cat magent.INFO 10113 rm -rf magent.INFO 10114 ggr magent.go 10115 sudo netstat -natpl 10116 ifconfig 10117 ggr magent.go 10118 sudo netstat -natpl 10119 ggr magent.go 10120 sudo netstat -natpl 10121 ifconfig 10122 ggr magent.go 10123 pwd 10124 ll 10125 $MCODE/www/motan-client/examples/ 10126 ll 10127 php motan.php 10128* history 10129 go get github.com/mkevac/debugcharts 10130 $GOPATH/src/github.com/rakyll/gom/cmd/gom 10131 go install 10132 $MCODE/www/motan-client/examples 10133 php motan.php

硬编码的 output 10091 go tool pprof golearning.test mem.out 10092 ll 10093 cat mem.out 10094 cat golearning.test 10095 ll 10096 go tool pprof golearning.test mem.out 10097 go tool pprof golearning.test cpu.out 10098 go tool pprof golearning.test mem.out 10099 cat mem.out 10100 cat cpu.out 10101 go tool pprof golearning.test cpu.out 10102 go tool pprof golearning.test cpu.out callgrind>xxx 10103 go tool pprof golearning.test cpu.out 10104 sudo netstat -natpl 10105* ll 10106* $GOPATH/src/motan-go/main 10107* ll 10108* ll magent*

comments powered by Disqus