一次性能优化实战
指标采集
在main.go中添加:
1 | go func() { |
在压测时保存下各种指标信息:
1 | curl http://127.0.0.1:6060/debug/pprof/heap -o heap_cui.out && curl http://127.0.0.1:6060/debug/pprof/goroutine -o goroutine_cui.out && curl http://127.0.0.1:6060/debug/pprof/mutex -o mutex_cui.out && curl http://127.0.0.1:6060/debug/pprof/profile?seconds=30 -o profile_cui.out && curl http://127.0.0.1:6060/debug/pprof/block -o block_cui.out |
一般很少用到trace,且trace文件体积会比较大,上百M(另外几个指标的文件大小都是几百k到几M级); 可以通过curl http://127.0.0.1:6060/debug/pprof/trace?seconds=30 -o trace_cui.out
来采集
指标分析
以分析内存分配为例:
go tool pprof heap_cui.out
,而后在交互式的命令行中输入web
,可以查看内存分配信息
但这种方式无法查看更详细的内容,更建议通过指定在浏览器中打开这种方式查看: go tool pprof --http :9091 heap_cui.out
可以有各种选择
其中Flame Graph即火焰图形式展现, 在最下方一行,占据越多宽度的,就是内存分配最多的点;
下一步就可以根据这些信息进行定位,进而优化
看起来“人畜无害”的Go官方库提供的一些字符串操作方法,如 strings.ToUpper
,strings.ToLower
,strings.SplitN
, errors.New
为什么会成为性能瓶颈?
分析一下不难发现,这几个方法都有内存分配
1 |
Go memory ballast: How I learnt to stop worrying and love the heap
原文作者: fliter
原文链接:
http://www.dashen.tech/2022/06/30/一次性能优化实战/版权声明: 转载请注明出处