2019独角兽企业重金招聘Python工程师标准>>>

golang里面本身自带内存分析,cpu分析,堆分配信息,线程使用情况,goroutine使用情况.这些分析包含在runtime/pprof这个包下面,本文参考的文献:Go性能监控/分析工具:go tool pprof

先看测试代码:

go func() {fcup, err := os.Create("pprofcpu.log")if err != nil {fmt.Println("can't create file")return}fmem, err := os.Create("pprofmem.log")if err != nil {fmt.Println("can't create file")return}fblock, err := os.Create("pprofblock.log")if err != nil {fmt.Println("can't create file")return}froutinue, err := os.Create("pprofroutinue.log")if err != nil {fmt.Println("can't create file")return}fthread, err := os.Create("pprofthread.log")if err != nil {fmt.Println("can't create file")return}fheap, err := os.Create("pprofheap.log")if err != nil {fmt.Println("can't create file")return}for {pprof.StartCPUProfile(fcup)runtime.MemProfileRate = 512 * 1024runtime.SetBlockProfileRate(1)time.Sleep(time.Second * 60)pprof.StopCPUProfile()pprof.WriteHeapProfile(fmem)pprof.Lookup("block").WriteTo(fblock, 2)pprof.Lookup("goroutine").WriteTo(froutinue, 2)pprof.Lookup("threadcreate").WriteTo(fthread, 2)pprof.Lookup("heap").WriteTo(fheap, 2)}}()

这个是关于分析工具部分的一段实现代码,主要是将cpu信息,heap信息,goroutinue信息,线程信息,块信息都保存到文件里面.这些接口使用"runtime/pprof"的就够了.在保持文件时,使用方法:WriteTo有两个参数,第一个参数是保存文件的句柄,第二个参数是保持信息的基本,可设置的值分别为0,1,2:

为 0 时,仅仅输出 pprof(程序)需要的十六进制地址

为 1 时,输出时增加函数名和行号,这样无需工具也可以阅读此 profile

为 2 时,并且当输出 goroutine profile 时,输出的 goroutine 栈的格式为未 recovered panic 时的格式

这样直接使用看起来不是很方便,go还提供了 web的形式,使用会更方便一些. 首先是需要载入包:_ "net/http/pprof"需要注意的是必须以这样的形式import, 因为这个包其实在带中没有用到,但是在运行的时候会用到,所以如果前面不带"_"的导入,会编译错误. 然后就是需要添加监听的端口:

pprofMux := http.DefaultServeMuxgo func() {http.ListenAndServe("localhost:8081", pprofMux)}()

如果你的项目本身就是go 的web项目,那么就是要添加两个监听的端口,有一个是独立运行专门用来显示分析数据的.当程序运行之后在浏览器里面输入如下: http://localhost:8081/debug/pprof

就可以看到想要的页面了:

/debug/pprof/Types of profiles available:
Count   Profile
78  allocs
0   block
0   cmdline
1043    goroutine
78  heap
0   mutex
0   profile
17  threadcreate
0   trace
full goroutine stack dump
Profile Descriptions:allocs: A sampling of all past memory allocations
block: Stack traces that led to blocking on synchronization primitives
cmdline: The command line invocation of the current program
goroutine: Stack traces of all current goroutines
heap: A sampling of memory allocations of live objects. You can specify the gc GET parameter to run GC before taking the heap sample.
mutex: Stack traces of holders of contended mutexes
profile: CPU profile. You can specify the duration in the seconds GET parameter. After you get the profile file, use the go tool pprof command to investigate the profile.
threadcreate: Stack traces that led to the creation of new OS threads
trace: A trace of execution of the current program. You can specify the duration in the seconds GET parameter. After you get the trace file, use the go tool trace command to investigate the trace.

转载于:https://my.oschina.net/u/1013544/blog/3029713

