Golang 大杀器之性能剖析 PProf


本文摘自网络,作者,侵删。

Golang 大杀器之性能剖析 PProf

来源(转)

https://www.jianshu.com/p/4e4ff6be6af9

支持的使用模式:

* Report generation:报告生成
  • Interactive terminal use:交互式终端使用
  • Web interface:Web 界面

用途

  • 作用

    CPU Profiling:CPU 分析,按照一定的频率采集所监听的应用程序 CPU(含寄存器)的使用情况,
    可确定应用程序在主动消耗 CPU 周期时花费时间的位置
    
    Memory Profiling:内存分析,在应用程序进行堆分配时记录堆栈跟踪,用于监视当前和历史内存使用情况,
    以及检查内存泄漏
    
    Block Profiling:阻塞分析,记录 goroutine 阻塞等待同步(包括定时器通道)的位置
    
    Mutex Profiling:互斥锁分析,报告互斥锁的竞争情况
    

使用

  • go

    import (
        "net/http"
        _ "net/http/pprof"
    )
    
    func main() {
        http.ListenAndServe("0.0.0.0:6060", nil)
    }
    

访问

  • web界面

    查看当前总览:访问 http://127.0.0.1:6060/debug/pprof/
    
    • cpu(CPU Profiling): $HOST/debug/pprof/profile,默认进行 30s 的 CPU Profiling,得到一个分析用的 profile 文件

    • block(Block Profiling):$HOST/debug/pprof/block,查看导致阻塞同步的堆栈跟踪

    • goroutine:$HOST/debug/pprof/goroutine,查看当前所有运行的 goroutines 堆栈跟踪

    • heap(Memory Profiling): $HOST/debug/pprof/heap,查看活动对象的内存分配情况

    • mutex(Mutex Profiling):$HOST/debug/pprof/mutex,查看导致互斥锁的竞争持有者的堆栈跟踪

    • threadcreate:$HOST/debug/pprof/threadcreate,查看创建新OS线程的堆栈跟踪

  • 通过交互式终端使用

    (1) go tool pprof http://localhost:6060/debug/pprof/profile?seconds=60
    ## CPU
    

go tool pprof http://localhost:6060/debug/pprof/profile?seconds=60

heap 分析

(2) go tool pprof http://localhost:6060/debug/pprof/heap?seconds=60

block分析

(3) go tool pprof http://localhost:6060/debug/pprof/block?seconds=60

协程分析

(4) go tool pprof http://localhost:6060/debug/pprof/goroutine?seconds=60

mutex 分析

(5) go tool pprof http://localhost:6060/debug/pprof/mutex?seconds=60


* flat:当前函数占用CPU的耗时

* flat%:当前函数占用CPU的耗时百分比

* sum%:给定函数累积使用 CPU 总比例

* cum:当前函数加上调用当前函数的函数占用CPU的总耗时

* cum%:当前函数加上调用当前函数的函数占用CPU的总耗时百分比

* -inuse_space:分析应用程序的常驻内存占用情况

* -alloc_objects:分析应用程序的内存临时分配情况

  

~~~json
$ go tool pprof http://localhost:6060/debug/pprof/heap
Fetching profile over HTTP from http://localhost:6060/debug/pprof/heap
Saved profile in /Users/eddycjy/pprof/pprof.alloc_objects.alloc_space.inuse_objects.inuse_space
.008.pb.gz
Type: inuse_space
Entering interactive mode (type "help" for commands, "o" for options)
(pprof) top
Showing nodes accounting for 837.48MB, 100% of 837.48MB total
      flat  flat%   sum%        cum   cum%
  837.48MB   100%   100%   837.48MB   100%  main.main.func1
  • PProf 可视化界面

    go tool pprof -http=":8081" [binary] [profile]
    
    binary:可执行程序的二进制文件,通过go build命名生成
    profile: protobuf格式的文件或者地址
    
    如:go tool pprof -http=":8081" ew-iepms.exe http://localhost:6060/debug/pprof/block
    
    如果出现 Could not execute dot; may need to install graphviz.,就是提示你要安装 graphviz 了
    
    查看 PProf 可视化界面:
    http://localhost:8081/top
    http://localhost:8081/ui/
    
   go tool pprof -http=":8081"  ew-iepms.exe http://localhost:6060/debug/pprof/block
   
   ## CPU
   go tool pprof -http=":8081"  ew-iepms.exe http://localhost:6060/debug/pprof/profile
   ## heap 分析
   (2) go tool pprof -http=":8081"  ew-iepms.exe http://localhost:6060/debug/pprof/heap
   ## block分析
   (3) go tool pprof -http=":8081"  ew-iepms.exe http://localhost:6060/debug/pprof/block
   ## 协程分析
   (4) go tool pprof -http=":8081"  ew-iepms.exe http://localhost:6060/debug/pprof/goroutine
   ## mutex 分析
   (5) go tool pprof -http=":8081"  ew-iepms.exe http://localhost:6060/debug/pprof/mutex

本文来自:简书

感谢作者:我是不会赢的

查看原文:Golang 大杀器之性能剖析 PProf

相关阅读 >>

Go 内嵌静态资源

slice

关于Golang Gopath的新用法

leetcode 搜索旋转排序数组 Golang

Go语言指向指针的指针

Golang rune几个字节

Golang解决中文乱码的方法

Golang如何导入包

Golang实现:树形结构返回所有菜单列表

Go - httpclient 常用操作

更多相关阅读请进入《Go》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...