Go内置标准库
- runtime/pprof:采集工具型应用运行数据进行分析
- net/http/pprof:采集服务型应用运行时数据进行分析
工具型 runtime/pprof
pprof.StartCPUProfile(w io.Writer) // 开启CPU性能分析
pprof.StopCPUProfile() // 关闭cpu性能分析
然后会生成一个文件进行分析,使用命令
go tool pprof xxx.txt
pprof.WriteHeapProfile(w io.Writer) // 记录程序堆栈(内存)信息
然后同上使用命令
go tool pprof xxx.txt
服务型 net/http/pprof
import _ "net/http/pprof"
// 需要手动注册一些路由:
r.HandleFunc("/debug/pprof/", pprof.Index)
r.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline)
r.HandleFunc("/debug/pprof/profile", pprof.Profile)
r.HandleFunc("/debug/pprof/symbol", pprof.Symbol)
r.HandleFunc("/debug/pprof/trace", pprof.Trace)
如果你使用的是gin框架,那么推荐使用"github.com/DeanThompson/ginpprof"。