Golang pprof详解

go的pprof包

go中有pprof包来做代码的性能监控,在两个地方有包:

net/http/pprof

runtime/pprof

其实net/http/pprof中只是使用runtime/pprof包来进行封装了一下,并在http端口上暴露出来.

本篇只讲如何在web上查看性能。

一、代码部分

1.import 增加net/http/pprof包   

import(
    _ net/http/pprof
)

2. 打开http 监听端口

go func() {
        log.Println(http.ListenAndServe("localhost:10000", nil)) 
}()

二、网页上查看

*浏览器可以打开 http://127.0.0.1:10000/debug/pprof/ 可以查看各种profile索引

1.如果安装过graphviz直接提交过这步骤,否则可以到 http://www.graphviz.org/download/下载,并把bin加入到环境变量

2.查看profile :在命令行输入 

go tool pprof http://localhost:10000/debug/pprof/profile

此后的30秒进入收集profile信息的状态。

30秒后进入pprof的交互模式,然后输入

web

然后浏览器自动弹开到网页展示svg图

3.查看已经保存的profile文件

go tool pprof profile C:\Users\user\pprof\pprof.samples.cpu.004.pb.gz

然后也是进入pprof的交互模式,然后输入web

还可以查看heap和goroutine

go tool pprof http://localhost:10000/debug/pprof/heap
go tool pprof http://127.0.0.1:10000/debug/pprof/goroutine 

  

猜你喜欢

转载自www.cnblogs.com/mrblue/p/9555118.html