go-pprof笔记

开篇

编写好了golang服务之后,接着要开始关注服务的CPU,内存使用情况。golang提供了性能剖析工具,记录一些自己搜集到的信息,写下一些实践的情况。在golang中内置了pprof工具,专门来做golang语言的优化。

PProf 关注的模块

  • CPU profile:报告程序的 CPU 使用情况,按照一定频率去采集应用程序在 CPU 和寄存器上面的数据
  • Memory Profile(Heap Profile):报告程序的内存使用情况
  • Block Profiling:报告 goroutines 不在运行状态的情况,可以用来分析和查找死锁等性能瓶颈
  • Goroutine Profiling:报告 goroutines 的使用情况,有哪些 goroutine,它们的调用关系是怎样的

尝试

在代码中添加这些内容

import (
    ...
    "runtime"
    "runtime/pprof"
    ...
)
    var cpuprofile string = "./YZSvr.prof"
    f, err := os.Create(cpuprofile)
    if err != nil {
        logrus.Warn(err.Error())
    }
列名含义
flat函数执行消耗时间
flat%flat占CPU总时间的比例。程序总耗时
sum%前面每一行的flat占比总和
cum累计量
cum%cum占用总时间的比例
(pprof) top10
Showing nodes accounting for 7.47s, 73.60% of 10.15s total
Dropped 136 nodes (cum <= 0.05s)
Showing top 10 nodes out of 53
      flat  flat%   sum%        cum   cum%
     5.93s 58.42% 58.42%      5.98s 58.92%  runtime.stdcall1
     0.33s  3.25% 61.67%      2.04s 20.10%  runtime.timerproc
     0.30s  2.96% 64.63%      0.30s  2.96%  runtime.stdcall2
     0.23s  2.27% 66.90%      0.23s  2.27%  runtime.casgstatus
     0.14s  1.38% 68.28%      0.47s  4.63%  runtime.schedule
     0.14s  1.38% 69.66%      7.10s 69.95%  runtime.systemstack
     0.12s  1.18% 70.84%      0.13s  1.28%  runtime.(*mcache).prepareForSweep
     0.10s  0.99% 71.82%      0.19s  1.87%  octopus.com/octserver/YZSvr/yz_db.(*SYzDb).GetResult

生成的svg图片

 

 
20716125-bdb8ad292b8540bb.png
1R4KRH.png

 

(pprof) callgrind
Generating report in profile010.callgraph.out

安装:

go get -u github.com/google/pprof

直接使用pprof生成火焰图,在web里面查看:

go tool pprof YZSvr.exe YZSvr.prof

可以通过修改环境变量直接修改程序里面的线程数目;
export GOMAXPROCS=30

引用

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值