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

相关文章

类型转换 1、int转string 2、string转int 3、string转float ...
package main import s "strings" import...
类使用:实现一个people中有一个sayhi的方法调用功能,代码如...
html代码: beego代码:
1、读取文件信息: 2、读取文件夹下的所有文件: 3、写入文件...
配置环境:Windows7+推荐IDE:LiteIDEGO下载地址:http:...