Go 库性能分析工具pprof
作者:小马别过河 发布时间:2024-02-13 20:55:27
场景
我们一般没必要过度优化 Go 程序性能。但是真正需要时,Go 提供的 pprof 工具能帮我们快速定位到问题。比如,我们团队之前有一个服务,在本地和测试环境没问题,一到灰度环境,就报 cpu 负载过高,后经排查,发现某处代码死循环了。我把代码简化成如下:
// 处理某些业务,真实的代码中这个死循环很隐蔽
func retrieveSomeThing() {
for {}
}
// 处理其他的一些业务,无意义,用于后续做例子
func doSomeThing() {
do1()
for i := 0; i < 200000000; i++ {}
do2()
}
// 无意义
func do1() {
for i := 0; i < 200000000; i++ {}
}
// 无意义
func do2() {
for i := 0; i < 200000000; i++ {}
}
func main() {
go retrieveSomeThing()
go doSomeThing()
// 阻塞一下
time.Sleep(3 * time.Second)
}
解决问题前,先介绍下 pprof。
pprof
pprof 包会输出运行时的分析数据(profiling data),这些数据可以被 pprof 的可视化工具解析。Go 标准库主要提供了两个包:
runtime/pprof
通过写入到文件的方式暴露 profile 数据;net/http/pprof
通过 http 服务暴露 profile 数据,适用于守护进程。
生成 profile 文件
CPU 性能分析
在 runtime/pprof
中,使用StartCPUProfile
开启 CPU 性能分析。退出程序前,需要调用StopCPUProfile
把采样数据 flush 到输出文件。
采样的频率默认是 100 Hz(每秒 100 次)。
// 输出到标准输出,一般是指定文件
if err := pprof.StartCPUProfile(os.Stdout); err != nil {
log.Fatal("could not start CPU profile: ", err)
}
defer pprof.StopCPUProfile()
内存性能分析
调用 WriteHeapProfile
开启内存性能分析:
// 输出到标准输出,一般是指定文件
if err := pprof.WriteHeapProfile(os.Stdout); err != nil {
log.Fatal("could not write memory profile: ", err)
}
}
分析 profile 文件 && 优化代码
以开篇的代码为例,由于是 CPU 过载,我们可以在 main 函数开启 CPU Profile:
// 通过参数指定 cpu profile 输出的文件
var cpuprofile = flag.String("cpuprofile", "", "write cpu profile to `file`")
func main() {
flag.Parse()
if *cpuprofile != "" {
f, err := os.Create(*cpuprofile)
if err != nil {
log.Fatal("could not create CPU profile: ", err)
}
// 开启 CPU 分析
if err := pprof.StartCPUProfile(f); err != nil {
log.Fatal("could not start CPU profile: ", err)
}
defer pprof.StopCPUProfile()
}
// 业务代码
go retrieveSomeThing()
go doSomeThing()
// 模拟阻塞
time.Sleep(5 * time.Second)
}
我们执行命令,输出 profile 文件到 cpu.prof。
go run main.go -cpuprofile cpu.prof
go tool pprof
Go 提供性能解析工具:go tool pprof
。我们使用 go tool 打开 profile 文件。
> go tool pprof cpu.prof
Type: cpu
Time: Nov 16, 2022 at 1:40pm (CST)
Duration: 5.17s, Total samples = 4.54s (87.75%)
Entering interactive mode (type "help" for commands, "o" for options)
(pprof)
这是个交互式的界面,输入help
可以查看所有命令。
top 命令
我们使用 topN 命令,查看根据 flat 从大到小排序的前 N 条数据。
(pprof) top10
Showing nodes accounting for 4650ms, 100% of 4650ms total
flat flat% sum% cum cum%
4220ms 90.75% 90.75% 4450ms 95.70% main.retrieveSomeThing
230ms 4.95% 95.70% 230ms 4.95% runtime.asyncPreempt
80ms 1.72% 97.42% 200ms 4.30% main.doSomeThing
70ms 1.51% 98.92% 70ms 1.51% main.do2 (inline)
50ms 1.08% 100% 50ms 1.08% main.do1 (inline)
top 命令返回数据有5个指标:
flat : 本函数占用的 CPU 时间,不包括调用函数的时间;
flat% : flat 占的百分比;
sum% : 前面 flat% 的总和;
cum : 累计时间,包括调用的函数的时间;
cum% : cum 的百分比。
以main.doSomeThing
(排第三的函数)为例子,耗时为:
func doSomeThing() { // flat: 80ms cum: 200ms
do1() // 执行时间 50ms
for i := 0; i < 200000000; i++ {} // 执行时间 80ms
do2() // 执行时间 70ms
}
doSomeThing
的 flat 的值为:
for i := 0; i < 200000000; i++ {}的执行时间(80ms),不包括do1和do2的时间。
doSomeThing
的 cum 的值为:
cum(200ms) = doSomething的flat(80ms) + do1的flat(50ms) + do2的flat(70ms)
ps: top 可以使用 -cum 参数来指定,根据 cum 排序。
list 命令
明白了 top 的指标的意思,我们关注到,排在 top1 的函数是 retrieveSomeThing。可以使用 list 命令,查看 retrieveSomeThing 耗时:
(pprof) list retrieveSomeThing
Total: 4.65s
ROUTINE ======================== main.retrieveSomeThing in /xxxx/pprof_note/pprof/main.go
4.22s 4.45s (flat, cum) 95.70% of Total
10ms 10ms 1:package main
. . 2:
. . 3:import (
. . 4: "flag"
. . 5: "log"
. . 6: "os"
. . 7: "runtime/pprof"
. . 8: "time"
. . 9:)
. . 10:
. . 11:// 处理某些业务,真实的代码中这个死循环很隐蔽
. . 12:func retrieveSomeThing() {
4.21s 4.44s 13: for {
. . 14: }
. . 15:}
. . 16:
. . 17:// 处理其他的一些业务,无意义,用于后续做例子
. . 18:func doSomeThing() {
我们定位到13行需要优化。
来源:https://juejin.cn/post/7166518730514497543
猜你喜欢
- iterrows(),iteritems(),itertuples()区别Python函数之iterrows, iteritems, ite
- 一、数据预处理实验数据来自genki4k提取含有完整人脸的图片def init_file(): num = 0&n
- 本文实例为大家分享了JSP学生信息管理系统源码,供大家参考,具体内容如下新建学生信息数据库1.添加记录模块<%@ page conte
- Python3中sort方法是列表类型list的内置方法,使用sort方法对list排序会修改list本身,不会返回新的list。sort方
- 详细:1.闵可夫斯基距离(Minkowski Distance)2.欧氏距离(Euclidean Distance)3.曼哈顿距离(Manh
- 对Python字符串,除了比较老旧的%,以及用来替换掉%的format,及在python 3.6中加入的f这三种格式化方法以外,还有可以使用
- 像素误差看自己设计好上线的网站,偶尔会发觉像素行间出现了弹性空间,总在不经意间蹦出一定的差距。有些页面很难发现,比如活动类页面,这类页面多呈
- 1.链式法则根据以前的知识,如果我们需要寻找到目标参数的值的话,我们需要先给定一个初值,然后通过梯度下降,不断对其更新,直到最终的损失值最小
- 一、查看event是否开启show variables like '%sche%'; set global ev
- ASP木马防御: 代码如下:const adTypeBinary=1 dim jpg(1):jpg(0)=CB
- 一、XGBoostXGBoost并不是一种模型,而是一个可供用户轻松解决分类、回归或排序问题的软件包。1 XGBoost的优点简单易用。相对
- 主要应用了argsort()函数,函数原型:numpy.argsort(a, axis=-1, kind='quicksort
- API说明调用地址:http://yzxyzm.market.alicloudapi.com/yzx/verifySms请求方式:POST返
- 前言在laravel项目开发中,经常使用到公共函数,那如何在laravel配置全局公共函数呢??下面话不多说了,来一起看看详细的介绍吧方法如
- Postman的脚本可以导出多种语言的脚本,方便二次维护开发。Python的requests库,支持python2和python3,用于发送
- 一、建立一个空白的解决方案,名称为“SpringDotNot”二、新建一个类库项目:IBLL在IBL
- 需要用到的库有selenium,还需要安装Chrome浏览器驱动,具体如何安装我就不详述了在这里我模拟了csdn的登录过程**1**.首先打
- 介绍本文主要介绍Python中迭代的基本知识和使用什么是迭代在Python中,如果给定一个list或tuple,我们可以通过for循环来遍历
- 项目应用中,曾有以下一个场景:接口中要求发送一个int类型的流水号,由于多线程模式,如果用时间戳,可能会有重复的情况(当然概率很小)。所以想
- 是的,我在这里要说:注册表单将死(好拗口啊,但是不知道怎么翻译更恰当)。回想一下,当你想享受一个网站服务的时候,碰到的第一件事是什么?一个表