golang pprof使用

在程序中加上:

import (

_ "net/http/pprof"

)

 

go func() {
   http.ListenAndServe("127.0.0.1:8888", nil)
}()

如果需要监听block阻塞情况:

runtime.SetBlockProfileRate(5 * 1000 * 1000) //采样频率,ns

监听锁事件:

// 当 rate = 0 时,关闭 mutex prof (默认值)
// 当 rate = 1 时,表示记录所有的 mutex event
// 当 rate > 1 时,记录 1/rate 的 mutex event(随机)
runtime.SetMutexProfileFraction(1)

浏览器查看:

打开浏览器访问 http://localhost:8888/debug/pprof/

 

命令行分析:

1. 安装Graphviz
brew install graphviz

2. cpu profiling:

go tool pprof http://localhost:8888/debug/pprof/profile

会生成pb.gz文件:

pprof.test.samples.cpu.001.pb.gz

 

可视化界面:

go tool pprof -http=":8081" pprof.test.samples.cpu.001.pb.gz
访问响应URL,即可查看火焰图

curl http://localhost:8081/ui/flamegraph > flamegraph.html

 

参考内存占用:

go tool pprof -alloc_space http://localhost:8888/debug/pprof/heap

可以使用参数指明分析的类型:

inuse_space — amount of memory allocated and not released yet

inuse_objects— amount of objects allocated and not released yet

alloc_space — total amount of memory allocated (regardless of released)

alloc_objects — total amount of objects allocated (regardless of released)

进入交互式模式之后,比较常用的有 top、list、traces、web 等命令。

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值