pprof项目优化之性能优化
一、内置包pprof
runtime/pprof:采集工具型应用运行数据进行分析
net/http/pprof:采集服务型应用运行时数据进行分析
具体使用参照https://www.liwenzhou.com/posts/Go/performance_optimisation/
二、gin框架
1、导入
github.com/gin-contrib/pprof
2、注册路由
pprof.Register(r)
3、网址查看性能
/debug/pprof/profile:访问这个链接会自动进行 CPU profiling,持续 30s,并生成一个文件供下载
/debug/pprof/heap: Memory Profiling 的路径,访问这个链接会得到一个内存 Profiling 结果的文件
/debug/pprof/block:block Profiling 的路径
/debug/pprof/goroutines:运行的 goroutines 列表,以及调用关系
三、命令行交互界面
1、使用命令进入pprof命令界面
go tool pprof http://192.168.93.128:8080/debug/pprof/profile
//等待30s进行测试(web应用需要运行)
top3查询前3占用cpu情况
flat:当前函数占用CPU的耗时
flat::当前函数占用CPU的耗时百分比
sun%:函数占用CPU的耗时累计百分比
cum:当前函数加上调用当前函数的函数占用CPU的总耗时
cum%:当前函数加上调用当前函数的函数占用CPU的总耗时百分比
最后一列:函数名称
四、go-torch和火焰图
1、安装go-torch
go get -v github.com/uber/go-torch
如果看完对自己有所帮助,请点赞支持,谢谢大家