prof安装
cd ~
git clone https://github.com/brendangregg/FlameGraph.git
cd FlameGraph-master
cp flamegraph.pl /usr/local/bin
// 检测FlameGraph安装是否成功
flamegraph.pl -h
火焰图安装
go get github.com/uber/go-torch
代码埋点
const (
cpuProfile = "cpuprof"
memProfile = "memprof"
)
// cpu埋点
fc, _ := os.Create(cpuProfile)
pprof.StartCPUProfile(fc)
defer func() {
pprof.StopCPUProfile()
fc.Close()
}()
// mem埋点
fm, _ := os.Create(memProfile)
pprof.WriteHeapProfile(fm)
defer fm.Close()
代码开始运行生成.prof文件
./main
解析prof 文件
go tool pprof -svg ./main cpuprof
go tool pprof -svg ./main memprof
生成火焰图
go-torch --binaryname=./main --binaryinput=cpuprof
go-torch --binaryname=./main --binaryinput=memprof
参考文献