Go性能调优

简介

性能调优原则

  • 要依靠数据不是猜测
  • 要定位最大瓶颈而不是细枝末节
  • 不要过早优化
  • 不要过度优化

性能分析工具pprof

pprof是用于可视化和分析性能 分析数据的工具

排查实战

引入 _ "net/http/pprof"

go func() {
    if err := http.ListenAndServe(":6060", nil); err != nil {
        log.Fatal(err)
    }
    os.Exit(0)
}()

浏览器查看http://localhost:6060/debug/pprof

排查cpu

go tool pprof -http=:8080 http://localhost:6060/debug/pprof/cpu

go tool pprof http://localhost:6060/debug/pprof/profile?seconds=10

命令:topN

查看占用资源最多的函数

image-20220515204522928

flat当前函数本身的执行耗时
flat%flat占CPU总时间的比例
sum%上面每一行的flat%总和
cum指当前函数本身加上其调用函数的总耗时
cum%cum占CPU总时间的比例

命令:list

根据指定的正则表达式查找代码行

image-20220515215309578

web

调用关系可视化

排查堆内存

go tool pprof http://localhost:6060/debug/pprof/heap

go tool pprof -http=:8080 http://localhost:6060/debug/pprof/heap

排查协程

go tool pprof http://localhost:6060/debug/pprof/goroutine

go tool pprof -http=:8080 http://localhost:6060/debug/pprof/goroutine

排查互斥锁

go tool pprof -http=:8080 http://localhost:6060/debug/pprof/mutex

排查阻塞

go tool pprof -http=:8080 http://localhost:6060/debug/pprof/block

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值