教程集 www.jiaochengji.com
教程集 >  Golang编程  >  golang教程  >  正文 Golang pprof 性能分析与火焰图

Golang pprof 性能分析与火焰图

发布时间:2022-03-01   编辑:jiaochengji.com
教程集为您提供Golang pprof 性能分析与火焰图等资源,欢迎您收藏本站,我们将为您提供最新的Golang pprof 性能分析与火焰图资源
<h3>1. 安装graphviz</h3> <h4>1.1 下载 graphviz (windows 环境)</h4> <pre><code class="lang-shell hljs">https://graphviz.gitlab.io/_pages/Download/windows/graphviz-2.38.msi </code></code></pre>

下载完之后安装,安装完成之后将graphviz的安装bin目录加入环境变量中

<pre><code class="lang-shell hljs"># 例如 C:\Program Files (x86)\Graphviz2.38\bin </code></code></pre> <h4>1.2 测试graphviz是否安装成功</h4> <pre><code class="lang-shell hljs">dot -version dot - graphviz version 2.38.0 (20140413.2041) libdir = "C:\Program Files (x86)\Graphviz2.38\bin" Activated plugin library: gvplugin_dot_layout.dll Using layout: dot:dot_layout Activated plugin library: gvplugin_core.dll Using render: dot:core Using device: dot:dot:core The plugin configuration file: C:\Program Files (x86)\Graphviz2.38\bin\config6 was successfully loaded. render : cairo dot fig gd gdiplus map pic pov ps svg tk vml vrml xdot layout : circo dot fdp neato nop nop1 nop2 osage patchwork sfdp twopi textlayout : textlayout device : bmp canon cmap cmapx cmapx_np dot emf emfplus eps fig gd gd2 gif gv imap imap_np ismap jpe jpeg jpg metafile pdf pic plain plain-ext png pov ps ps2 svg svgz tif tiff tk vml vmlz vrml wbmp xdot xdot1.2 xdot1.4 loadimage : (lib) bmp eps gd gd2 gif jpe jpeg jpg png ps svg </code></code></pre> <h3>2. 使用pprof</h3> <h4>2.1 修改代码</h4>

<code>main.go</code>

<pre><code class="lang-go hljs">import "net/http" import _ "net/http/pprof" func main() { http.ListenAndServe("0.0.0.0:8080", nil) } </code></code></pre>

启动程序

<code>go run main.go</code>

<pre><code class="lang-shell hljs"># 通过浏览器访问 http://127.0.0.1:8080/debug/pprof/ # 访问该地址将看到性能分析界面 </code></code></pre> <h4>2.2 火焰图生成</h4>

在命令行下执行

<pre><code class="lang-shell hljs">go tool pprof -http=:1234 http://localhost:8080/debug/pprof/profile # 执行上述命令稍微等会,浏览器上会出现一UI界面 VIEW->Flame Graph 看到火焰图 </code></code></pre> <h3>3. Gin框架使用pprof</h3> <h4>3.1 安装需要包</h4>

进入项目根目录,安装gin使用的pprof包

<pre><code class="lang-shell hljs"> go get github.com/gin-contrib/pprof </code></code></pre>

在项目中使用pprof
example:

<pre><code class="lang-shell hljs">package main import ( "github.com/gin-contrib/pprof" "github.com/gin-gonic/gin" ) func main() { router := gin.Default() pprof.Register(router) router.Run(":8080") } </code></code></pre> <h4>3.2 启动程序</h4> <blockquote>

启动程序之后会看到很多自动添加pprof 相关的接口

通过这些接口我们可以进行分析

</blockquote> <pre><code class="lang-shell hljs">go run main.go [GIN-debug] [WARNING] Creating an Engine instance with the Logger and Recovery middleware already attached. [GIN-debug] [WARNING] Running in "debug" mode. Switch to "release" mode in production. - using env: export GIN_MODE=release - using code: gin.SetMode(gin.ReleaseMode) [GIN-debug] GET /debug/pprof/ --> github.com/gin-contrib/pprof.pprofHandler.func1 (3 handlers) [GIN-debug] GET /debug/pprof/cmdline --> github.com/gin-contrib/pprof.pprofHandler.func1 (3 handlers) [GIN-debug] GET /debug/pprof/profile --> github.com/gin-contrib/pprof.pprofHandler.func1 (3 handlers) [GIN-debug] POST /debug/pprof/symbol --> github.com/gin-contrib/pprof.pprofHandler.func1 (3 handlers) [GIN-debug] GET /debug/pprof/symbol --> github.com/gin-contrib/pprof.pprofHandler.func1 (3 handlers) [GIN-debug] GET /debug/pprof/trace --> github.com/gin-contrib/pprof.pprofHandler.func1 (3 handlers) [GIN-debug] GET /debug/pprof/allocs --> github.com/gin-contrib/pprof.pprofHandler.func1 (3 handlers) [GIN-debug] GET /debug/pprof/block --> github.com/gin-contrib/pprof.pprofHandler.func1 (3 handlers) [GIN-debug] GET /debug/pprof/goroutine --> github.com/gin-contrib/pprof.pprofHandler.func1 (3 handlers) [GIN-debug] GET /debug/pprof/heap --> github.com/gin-contrib/pprof.pprofHandler.func1 (3 handlers) [GIN-debug] GET /debug/pprof/mutex --> github.com/gin-contrib/pprof.pprofHandler.func1 (3 handlers) [GIN-debug] GET /debug/pprof/threadcreate --> github.com/gin-contrib/pprof.pprofHandler.func1 (3 handlers) ... [GIN-debug] Environment variable PORT is undefined. Using port :8080 by default [GIN-debug] Listening and serving HTTP on :8080 </code></code></pre>

