Golang 性能定位工具pprof基本用法

2023-03-1516:19:43编程语言入门到精通Comments1,194 views字数 1952阅读模式

pprof是golang提供的一个性能分析工具,功能强大。包含cpu、heap、block、traces等执行信息。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/31320.html

原生工具包

"runtime"
"runtime/pprof"
"runtime/trace"

原生工具包包含pprof、trace。具体用法如下,直接贴代码:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/31320.html

  • 获取CPU信息
	cf, err := os.Create("cpu.pprof")
	if err != nil {
		log.Fatal("could not create CPU profile: ", err)
	}
	if err := pprof.StartCPUProfile(cf); err != nil {  //监控cpu
		log.Fatal("could not start CPU profile: ", err)
	}
	defer pprof.StopCPUProfile()
  • 获取MEM(heap)信息
mf, err := os.Create(memprofile)
	if err != nil {
		log.Fatal("could not create memory profile: ", err)
	}
// 业务代码
// ...

runtime.GC() // GC,获取最新的数据信息
if err := pprof.WriteHeapProfile(mf); err != nil {  // 写入内存信息
		log.Fatal("could not write memory profile: ", err)
}
  • 获取trace信息
tf, err := os.Create("trace.out")
	if err != nil {
		panic(fmt.Errorf("failed to create trace output file: %v", err))
	}
	defer func() {
		if err := tf.Close(); err != nil {
			log.Fatalf("failed to close trace file: %v", err)
		}
	}()

	// 写trace文件
	if err := trace.Start(tf); err != nil {
		log.Fatalf("failed to start trace: %v", err)
	}
	defer trace.Stop()

通用方式

golang net/pprof 封装了runtime包中的pprof 工具,大多数情况定位和分析问题都是在线上,直接使用runtime原生工具需要停止程序,或者需要编码人员写框架代码支持动态获取,相对的比较麻烦。 net/pprof库做到了无需停止程序,可以直接使用文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/31320.html

使用简单文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/31320.html

main 包中import
	"net/http"
	_ "net/http/pprof"
//代码中添加:
http.ListenAndServe("http://127.0.0.1:8080",nil)

接下来就可以直接使用了,直接在浏览器中输入 http://127.0.0.1:8080/debug/pprof/文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/31320.html

Golang 性能定位工具pprof基本用法

net pprof 网页界面文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/31320.html

可以看到runtime包中cpu、heap、trace等与之对应的都可以获取到。其中profile和trace直接点击可以下载文件,然后使用go tool pprof xx进行分析。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/31320.html

在生产环境无法直接使用浏览器,也可以通过以下方式下载离线文件,然后通过go tool 进行分析:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/31320.html

go tool pprof http://localhost:6060/debug/pprof/heap
// 获取cpu信息,采集时间为30s
go tool pprof http://localhost:6060/debug/pprof/profile?seconds=30
go tool pprof http://localhost:6060/debug/pprof/block
go tool pprof http://localhost:6060/debug/pprof/mutex
// 获取trace文件,采集时间为5s
wget -O trace.out http://localhost:6060/debug/pprof/trace?seconds=5
go tool trace trace.out
// 其中go tool pprof 也可以使用curl -s xxx > xx.prof 来替代,获取离线文件,例如
curl -s http://localhost:6060/debug/pprof/heap > heap.prof

建议net pprof 监听使用127.0.0.1 能够在一定程度上防止他人恶意采集。因为在采集信息的过程中对业务是有性能损耗的。runtime底层pprof其实就是一系列的埋点操作。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/31320.html

常见实用场景

Golang pprof分析CPU文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/31320.html

Golang 内存性能分析文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/31320.html

go pprof可以直接可视化,但需要安装Graphviz图形工具,具体安装方式见官网:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/31320.html

Download | Graphviz文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/31320.html

https://graphviz.org/download/
文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/31320.html
  • 本站内容整理自互联网,仅提供信息存储空间服务,以方便学习之用。如对文章、图片、字体等版权有疑问,请在下方留言,管理员看到后,将第一时间进行处理。
  • 转载请务必保留本文链接:https://www.cainiaoxueyuan.com/ymba/31320.html

Comment

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定