pprof 是 golang 性能监控的工具, 这里是我自己的一些使用经验
1. 引入 pprof 库
代码需要引入 pprof 的包, 并且启动一个 http server 服务
import (
_ "net/http/pprof"
)
然后服务中需要启动一个 http server, 但是如果原来就有 http server, 则无需另外启动
2. 获取
<endpoint>
: 为服务器的访问地址
获取 trace 数据:
curl -o <trace-filename> http://<endpoint>/debug/pprof/trace?seconds=10
解释:
-o <trace-filename>
: 将捕获到的数据保存到<trace-filename>
文件中/debug/pprof/trace
: 表明捕获的数据为trace?seconds=10
: 表明捕获时长为10s
, 如果不写该参数, 默认也是10s
获取 pprof 数据:
curl -o <pprof-filename> http://<endpoint>/debug/pprof/profile?seconds=10
解释:
/debug/pprof/profile
: 表明捕获的数据为pprof
3. 查看
查看 trace 数据:
go tool trace -http=:8080 <trace-filename>
解释:
go tool trace
: 以 trace 的方式来解析捕获文件-http=:8080
: 启动一个 http 服务, endpoint 为localhost:8080
<trace-filename>
: 指定要查看的在第 2 步中生成的数据文件
查看 pprof 数据:
go tool pprof -http=:8080 <pprof-filename>