Golang pprof 性能分析与火焰图
下载完之后安装,安装完成之后将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:
启动程序之后会看到很多自动添加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>在新的命令行窗口
<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>在浏览器中能看到如下的性能分析图:
火焰图:
在命令行下可以看到性能数据
使用 <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>- [1] gin-contrib/pprof
- [2] pprof
您可能感兴趣的文章:
Golang pprof 性能分析与火焰图
Golang火焰图
photoshop自带滤镜做出火焰效果文字制作教程
Go 代码调优利器 - 火焰图
Golang 踩坑pprof分析
photoshop设计燃烧金属火焰字效果制作教程
Golang-性能监控及调优
photoshop用钢笔工具绘制漂亮火焰效果制作教程
photoshop制作火焰人像效果后期教程
photoshop制作金属燃烧的火焰字效果教程