pprof命令总结

启用pprof分析

import (
    "net/http"
    _ "net/http/pprof"
)

func pprof() error {
    if err := http.ListenAndServe("0.0.0.0:8080", nil); err != nil {
        return err
    }
    return nil
}

func main() {
    go pprof()
    // order codes
}

获取诊断报告进行分析

直接访问 localhost:8080/debug/pprof 可以看到能获取到的诊断报告

诊断报告类型 备注
allocs 内存分配采样
block 导致阻塞的的同步语句堆栈信息, 但需要使用runtime.SetBlockProfileRate(1)开启
cmdline 进程启动的命令行参数
goroutine 当前所有goroutine的堆栈信息
heap 当前活动的对象内存分配采样
mutex 持有锁的堆栈信息
profile cpu的占用信息
threadcreate 导致操作系统创建新增的线程的堆栈信息
trace 当前程序执行的trace

分析CPU占用

go tool pprof --http=0.0.0.0:8081 --seconds=10 localhost:8080/debug/pprof/profile

分析内存占用

go tool pprof --http=0.0.0.0:8081 localhost:8080/debug/pprof/heap
go tool pprof --http=0.0.0.0:8081 localhost:8080/debug/pprof/allocs

查看trace信息

wget -O trace.out localhost:8080/debug/pprof/trace
go tool trace --http=:8081 trace.out
posted @ 2021-04-11 20:07  Me1onRind  阅读(699)  评论(0编辑  收藏  举报