高质量编程与性能调优实践02 | 青训营

47 阅读1分钟

02性能调优实战

2.1简介

过早优化:功能会迭代或者用户数量没到性能瓶颈,过早就没用;

image-20230603111621576

2.2 性能分析工具 pprof

image-20230603112257740

2.2.1 pprof 功能简介

image-20230603112312625

2.2.2 pprof 实战排查

github.com/wolfogre/go…

image-20230603113047439

浏览器查看指标

http://localhost:6060/debug/pprof/

image-20230603113541097

CPU 采集10s样本数据

image-20230603113723179

image-20230603114210899

image-20230603114737624

image-20230603115005045

web自动生成 调用关系图(每个节点 调用 消耗的数据和 调用链);

image-20230603115220379

Heap—堆内存

image-20230603115501294

image-20230603115812000

可以把这个占用了1G的方法注释掉

image-20230603115825075

注释之后 ;内存占用 降低

image-20230603130017814

image-20230603130105086

goroutine—协程

image-20230603130121588

image-20230603130323793

image-20230603130410545

image-20230603130458334

image-20230603130524325

mutex—锁

image-20230603130606574

block—阻塞

image-20230603130628343

image-20230603130712587

image-20230603131841264

image-20230603132005803

小结

image-20230603132046583

2.2.3 pprof采样过程&原理

CPU

image-20230603160558729

Heap—堆内存

image-20230603161836863

Goroutine—协程&ThreadCreate—线程创建

image-20230603161825429

Block& Mutex

image-20230603201634199

image-20230603201814165

2.3 性能调优案例

image-20230603201933681

2.3.1 业务服务优化

image-20230603202116032

建立服务性能评估手段

image-20230604192314006

分析性能数据,定位性能瓶颈
重点优化项改造
优化效果验证

image-20230604192931017

image-20230604193017567

2.3.2 基础库优化

image-20230604193220700

2.3.3 Go语言本身优化

image-20230604193348017

2.4 总结

image.png