1.安装组件
- 安装go-torch
go get github.com/uber/go-torch - 安装 FlameGraph
cd $WORK_PATH && git clone https://github.com/brendangregg/FlameGraph.git
export PATH=$PATH:$WORK_PATH/FlameGraph - 安装graphviz
yum install graphviz(CentOS, Redhat)
2.代码修改
package main
import (
"net/http"
"net/http/pprof"
)
func main() {
// 主函数中添加
go func() {
http.HandleFunc("/debug/pprof/block", pprof.Index)
http.HandleFunc("/debug/pprof/goroutine", pprof.Index)
http.HandleFunc("/debug/pprof/heap", pprof.Index)
http.HandleFunc("/debug/pprof/threadcreate", pprof.Index)
http.ListenAndServe("0.0.0.0:8888", nil)
}()
var finishWaiter chan int
<-finishWaiter
}
3.查看结果
运行上述程序后,使用如下命令生成CPU火焰图:
go-torch -u http://localhost:8888/debug/pprof/ -p > profile-local.svg
效果图如下:
生成内存火焰图:
go-torch -u http://localhost:8888/debug/pprof/heap -p > heap-local.svg
效果图如下:
也可以分步骤查看结果:
go tool pprof --text http://localhost:7080/debug/pprof/profile
- 命令会生成一个 profile文件:pprof.samples.cpu.001.pb.gz
go-torch -p ~/Downloads/pprof.samples.cpu.001.pb.gz > ~/Desktop/profile-local.svg
问题排查
1.Centos7 使用报错
perl: warning: Setting locale failed.
perl: warning: Please check that your locale settings:
LANGUAGE = "en_US.UTF-8",
LC_ALL = "en_US.UTF-8",
LC_PAPER = "en_US.UTF-8",
LC_MONETARY = "en_US.UTF-8",
LC_NUMERIC = "en_US.UTF-8",
LC_MEASUREMENT = "en_US.UTF-8",
LC_CTYPE = "en_US.UTF-8",
LC_TIME = "en_US.UTF-8",
LANG = "en_US.UTF-8"
are supported and installed on your system.
perl: warning: Falling back to the standard locale ("C").
解决方法:
export LC_ALL="C"
网友评论