<p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px;">Go语言自带了强大的性能测试工具pprof,位于 net/http 包下的 pprof。 <a href="https://golang.org/pkg/net/http/pprof/" style="box-sizing: border-box; color: rgb(51, 202, 187); text-decoration-line: none; background: transparent; transition: all 0.3s linear 0s; outline: none !important;">官方文档</a></p><h3 style="box-sizing: border-box; margin-top: 0.3em; margin-bottom: 16px; font-weight: 300; line-height: 1.43; font-size: 1.5em; font-family: Raleway, 微軟正黑體, "Helvetica Neue", Helvetica, Arial, sans-serif; letter-spacing: 0.5px; position: relative;"><a class="reference-link" style="box-sizing: border-box; color: rgb(51, 202, 187); background: transparent; transition: all 0.3s linear 0s; outline: none !important;"></a><strong style="box-sizing: border-box;">PProf 关注的模块</strong></h3><ul style="box-sizing: border-box; margin-bottom: 16px; padding: 0px 0px 0px 2em;" class=" list-paddingleft-2"><li><p>CPU profile:报告程序的 CPU 使用情况,按照一定频率去采集应用程序在 CPU 和寄存器上面的数据</p></li><li><p>Memory Profile(Heap Profile):报告程序的内存使用情况</p></li><li><p>Block Profiling:报告 goroutines 不在运行状态的情况,可以用来分析和查找死锁等性能瓶颈</p></li><li><p>Goroutine Profiling:报告 goroutines 的使用情况,有哪些 goroutine,它们的调用关系是怎样的</p></li></ul><h3 style="box-sizing: border-box; margin-top: 0.3em; margin-bottom: 16px; font-weight: 300; line-height: 1.43; font-size: 1.5em; font-family: Raleway, 微軟正黑體, "Helvetica Neue", Helvetica, Arial, sans-serif; letter-spacing: 0.5px; position: relative;"><a class="reference-link" style="box-sizing: border-box; color: rgb(51, 202, 187); background: transparent; transition: all 0.3s linear 0s; outline: none !important;"></a>PProf 使用方式</h3><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px;">PProf 主要涉及两个包:”net/http/pprof”、”runtime/pprof”。</p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px;">net/http/pprof:只是使用runtime/pprof包来进行封装了一下,并在http端口上暴露出来。使用 net/http/pprof 可以做到直接看到当前 web 服务的状态,包括 CPU 占用情况和内存使用情况等。如果服务是一直运行的,如 web 应用,可以很方便的使用第一种方式 <code style="box-sizing: border-box; font-family: "YaHei Consolas Hybrid", Consolas, "Meiryo UI", "Malgun Gothic", "Segoe UI", "Trebuchet MS", Helvetica, monospace, monospace; font-size: 14px; color: rgb(232, 62, 140); overflow-wrap: break-word; padding: 3px; margin: 0px 4px 0px 5px; background: rgb(246, 246, 246); border-radius: 3px; border: 1px solid rgb(238, 238, 238);">import "net/http/pprof"</code>。</p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px;">runtime/pprof:可以用来产生 dump 文件,再使用 Go Tool PProf 来分析这运行日志。</p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px;">引入示例:</p><pre class="prettyprint linenums prettyprinted" style="box-sizing: border-box; font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-alternates: normal; font-kerning: auto; font-optical-sizing: auto; font-feature-settings: normal; font-variation-settings: normal; font-variant-position: normal; font-stretch: normal; font-size: 13.6px; line-height: 1.6; font-family: "YaHei Consolas Hybrid", Consolas, "Meiryo UI", "Malgun Gothic", "Segoe UI", "Trebuchet MS", Helvetica, monospace, monospace; margin-top: 0px; margin-bottom: 16px; overflow: auto; color: rgb(47, 111, 159); background-color: rgb(246, 246, 246); border: 1px solid rgb(238, 238, 238); padding: 10px; border-radius: 3px; overflow-wrap: break-word; text-wrap: wrap;">#web服务器:import ( "net/http" _ "net/http/pprof")#一般应用程序(实际应用无web交互)import ( "net/http" _ "runtime/pprof")</pre><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px;">下面主要介绍web收集PProf信息方式</p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px;">使用示例:</p><pre class="prettyprint linenums prettyprinted" style="box-sizing: border-box; font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-alternates: normal; font-kerning: auto; font-optical-sizing: auto; font-feature-settings: normal; font-variation-settings: normal; font-variant-position: normal; font-stretch: normal; font-size: 13.6px; line-height: 1.6; font-family: "YaHei Consolas Hybrid", Consolas, "Meiryo UI", "Malgun Gothic", "Segoe UI", "Trebuchet MS", Helvetica, monospace, monospace; margin-top: 0px; margin-bottom: 16px; overflow: auto; color: rgb(47, 111, 159); background-color: rgb(246, 246, 246); border: 1px solid rgb(238, 238, 238); padding: 10px; border-radius: 3px; overflow-wrap: break-word; text-wrap: wrap;">package mainimport ( "fmt" "log" "net/http" _ "net/http/pprof" "time")func sayHelloHandler(w http.ResponseWriter, r *http.Request) { hellowold(10000) fmt.Println("path", r.URL.Path) fmt.Println("scheme", r.URL.Scheme) fmt.Fprintf(w, "Hello world!\n") //这个写入到w的是输出到客户端的}func main() { http.HandleFunc("/", sayHelloHandler) // 设置访问路由 log.Fatal(http.ListenAndServe(":8080", nil))}func hellowold(times int) { time.Sleep(time.Second) var counter int for i := 0; i < times; i++ { for j := 0; j < times; j++ { counter++ } }}</pre><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px;">访问:<a href="http://127.0.0.1:8080/debug/pprof/" style="box-sizing: border-box; color: rgb(51, 202, 187); text-decoration-line: none; background: transparent; transition: all 0.3s linear 0s; outline: none !important;">http://127.0.0.1:8080/debug/pprof/</a></p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px;">输出:</p><pre class="prettyprint linenums prettyprinted" style="box-sizing: border-box; font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-alternates: normal; font-kerning: auto; font-optical-sizing: auto; font-feature-settings: normal; font-variation-settings: normal; font-variant-position: normal; font-stretch: normal; font-size: 13.6px; line-height: 1.6; font-family: "YaHei Consolas Hybrid", Consolas, "Meiryo UI", "Malgun Gothic", "Segoe UI", "Trebuchet MS", Helvetica, monospace, monospace; margin-top: 0px; margin-bottom: 16px; overflow: auto; color: rgb(47, 111, 159); background-color: rgb(246, 246, 246); border: 1px solid rgb(238, 238, 238); padding: 10px; border-radius: 3px; overflow-wrap: break-word; text-wrap: wrap;">/debug/pprof/profiles:0 block4 goroutine2 heap0 mutex5 threadcreatefull goroutine stack dump</pre><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px;">这个路径下还有几个子页面:</p><ul style="box-sizing: border-box; margin-bottom: 16px; padding: 0px 0px 0px 2em;" class=" list-paddingleft-2"><li><p style="box-sizing: border-box; margin-top: 16px; margin-bottom: 16px;"><code style="box-sizing: border-box; font-family: "YaHei Consolas Hybrid", Consolas, "Meiryo UI", "Malgun Gothic", "Segoe UI", "Trebuchet MS", Helvetica, monospace, monospace; font-size: 14px; color: rgb(232, 62, 140); overflow-wrap: break-word; padding: 3px; margin: 0px 4px 0px 5px; background: rgb(246, 246, 246); border-radius: 3px; border: 1px solid rgb(238, 238, 238);">/debug/pprof/profile</code>:访问这个链接会自动进行 CPU profiling,持续 30s,并生成一个文件供下载。</p></li><li><p style="box-sizing: border-box; margin-top: 16px; margin-bottom: 16px;"><code style="box-sizing: border-box; font-family: "YaHei Consolas Hybrid", Consolas, "Meiryo UI", "Malgun Gothic", "Segoe UI", "Trebuchet MS", Helvetica, monospace, monospace; font-size: 14px; color: rgb(232, 62, 140); overflow-wrap: break-word; padding: 3px; margin: 0px 4px 0px 5px; background: rgb(246, 246, 246); border-radius: 3px; border: 1px solid rgb(238, 238, 238);">/debug/pprof/cmdline</code> 获取程序启动时的命令及参数。</p></li><li><p style="box-sizing: border-box; margin-top: 16px; margin-bottom: 16px;"><code style="box-sizing: border-box; font-family: "YaHei Consolas Hybrid", Consolas, "Meiryo UI", "Malgun Gothic", "Segoe UI", "Trebuchet MS", Helvetica, monospace, monospace; font-size: 14px; color: rgb(232, 62, 140); overflow-wrap: break-word; padding: 3px; margin: 0px 4px 0px 5px; background: rgb(246, 246, 246); border-radius: 3px; border: 1px solid rgb(238, 238, 238);">/debug/pprof/symbol</code> 根据传入的程序计数器(PC)的值,获取对应的函数的名称信息,调用了runtime包的FuncForPC获取对应的函数信息。可以传入多个PC值,以加号作为连接符号,比如访问:<a href="http://127.0.0.1:8080/debug/pprof/symbol?0x4e667d+0x6ec770" style="box-sizing: border-box; color: rgb(51, 202, 187); text-decoration-line: none; background: transparent; transition: all 0.3s linear 0s; outline: none !important;">http://127.0.0.1:8080/debug/pprof/symbol?0x4e667d+0x6ec770</a>, 返回PC值与函数对应名称的信息。</p></li><li><p style="box-sizing: border-box; margin-top: 16px; margin-bottom: 16px;"><code style="box-sizing: border-box; font-family: "YaHei Consolas Hybrid", Consolas, "Meiryo UI", "Malgun Gothic", "Segoe UI", "Trebuchet MS", Helvetica, monospace, monospace; font-size: 14px; color: rgb(232, 62, 140); overflow-wrap: break-word; padding: 3px; margin: 0px 4px 0px 5px; background: rgb(246, 246, 246); border-radius: 3px; border: 1px solid rgb(238, 238, 238);">/debug/pprof/trace</code> 此接口用于获取程序执行中的事件跟踪信息,比如协程、系统调用、GC、堆大小改变等事件,大多数事件的跟踪精确度能达到纳秒级别,后端调用了runtime包的StartTrace,会进行一个STW的操作。获取数据的时长默认为1秒,可以通过seconds参数进行修改。先通过以下命令获取数据:</p><p style="box-sizing: border-box; margin-top: 16px; margin-bottom: 16px;">wget <a href="http://127.0.0.1:8080/debug/pprof/trace?seconds=10" style="box-sizing: border-box; color: rgb(51, 202, 187); text-decoration-line: none; background: transparent; transition: all 0.3s linear 0s; outline: none !important;">http://127.0.0.1:8080/debug/pprof/trace?seconds=10</a> -O tracefile</p><p style="box-sizing: border-box; margin-top: 16px; margin-bottom: 16px;">然后执行go tool trace tacefile进行数据分析。</p></li><li><p style="box-sizing: border-box; margin-top: 16px; margin-bottom: 16px;"><code style="box-sizing: border-box; font-family: "YaHei Consolas Hybrid", Consolas, "Meiryo UI", "Malgun Gothic", "Segoe UI", "Trebuchet MS", Helvetica, monospace, monospace; font-size: 14px; color: rgb(232, 62, 140); overflow-wrap: break-word; padding: 3px; margin: 0px 4px 0px 5px; background: rgb(246, 246, 246); border-radius: 3px; border: 1px solid rgb(238, 238, 238);">/debug/pprof/block</code>:block Profiling 的路径。</p></li><li><p style="box-sizing: border-box; margin-top: 16px; margin-bottom: 16px;"><code style="box-sizing: border-box; font-family: "YaHei Consolas Hybrid", Consolas, "Meiryo UI", "Malgun Gothic", "Segoe UI", "Trebuchet MS", Helvetica, monospace, monospace; font-size: 14px; color: rgb(232, 62, 140); overflow-wrap: break-word; padding: 3px; margin: 0px 4px 0px 5px; background: rgb(246, 246, 246); border-radius: 3px; border: 1px solid rgb(238, 238, 238);">/debug/pprof/goroutines</code>:运行的 goroutines 列表,以及调用关系。</p></li><li><p style="box-sizing: border-box; margin-top: 16px; margin-bottom: 16px;"><code style="box-sizing: border-box; font-family: "YaHei Consolas Hybrid", Consolas, "Meiryo UI", "Malgun Gothic", "Segoe UI", "Trebuchet MS", Helvetica, monospace, monospace; font-size: 14px; color: rgb(232, 62, 140); overflow-wrap: break-word; padding: 3px; margin: 0px 4px 0px 5px; background: rgb(246, 246, 246); border-radius: 3px; border: 1px solid rgb(238, 238, 238);">/debug/pprof/heap</code>: Memory Profiling 的路径,访问这个链接会得到一个内存 Profiling 结果的文件。</p></li><li><p style="box-sizing: border-box; margin-top: 16px; margin-bottom: 16px;"><code style="box-sizing: border-box; font-family: "YaHei Consolas Hybrid", Consolas, "Meiryo UI", "Malgun Gothic", "Segoe UI", "Trebuchet MS", Helvetica, monospace, monospace; font-size: 14px; color: rgb(232, 62, 140); overflow-wrap: break-word; padding: 3px; margin: 0px 4px 0px 5px; background: rgb(246, 246, 246); border-radius: 3px; border: 1px solid rgb(238, 238, 238);">/debug/pprof/threadcreate</code>查看线程创建信息。</p></li></ul><h4 style="box-sizing: border-box; margin-top: 0.3em; margin-bottom: 16px; font-weight: 300; line-height: 1.4; font-size: 1.25em; font-family: Raleway, 微軟正黑體, "Helvetica Neue", Helvetica, Arial, sans-serif; letter-spacing: 0.5px; position: relative;"><a class="reference-link" style="box-sizing: border-box; color: rgb(51, 202, 187); background: transparent; transition: all 0.3s linear 0s; outline: none !important;"></a>Go tool pprof 分析工具(图形工具graphviz为例)</h4><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px;">Go Tool PProf 工具可以对以上链接下载的 prof 文件进行更详细的分析,可以生成调用关系图和火焰图。</p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px;">centos 7.5 下安装graphviz</p><pre class="prettyprint linenums prettyprinted" style="box-sizing: border-box; font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-alternates: normal; font-kerning: auto; font-optical-sizing: auto; font-feature-settings: normal; font-variation-settings: normal; font-variant-position: normal; font-stretch: normal; font-size: 13.6px; line-height: 1.6; font-family: "YaHei Consolas Hybrid", Consolas, "Meiryo UI", "Malgun Gothic", "Segoe UI", "Trebuchet MS", Helvetica, monospace, monospace; margin-top: 0px; margin-bottom: 16px; overflow: auto; color: rgb(47, 111, 159); background-color: rgb(246, 246, 246); border: 1px solid rgb(238, 238, 238); padding: 10px; border-radius: 3px; overflow-wrap: break-word; text-wrap: wrap;">yum install 'graphviz*' --skip-broken</pre><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px;">—skip-broken可选:跳过错误依赖,不加这个参数会提示安装包依赖错误,因为这里并不需要其它的安装包,所以跳过即可。</p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px;">其他系统安装方式可参考:<a href="http://www.graphviz.org/download/" style="box-sizing: border-box; color: rgb(51, 202, 187); text-decoration-line: none; background: transparent; transition: all 0.3s linear 0s; outline: none !important;">官网</a></p><h4 style="box-sizing: border-box; margin-top: 0.3em; margin-bottom: 16px; font-weight: 300; line-height: 1.4; font-size: 1.25em; font-family: Raleway, 微軟正黑體, "Helvetica Neue", Helvetica, Arial, sans-serif; letter-spacing: 0.5px; position: relative;"><a class="reference-link" style="box-sizing: border-box; color: rgb(51, 202, 187); background: transparent; transition: all 0.3s linear 0s; outline: none !important;"></a>Go tool pprof常用基本调试基本命令(默认30s采集时间,可通过—seconds)</h4><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px;">下面以cpu信息分析为例:</p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px;">通过执行</p><pre class="prettyprint linenums prettyprinted" style="box-sizing: border-box; font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-alternates: normal; font-kerning: auto; font-optical-sizing: auto; font-feature-settings: normal; font-variation-settings: normal; font-variant-position: normal; font-stretch: normal; font-size: 13.6px; line-height: 1.6; font-family: "YaHei Consolas Hybrid", Consolas, "Meiryo UI", "Malgun Gothic", "Segoe UI", "Trebuchet MS", Helvetica, monospace, monospace; margin-top: 0px; margin-bottom: 16px; overflow: auto; color: rgb(47, 111, 159); background-color: rgb(246, 246, 246); border: 1px solid rgb(238, 238, 238); padding: 10px; border-radius: 3px; overflow-wrap: break-word; text-wrap: wrap;">go tool pprof --text http://127.0.0.1:8080/debug/pprof/profile</pre><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px;">生成profile压缩gz文件(一般在/root/pprof目录下的.gz),选择文件进入交互,或执行一下命令(注意:执行的过程,访问一下<a href="http://localhost:8080/%EF%BC%8C%E8%BF%99%E6%A0%B7%E4%BC%9A%E5%BE%97%E5%88%B0%E7%9B%B8%E5%BA%94%E7%9A%84%E7%9B%91%E6%B5%8B%E6%95%B0%E6%8D%AE%EF%BC%89" style="box-sizing: border-box; color: rgb(51, 202, 187); text-decoration-line: none; background: transparent; transition: all 0.3s linear 0s; outline: none !important;">http://localhost:8080/,这样会得到相应的监测数据)</a></p><pre class="prettyprint linenums prettyprinted" style="box-sizing: border-box; font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-alternates: normal; font-kerning: auto; font-optical-sizing: auto; font-feature-settings: normal; font-variation-settings: normal; font-variant-position: normal; font-stretch: normal; font-size: 13.6px; line-height: 1.6; font-family: "YaHei Consolas Hybrid", Consolas, "Meiryo UI", "Malgun Gothic", "Segoe UI", "Trebuchet MS", Helvetica, monospace, monospace; margin-top: 0px; margin-bottom: 16px; overflow: auto; color: rgb(47, 111, 159); background-color: rgb(246, 246, 246); border: 1px solid rgb(238, 238, 238); padding: 10px; border-radius: 3px; overflow-wrap: break-word; text-wrap: wrap;">go tool pprof http://localhost:8080/debug/pprof/profileFetching profile over HTTP from http://localhost:8080/debug/pprof/profileSaved profile in /root/pprof/pprof.main.samples.cpu.002.pb.gzFile: mainType: cpuTime: Dec 14, 2018 at 3:26pm (CST)Duration: 30s, Total samples = 60ms ( 0.2%)Entering interactive mode (type "help" for commands, "o" for options)(pprof)</pre><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px;">输入top</p><pre class="prettyprint linenums prettyprinted" style="box-sizing: border-box; font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-alternates: normal; font-kerning: auto; font-optical-sizing: auto; font-feature-settings: normal; font-variation-settings: normal; font-variant-position: normal; font-stretch: normal; font-size: 13.6px; line-height: 1.6; font-family: "YaHei Consolas Hybrid", Consolas, "Meiryo UI", "Malgun Gothic", "Segoe UI", "Trebuchet MS", Helvetica, monospace, monospace; margin-top: 0px; margin-bottom: 16px; overflow: auto; color: rgb(47, 111, 159); background-color: rgb(246, 246, 246); border: 1px solid rgb(238, 238, 238); padding: 10px; border-radius: 3px; overflow-wrap: break-word; text-wrap: wrap;">(pprof) topShowing nodes accounting for 60ms, 100% of 60ms total flat flat% sum% cum cum% 60ms 100% 100% 60ms 100% main.hellowold /home/go/src/example/main.go 0 0% 100% 60ms 100% main.sayHelloHandler /home/go/src/example/main.go 0 0% 100% 60ms 100% net/http.(*ServeMux).ServeHTTP /usr/lib/golang/src/net/http/server.go 0 0% 100% 60ms 100% net/http.(*conn).serve /usr/lib/golang/src/net/http/server.go 0 0% 100% 60ms 100% net/http.HandlerFunc.ServeHTTP /usr/lib/golang/src/net/http/server.go 0 0% 100% 60ms 100% net/http.serverHandler.ServeHTTP /usr/lib/golang/src/net/http/server.go(pprof)</pre><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px;">可以看到 main.hellowold 是用cpu最多。</p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px;">输入 web #生成调用关系图</p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px;"><img src="https://www.maxiaoke.com/uploads/images/20231219/ccc573df4f6394fee008e185fef7e1af.png" alt=""/></p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px;">如查看历史调试文件信息,通过指定的profile文件进入(pprof)即可:</p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px;">例如:</p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px;">go tool pprof /root/pprof/pprof.main.samples.cpu.001.pb.gz<br/>File: main<br/>Type: cpu<br/>Time: Dec 14, 2018 at 11:37am (CST)<br/>Duration: 29.99s, Total samples = 0<br/>Entering interactive mode (type “help” for commands, “o” for options)<br/>(pprof)</p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px;">如果想查看pprof其他命令,可输入help。</p><h2 style="box-sizing: border-box; margin-top: 0.3em; margin-bottom: 1em; font-weight: 300; line-height: 1.225; font-size: 1.75em; font-family: Raleway, 微軟正黑體, "Helvetica Neue", Helvetica, Arial, sans-serif; letter-spacing: 0.5px; position: relative; padding-bottom: 0.5em; border-bottom: 1px solid rgb(238, 238, 238);"><a class="reference-link" style="box-sizing: border-box; color: rgb(51, 202, 187); background: transparent; transition: all 0.3s linear 0s; outline: none !important;"></a><strong style="box-sizing: border-box;">生成火焰图</strong> (官方:<a href="https://github.com/uber/go-torch%EF%BC%89" style="box-sizing: border-box; color: rgb(51, 202, 187); text-decoration-line: none; background: transparent; transition: all 0.3s linear 0s; outline: none !important;">https://github.com/uber/go-torch)</a></h2><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px;">从Go 1.11开始,火焰图形可视化<code style="box-sizing: border-box; font-family: "YaHei Consolas Hybrid", Consolas, "Meiryo UI", "Malgun Gothic", "Segoe UI", "Trebuchet MS", Helvetica, monospace, monospace; font-size: 14px; color: rgb(232, 62, 140); overflow-wrap: break-word; padding: 3px; margin: 0px 4px 0px 5px; background: rgb(246, 246, 246); border-radius: 3px; border: 1px solid rgb(238, 238, 238);">go tool pprof</code>直接可用!</p><pre class="prettyprint linenums prettyprinted" style="box-sizing: border-box; font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-alternates: normal; font-kerning: auto; font-optical-sizing: auto; font-feature-settings: normal; font-variation-settings: normal; font-variant-position: normal; font-stretch: normal; font-size: 13.6px; line-height: 1.6; font-family: "YaHei Consolas Hybrid", Consolas, "Meiryo UI", "Malgun Gothic", "Segoe UI", "Trebuchet MS", Helvetica, monospace, monospace; margin-top: 0px; margin-bottom: 16px; overflow: auto; color: rgb(47, 111, 159); background-color: rgb(246, 246, 246); border: 1px solid rgb(238, 238, 238); padding: 10px; border-radius: 3px; overflow-wrap: break-word; text-wrap: wrap;"># This will listen on :8081 and open a browser.# Change :8081 to a port of your choice.$ go tool pprof -http=":8081" [binary] [profile]</pre><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px;">如果你不能使用Go 1.11,你可以获得最新的<code style="box-sizing: border-box; font-family: "YaHei Consolas Hybrid", Consolas, "Meiryo UI", "Malgun Gothic", "Segoe UI", "Trebuchet MS", Helvetica, monospace, monospace; font-size: 14px; color: rgb(232, 62, 140); overflow-wrap: break-word; padding: 3px; margin: 0px 4px 0px 5px; background: rgb(246, 246, 246); border-radius: 3px; border: 1px solid rgb(238, 238, 238);">pprof</code>工具并使用它:</p><pre class="prettyprint linenums prettyprinted" style="box-sizing: border-box; font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-alternates: normal; font-kerning: auto; font-optical-sizing: auto; font-feature-settings: normal; font-variation-settings: normal; font-variant-position: normal; font-stretch: normal; font-size: 13.6px; line-height: 1.6; font-family: "YaHei Consolas Hybrid", Consolas, "Meiryo UI", "Malgun Gothic", "Segoe UI", "Trebuchet MS", Helvetica, monospace, monospace; margin-top: 0px; margin-bottom: 16px; overflow: auto; color: rgb(47, 111, 159); background-color: rgb(246, 246, 246); border: 1px solid rgb(238, 238, 238); padding: 10px; border-radius: 3px; overflow-wrap: break-word; text-wrap: wrap;"># Get the pprof tool directly$ go get -u github.com/google/pprof$ pprof -http=":8081" [binary] [profile]</pre><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px;">我的golang 版本 v 1.11,所以只执行以下命令:(注意:在执行以下命令过程,访问一下<a href="http://localhost:8080/%EF%BC%8C%E6%B6%88%E8%80%97%E4%BB%A5%E4%B8%8B%E6%80%A7%E8%83%BD%EF%BC%89" style="box-sizing: border-box; color: rgb(51, 202, 187); text-decoration-line: none; background: transparent; transition: all 0.3s linear 0s; outline: none !important;">http://localhost:8080/,消耗以下性能)</a></p><pre class="prettyprint linenums prettyprinted" style="box-sizing: border-box; font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-alternates: normal; font-kerning: auto; font-optical-sizing: auto; font-feature-settings: normal; font-variation-settings: normal; font-variant-position: normal; font-stretch: normal; font-size: 13.6px; line-height: 1.6; font-family: "YaHei Consolas Hybrid", Consolas, "Meiryo UI", "Malgun Gothic", "Segoe UI", "Trebuchet MS", Helvetica, monospace, monospace; margin-top: 0px; margin-bottom: 16px; overflow: auto; color: rgb(47, 111, 159); background-color: rgb(246, 246, 246); border: 1px solid rgb(238, 238, 238); padding: 10px; border-radius: 3px; overflow-wrap: break-word; text-wrap: wrap;">go tool pprof -http=":8081" http://localhost:8080/debug/pprof/profileFetching profile over HTTP from http://localhost:8080/debug/pprof/profileSaved profile in /root/pprof/pprof.main.samples.cpu.007.pb.gz</pre><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px;">过一会儿会产生个web窗口</p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px;"><img src="https://www.maxiaoke.com/uploads/images/20231219/ccefa2f92eb454206709ee9752ca1b4e.png" alt=""/></p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px;">选择 VIEW->Flame Graph 得到火焰图形</p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 0px !important;"><img src="https://www.maxiaoke.com/uploads/images/20231219/9455da423a193850dc38c55ebdaf1a19.png" alt=""/></p><p>上</p><p><br/></p>