安装 graphviz
brew install graphviz // for macos
apt install graphviz // for ubuntu
yum install graphviz // for centos
设置svg文件默认打开程序
右键svg文件 ->显示简介->默认程序
//main.go 文件
package main
import (
// 略
_ "net/http/pprof" // 会自动注册 handler 到 http server,方便通过 http 接口获取程序运行采样报告
// 略
)
func main() {
// 略
runtime.GOMAXPROCS(1) // 限制 CPU 使用数,避免过载
runtime.SetMutexProfileFraction(1) // 开启对锁调用的跟踪
runtime.SetBlockProfileRate(1) // 开启对阻塞操作的跟踪
go func() {
// 启动一个 http server,注意 pprof 相关的 handler 已经自动注册过了
if err := http.ListenAndServe(":6060", nil); err != nil {
log.Fatal(err)
}
os.Exit(0)
}()
// 略
}
打开浏览器访问 http://localhost:6060/debug/pprof/
交互式终端
go tool pprof http://localhost:6060/debug/pprof/profile
go tool pprof http://localhost:6060/debug/pprof/heap
go tool pprof http://localhost:6060/debug/pprof/allocs
go tool pprof http://localhost:6060/debug/pprof/goroutine
go tool pprof http://localhost:6060/debug/pprof/mutex
go tool pprof http://localhost:6060/debug/pprof/block
命令: top/list/web
火焰图
go tool pprof -http=:8081 ~/pprof/[文件路径名].pb.gz
参考链接
https://blog.wolfogre.com/posts/go-ppof-practice/
https://book.eddycjy.com/golang/tools/go-tool-pprof.html
原文:https://www.cnblogs.com/sanmubai/p/11996754.html