go中分析工具:pprof相关推荐

  1. Go性能分析工具pprof详解

    文章目录 一.什么是pprof 二.怎么使用pprof 1. 工具型应用 2. 服务型应用 三.`pprof`数据分析 CPU Profiling top指令排序展示 web指令生成图示 list指令 ...

  2. golang性能分析工具pprof介绍

    1 golang性能分析工具pprof介绍 文章目录 1 golang性能分析工具pprof介绍 1.1 pprof简介 1.2 pprof引入方法 1.3 使用pprof进行分析的方法 1.3.1 ...

  3. Go 学习笔记(81)— Go 性能分析工具 pprof

    Go 语言工具链中的 go pprof 可以帮助开发者快速分析及定位各种性能问题,如 CPU消耗 .内存分配及阻塞分析 .具体作用如下: 性能分析首先需要使用 runtime.pprof 包嵌入到待分 ...

  4. go性能分析工具pprof

    pprof的用途 CPU Profiling:CPU 分析,按照一定的频率采集所监听的应用程序 CPU(含寄存器)的使用情况,可确定应用程序在主动消耗CPU 周期时花费时间的位置 Memory Pro ...

  5. gbd 分析core文件_Go 性能分析工具 pprof 入门

    (给Go开发大全加星标) 来源:wudaijun https://wudaijun.com/2018/04/go-pprof/ [导读]pprof是golang用于性能分析的工具.可以生成图形和文本报 ...

  6. Go性能调优及相关工具使用(四)——性能调优工具pprof的使用

    文章目录 一.本次学习重点内容: 本堂课的知识要点有哪些? 1.性能发现工具pprof 2.性能调优案例 二.详细知识点介绍: 1.性能调优简介 性能调优原则: 2.性能发现工具pprof 说明: p ...

  7. Golang内存分析工具gctrace和pprof实战

    目录 gctrace 参数说明 举例分析 补充说明 pprof go tool pprof分析工具 参考 gctrace gctrace用途主要是用于跟踪GC的不同阶段的耗时与GC前后的内存量对比. ...

  8. go内存分析工具介绍--pprof

    本人小白,刚开始接触go就遇到了一个内存问题,在进行内存分析的时候发现了一下比较好的工具,在此留下记录. 废话不多说,直接开整. 什么是pprof:pprof是Go的性能分析工具,在程序运行过程中,可 ...

  9. ArcGIS10.6中,在3D分析工具中创建视线之后,怎么将其删除?

    如下图所示, 为3D分析工具条: 在3D分析中创建的线,例如做剖面图是插入的线,该怎样删除呢? 如下图所示. 删除方法是,先使用如下选择工具,然后双击线条,再按delete键.

  10. oracle 内存分析工具,IDE 中的分析工具

    IDE 中的分析工具 Oracle Solaris Studio IDE 提供的交互式图形分析工具可用于检查在 IDE 内部运行的项目的性能.分析工具使用 Oracle Solaris Studio ...

最新文章

  1. 60幅精美绝伦的绘景(Matte Paintings)作品欣赏(上篇)
  2. GNOME桌面的安装
  3. 在DataGrid中將RowHeader 加上文字...
  4. 如何在WhatsApp中将群聊静音
  5. SpringBoot +Lombok注解精华篇
  6. java 枚举的实现原理
  7. 数据库的运维策略脚本篇(内附脚本,无私分享)
  8. 论文赏析[ACL18]一个句子向量表示究竟可以塞进多少语言性质?
  9. 早期预警系统的组成要素
  10. d3.js——饼状图
  11. MyBatis源码阅读(五) ---Mapper接口的获取过程
  12. Google亲儿子 Nexus/Pixel 手机刷机Root之旅
  13. c语言酒店管理系统,基于C#的酒店管理系统(V3.1)最新版
  14. Eclipse下找不到“新建Web项目”
  15. Iframe的allow属性生效时机
  16. Linux虚拟机克隆后网络冲突解决办法
  17. 用计算机怎么弹学猫叫,抖音我们一起学猫叫计算器谱分享
  18. 手段-目的理论定性研究实践经验分享
  19. mysql 设置密码出现ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
  20. ss-libev 分析

热门文章

  1. ip xfrm命令是做什么的?
  2. VisualStudioCode常用快捷键
  3. 【TFS 2017 CI/CD系列 - 01】-- Agent篇
  4. 自动化测试 短信验证登录
  5. JS 日期工具类-基于yDate
  6. Oracle数据把持和控制言语详解-1
  7. ASIHTTPRequest框架进行文件下载
  8. 2010年一些零散的总结4
  9. 谁偷偷删了你的微信?别慌!一篇Python学习教程帮你都揪出来
  10. os模块,os.path模块,subprocess模块,configparser模块,shutil模块