golang pprof使用方法

陈陈陈_6150 · · 2007 次点击 · · 开始浏览    
这是一个创建于 的文章,其中的信息可能已经有所发展或是发生改变。

pprof 是用于可视化和分析性能分析数据的工具,可以对程序进行CPU分析、内存分析、组撒分析以及互斥锁的分析
pprof内置了两个库用于获取程序运行时的数据

  • runtime/pprof
    用来采集程序的运行数据进行分析
  • net/http/pprof
    采集http服务运行时的数据尽心分析

主要写下这次用到的结合beego的服务型应用的数据分析
首先在router.go中加入路由

beego.Router("/debug/pprof", &controllers.ProfController{})
beego.Router("/debug/pprof/:app([\\w]+)", &controllers.ProfController{})

并在controllers中加入控制器


type ProfController struct {
    beego.Controller
}

func (c *ProfController) Get() {
    switch c.Ctx.Input.Param(":app") {
    default:
        pprof.Index(c.Ctx.ResponseWriter, c.Ctx.Request)
    case "":
        pprof.Index(c.Ctx.ResponseWriter, c.Ctx.Request)
    case "cmdline":
        pprof.Cmdline(c.Ctx.ResponseWriter, c.Ctx.Request)
    case "profile":
        pprof.Profile(c.Ctx.ResponseWriter, c.Ctx.Request)
    case "symbol":
        pprof.Symbol(c.Ctx.ResponseWriter, c.Ctx.Request)
    }
    c.Ctx.ResponseWriter.WriteHeader(200)
}

然后运行程序就可以通过web进行访问了,访问http://localhost:8080/debug/pprof将会看到这样一个页面:

image.png

可以访问他的子页面查看具体的分析结果。

  • allocs
    过去所有的内存分析
  • block
    阻塞情况,block profiling
  • cmdline
    程序启动时的命令参数
  • goroutine
    正在运行的goroutines ,以及他们之间的调用关系。
  • heap
    memory profiling,得到程序的内存信息
  • mutex
    查看导致互斥锁的竞争持有者的信息
  • profile
    访问这个连接会进行30s 的 cpu profiling,结束后生成一个profle文件下载
  • threadcreate
    查看创建线程的信息
  • trace
    当前程序执行的信息
  • full goroutine stack dump
    显示所有goroutine的信息

这边目前有个问题,点击对应标签无法访问到对应的页面,如点击goroutine,跳转的地址是http://localhost:8080/debug/goroutine?debug=1实际上需要访问的地址是http://localhost:8080/debug/pprof/goroutine?debug=1

go tool pprof 分析工具
使用go tool pprof工具分析prof文件进行分析,并且可以生成调用关系图或者火焰图

执行命令
go tool pprof --text http://127.0.0.1:8080/debug/pprof/profile
得到prof文件

下载prof文件

输入top可以查看cpu使用情况
cup使用情况

输入web生成图片
调用关系图

暂时先写这么多,火焰图没有用到


有疑问加站长微信联系(非本文作者)

本文来自:简书

感谢作者:陈陈陈_6150

查看原文:golang pprof使用方法

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

2007 次点击  
加入收藏 微博
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传