golang pprof使用方法

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生成图片
调用关系图

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

你可能感兴趣的:(golang pprof使用方法)