通过浏览器访问

<pre><code class="lang-shell hljs">http://127.0.0.1:8080/debug/pprof/ </code></code></pre>
pprof1.png
<pre><code class="lang-shell hljs">allocs :过去所有内存分配的抽样 block :导致在同步基元上阻塞的堆栈跟踪 cmdline :当前程序的命令行调用 goroutine :goroutine堆栈跟踪 heap :活动对象的内存分配的采样 mutex :争用互斥锁的持有者的堆栈跟踪 profile :CPU摘要信息。您可以在seconds GET参数中指定持续时间。获得概要文件之后,使用go工具pprof命令来调查该概要文件。 threadcreate : 操作系统线程堆栈跟踪 trace : 当前程序的执行轨迹。您可以在seconds GET参数中指定持续时间。获得跟踪文件后,使用go工具跟踪命令来调查跟踪。 </code></code></pre> <h4>3.3 火焰图</h4>

在新的命令行窗口

<pre><code class="lang-shell hljs"># 执行命令后,会在浏览器打开一个窗口 go tool pprof -http=:1234 http://localhost:8080/debug/pprof/goroutine # 简单解释 # -http 表示使用交互式web接口查看获取的性能信息,指定可用的端口即可 # debug/pprof/需要查看的指标 (allocs,block,goroutine,heap...) </code></code></pre>

在浏览器中能看到如下的性能分析图:

pprof2.png

火焰图:

pprof3.png
<h4>3.4 pprof命令行</h4> <blockquote>

在命令行下可以看到性能数据

使用 <code>go tool pprof</code> 数据源

example :

go tool pprof http://127.0.0.1:8080/debug/pprof/allocs

</blockquote>

命令行下执行 ,常用的命令有top,tree,web等,通过help命令查看更多

<pre><code class="lang-shell hljs">PS G:\> go tool pprof http://127.0.0.1:8080/debug/pprof/allocs Fetching profile over HTTP from http://127.0.0.1:8080/debug/pprof/allocs Saved profile in C:\Users\captain\pprof\pprof.alloc_objects.alloc_space.inuse_objects.inuse_space.008.pb.gz Type: alloc_space Time: Sep 19, 2020 at 3:53pm (CST) Entering interactive mode (type "help" for commands, "o" for options) (pprof) </code></code></pre> <pre><code class="lang-shell hljs"># help 命令可以查看所有命令 # top [n] 查看性能指标数据 (pprof) top 5 Showing nodes accounting for 1026.66kB, 100% of 1026.66kB total Showing top 5 nodes out of 11 flat flat% sum% cum cum% 514.63kB 50.13% 50.13% 514.63kB 50.13% math/rand.NewSource 512.03kB 49.87% 100% 512.03kB 49.87% regexp/syntax.(*parser).maybeConcat 0 0% 100% 512.03kB 49.87% github.com/jinzhu/gorm.init.ializers 0 0% 100% 514.63kB 50.13% math/rand.init.ializers 0 0% 100% 512.03kB 49.87% regexp.Compile # tree [n] 以树形显示指标数据 (pprof) tree 5 Showing nodes accounting for 1026.66kB, 100% of 1026.66kB total Showing top 5 nodes out of 11 ---------------------------------------------------------- ------------- flat flat% sum% cum cum% calls calls% context ---------------------------------------------------------- ------------- 514.63kB 100% | math/rand.init.ializers 514.63kB 50.13% 50.13% 514.63kB 50.13% | math/rand.NewSource ---------------------------------------------------------- ------------- 512.03kB 100% | regexp.Compile 512.03kB 49.87% 100% 512.03kB 49.87% | regexp/syntax.(*parser).maybeConcat ---------------------------------------------------------- ------------- 0 0% 100% 512.03kB 49.87% | github.com/jinzhu/gorm.init.ializers 512.03kB 100% | regexp.Compile ---------------------------------------------------------- ------------- 0 0% 100% 514.63kB 50.13% | math/rand.init.ializers 514.63kB 100% | math/rand.NewSource ---------------------------------------------------------- ------------- 512.03kB 100% | github.com/jinzhu/gorm.init.ializers 0 0% 100% 512.03kB 49.87% | regexp.Compile 512.03kB 100% | regexp/syntax.(*parser).maybeConcat ---------------------------------------------------------- ------------- # web命令是希望通过web形式(在浏览器器中看到),因为前面已经安装了graphviz,所以web可以使用 # web命令会直接打开浏览器 (pprof) web </code></code></pre>
pprof4.png
<h4>参考文档</h4>

- [1] gin-contrib/pprof
- [2] pprof

到此这篇关于“ Golang pprof 性能分析与火焰图”的文章就介绍到这了,更多文章或继续浏览下面的相关文章,希望大家以后多多支持JQ教程网!

您可能感兴趣的文章:
Golang pprof 性能分析与火焰图
Golang火焰图
photoshop自带滤镜做出火焰效果文字制作教程
Go 代码调优利器 - 火焰图
Golang 踩坑pprof分析
photoshop设计燃烧金属火焰字效果制作教程
Golang-性能监控及调优
photoshop用钢笔工具绘制漂亮火焰效果制作教程
photoshop制作火焰人像效果后期教程
photoshop制作金属燃烧的火焰字效果教程

[关闭]
~ ~