关于pprof
概述
pprof是GoLang程序性能分析工具,prof是profile(画像)的缩写,用pprof我们可以分析下面9种数据
真正分析时常用4种
CPU Profiling:CPU 分析,按照一定的频率采集所监听的应用程序 CPU(含寄存器)的使用情况,可确定应用程序在主动消耗 CPU 周期时花费时间的位置
Memory Profiling:内存分析,在应用程序进行堆分配时记录堆栈跟踪,用于监视当前和历史内存使用情况,以及检查内存泄漏
Block Profiling:阻塞分析,记录 goroutine 阻塞等待同步(包括定时器通道)的位置
Mutex Profiling:互斥锁分析,报告互斥锁的竞争情况
非HTTP,工具类的应用
runtime/pprof
在本地生成好cpu,和内存prof文件.然后用go tool prof进行对这些文件的分析.
go tool pprof -http=:9091 cpuprofile
go tool pprof -http=:9091 memroyprofile
package main
import (
"fmt"
"os"
"runtime/pprof"
)
func main() {
//CPU Profile
f, err := os.Create("./cpuprofile")
if err != nil {
fmt.Println(err)
return
}
defer f.Close()
pprof.StartCPUProfile(f)
defer pprof.StopCPUProfile()
//Memory Profile
fm, err := os.Create("./memoryprofile")
if err != nil {
fmt.Println(err)
return
}
defer fm.Close()
pprof.WriteHeapProfile(fm)
for i := 0; i < 100; i++ {
fmt.Println("程序员麻辣烫")
}
}
http类分析
可以在程序中直接集成http一个请求端口,然后再浏览器中浏览,也可配合go tool pprof 进行命令行交互.
go tool pprof http://localhost:8082/debug/pprof/profile
gin
"github.com/gin-contrib/pprof"
grpc
go func() {
http.ListenAndServe(":10001", nil)
}()
火焰图
https://blog.csdn.net/Mr_XiMu/article/details/131123457
需要安装 graphviz
https://blog.csdn.net/Mr_XiMu/article/details/131123457