{"id":192,"date":"2026-07-02T10:13:20","date_gmt":"2026-07-02T02:13:20","guid":{"rendered":"https:\/\/yykcj.com\/?p=192"},"modified":"2026-07-02T10:13:20","modified_gmt":"2026-07-02T02:13:20","slug":"go-%e6%80%a7%e8%83%bd%e5%88%86%e6%9e%90%e4%b8%8e%e4%bc%98%e5%8c%96%ef%bc%9a%e4%bb%8e-pprof-%e5%88%b0%e5%ae%9e%e6%88%98%e8%b0%83%e4%bc%98","status":"publish","type":"post","link":"https:\/\/yykcj.com\/?p=192","title":{"rendered":"Go \u6027\u80fd\u5206\u6790\u4e0e\u4f18\u5316\uff1a\u4ece pprof \u5230\u5b9e\u6218\u8c03\u4f18"},"content":{"rendered":"<h2>\u4e3a\u4ec0\u4e48\u9700\u8981\u6027\u80fd\u5206\u6790\uff1f<\/h2>\n<p>\u5728 Go \u5f00\u53d1\u4e2d\uff0c\u6027\u80fd\u95ee\u9898\u5f80\u5f80\u9690\u85cf\u5728\u5e76\u53d1\u3001\u5185\u5b58\u5206\u914d\u548c I\/O \u64cd\u4f5c\u4e2d\u3002\u6ca1\u6709\u6570\u636e\u652f\u6491\u7684\u4f18\u5316\u662f\u76f2\u76ee\u7684\uff0c\u800c pprof \u662f Go \u5b98\u65b9\u63d0\u4f9b\u7684\u6027\u80fd\u5206\u6790\u5229\u5668\uff0c\u80fd\u5e2e\u52a9\u6211\u4eec\u7cbe\u51c6\u5b9a\u4f4d\u74f6\u9888\u3002\u672c\u6587\u5c06\u901a\u8fc7\u4e00\u4e2a\u6a21\u62df\u7684 HTTP \u670d\u52a1\uff0c\u5c55\u793a\u4ece\u5b89\u88c5\u3001\u91c7\u96c6\u3001\u5206\u6790\u5230\u4f18\u5316\u7684\u5b8c\u6574\u6d41\u7a0b\u3002<\/p>\n<h2>\u51c6\u5907\u5de5\u4f5c<\/h2>\n<p>\u786e\u4fdd\u4f60\u7684 Go \u7248\u672c >= 1.16\uff0c\u5e76\u5b89\u88c5 graphviz\uff08\u7528\u4e8e\u751f\u6210\u706b\u7130\u56fe\uff09\uff1a<\/p>\n<pre><code class=\"language-bash\">\n# macOS\nbrew install graphviz\n\n# Ubuntu\nsudo apt-get install graphviz\n<\/code><\/pre>\n<h2>\u7b2c\u4e00\u6b65\uff1a\u690d\u5165 pprof \u7aef\u70b9<\/h2>\n<p>\u5728 main.go \u4e2d\u5bfc\u5165\u5e76\u6ce8\u518c pprof \u8def\u7531\uff1a<\/p>\n<pre><code class=\"language-go\">\npackage main\n\nimport (\n\t&quot;log&quot;\n\t&quot;net\/http&quot;\n\t_ &quot;net\/http\/pprof&quot; \/\/ \u533f\u540d\u5bfc\u5165\uff0c\u81ea\u52a8\u6ce8\u518c \/debug\/pprof \u8def\u7531\n)\n\nfunc main() {\n\thttp.HandleFunc(&quot;\/work&quot;, workHandler)\n\tlog.Fatal(http.ListenAndServe(&quot;:8080&quot;, nil))\n}\n\nfunc workHandler(w http.ResponseWriter, r *http.Request) {\n\t\/\/ \u6a21\u62df\u8017\u65f6\u64cd\u4f5c\n\tresult := heavyComputation()\n\tw.Write([]byte(result))\n}\n\nfunc heavyComputation() string {\n\t\/\/ \u8fd9\u91cc\u5148\u5199\u4e00\u4e2a\u4f4e\u6548\u5b9e\u73b0\n\tsum := 0\n\tfor i := 0; i &lt; 100000; i++ {\n\t\tsum += i\n\t}\n\treturn fmt.Sprintf(&quot;sum: %d&quot;, sum)\n}\n<\/code><\/pre>\n<p>\u542f\u52a8\u670d\u52a1\uff1a<code>go run main.go<\/code><\/p>\n<h2>\u7b2c\u4e8c\u6b65\uff1a\u751f\u6210\u8d1f\u8f7d\u5e76\u91c7\u96c6\u6027\u80fd\u6570\u636e<\/h2>\n<p>\u4f7f\u7528 <code>go-wrk<\/code> \u6216 <code>ab<\/code> \u5de5\u5177\u6a21\u62df\u5e76\u53d1\u8bf7\u6c42\uff1a<\/p>\n<pre><code class=\"language-bash\">\n# \u5b89\u88c5 go-wrk\ngo install github.com\/tsliwowicz\/go-wrk@latest\n\n# \u53d1\u9001 10000 \u4e2a\u8bf7\u6c42\uff0c\u5e76\u53d1 10 \u4e2a\ngo-wrk -c 10 -n 10000 http:\/\/localhost:8080\/work\n<\/code><\/pre>\n<p>\u540c\u65f6\u5728\u53e6\u4e00\u4e2a\u7ec8\u7aef\u91c7\u96c6 CPU profile\uff0830 \u79d2\uff09\uff1a<\/p>\n<pre><code class=\"language-bash\">\ngo tool pprof -http=:8081 http:\/\/localhost:8080\/debug\/pprof\/profile?seconds=30\n<\/code><\/pre>\n<p>\u6267\u884c\u540e\u4f1a\u81ea\u52a8\u6253\u5f00\u6d4f\u89c8\u5668\u663e\u793a CPU \u5206\u6790\u7ed3\u679c\u3002<\/p>\n<h2>\u7b2c\u4e09\u6b65\uff1a\u5206\u6790\u706b\u7130\u56fe\u4e0e\u8c03\u7528\u56fe<\/h2>\n<p>\u5728 pprof \u7684 Web UI \u4e2d\uff0c\u91cd\u70b9\u5173\u6ce8\uff1a<\/p>\n<ul>\n<li><strong>\u706b\u7130\u56fe<\/strong>\uff1a\u67e5\u770b\u51fd\u6570\u8c03\u7528\u6808\u7684 CPU \u6d88\u8017\uff0c\u5bbd\u5ea6\u8d8a\u5bbd\u6d88\u8017\u8d8a\u5927\u3002<\/li>\n<li><strong>Top \u8868<\/strong>\uff1a\u6309 CPU \u5360\u7528\u6392\u5e8f\uff0c<code>flat<\/code> \u5217\u8868\u793a\u51fd\u6570\u81ea\u8eab\u6d88\u8017\uff0c<code>cum<\/code> \u5217\u5305\u542b\u5b50\u8c03\u7528\u3002<\/li>\n<li><strong>\u56fe<\/strong>\uff1a\u8282\u70b9\u8d8a\u5927\u3001\u8fb9\u8d8a\u7c97\u8bf4\u660e\u8017\u65f6\u8d8a\u591a\u3002<\/li>\n<\/ul>\n<p>\u5047\u8bbe\u6211\u4eec\u53d1\u73b0 <code>heavyComputation<\/code> \u5360\u7528\u4e86\u5927\u90e8\u5206 CPU\uff0c\u4f46\u5b9e\u9645\u4e1a\u52a1\u4e2d\u53ef\u80fd\u662f\u4e00\u4e2a\u5b57\u7b26\u4e32\u62fc\u63a5\u6216 JSON \u5e8f\u5217\u5316\u64cd\u4f5c\u3002<\/p>\n<h2>\u7b2c\u56db\u6b65\uff1a\u4f18\u5316\u4ee3\u7801<\/h2>\n<p>\u4f18\u5316\u524d\uff0c\u5148\u770b\u4e00\u4e2a\u66f4\u771f\u5b9e\u7684\u573a\u666f\uff1a\u5b57\u7b26\u4e32\u62fc\u63a5\u3002<\/p>\n<h3>\u4f4e\u6548\u7248\u672c\uff08\u4f7f\u7528 + \u62fc\u63a5\uff09<\/h3>\n<pre><code class=\"language-go\">\nfunc buildString(n int) string {\n\tresult := &quot;&quot;\n\tfor i := 0; i &lt; n; i++ {\n\t\tresult += fmt.Sprintf(&quot;%d&quot;, i) \/\/ \u6bcf\u6b21\u521b\u5efa\u65b0\u5b57\u7b26\u4e32\n\t}\n\treturn result\n}\n<\/code><\/pre>\n<h3>\u4f18\u5316\u7248\u672c\uff08\u4f7f\u7528 strings.Builder\uff09<\/h3>\n<pre><code class=\"language-go\">\nimport &quot;strings&quot;\n\nfunc buildStringOptimized(n int) string {\n\tvar sb strings.Builder\n\tsb.Grow(n * 10) \/\/ \u9884\u5206\u914d\u5185\u5b58\n\tfor i := 0; i &lt; n; i++ {\n\t\tsb.WriteString(fmt.Sprintf(&quot;%d&quot;, i))\n\t}\n\treturn sb.String()\n}\n<\/code><\/pre>\n<h3>\u6027\u80fd\u5bf9\u6bd4<\/h3>\n<pre><code class=\"language-bash\">\n# \u57fa\u51c6\u6d4b\u8bd5\u6587\u4ef6 benchmark_test.go\nfunc BenchmarkBuildString(b *testing.B) {\n\tfor i := 0; i &lt; b.N; i++ {\n\t\tbuildString(1000)\n\t}\n}\n\nfunc BenchmarkBuildStringOptimized(b *testing.B) {\n\tfor i := 0; i &lt; b.N; i++ {\n\t\tbuildStringOptimized(1000)\n\t}\n}\n<\/code><\/pre>\n<p>\u8fd0\u884c <code>go test -bench=. -benchmem<\/code>\uff0c\u4f18\u5316\u7248\u672c\u901a\u5e38\u5feb 10 \u500d\u4ee5\u4e0a\uff0c\u5185\u5b58\u5206\u914d\u51cf\u5c11 90%\u3002<\/p>\n<h2>\u7b2c\u4e94\u6b65\uff1a\u5185\u5b58\u5206\u6790<\/h2>\n<p>\u540c\u6837\u91c7\u96c6 heap \u5feb\u7167\uff1a<\/p>\n<pre><code class=\"language-bash\">\ngo tool pprof -http=:8082 http:\/\/localhost:8080\/debug\/pprof\/heap\n<\/code><\/pre>\n<p>\u5728 <code>View -> Flame Graph<\/code> \u4e2d\u67e5\u770b\u5185\u5b58\u5206\u914d\u3002\u5e38\u89c1\u95ee\u9898\uff1a<\/p>\n<ul>\n<li>\u4e34\u65f6\u5bf9\u8c61\u8fc7\u591a\uff08\u9ad8 <code>alloc_objects<\/code>\uff09<\/li>\n<li>\u5185\u5b58\u6cc4\u6f0f\uff08<code>inuse_space<\/code> \u6301\u7eed\u589e\u957f\uff09<\/li>\n<\/ul>\n<h3>\u5185\u5b58\u6cc4\u6f0f\u793a\u4f8b\u4e0e\u4fee\u590d<\/h3>\n<pre><code class=\"language-go\">\n\/\/ \u9519\u8bef\uff1a\u5168\u5c40\u7f13\u5b58\u53ea\u589e\u4e0d\u51cf\nvar cache = make(map[string]*BigStruct)\n\nfunc leakHandler(w http.ResponseWriter, r *http.Request) {\n\tkey := r.URL.Query().Get(&quot;key&quot;)\n\tcache[key] = NewBigStruct() \/\/ \u6c38\u8fdc\u4e0d\u5220\u9664\n}\n\n\/\/ \u4fee\u590d\uff1a\u4f7f\u7528 LRU \u7f13\u5b58\u6216\u5b9a\u671f\u6e05\u7406\n<\/code><\/pre>\n<h2>\u7b2c\u516d\u6b65\uff1agoroutine \u4e0e\u963b\u585e\u5206\u6790<\/h2>\n<p>\u91c7\u96c6 goroutine \u548c\u963b\u585e profile\uff1a<\/p>\n<pre><code class=\"language-bash\">\ngo tool pprof http:\/\/localhost:8080\/debug\/pprof\/goroutine\ngo tool pprof http:\/\/localhost:8080\/debug\/pprof\/block\n<\/code><\/pre>\n<p>\u963b\u585e\u5206\u6790\u9700\u8981\u5148\u542f\u7528\uff1a\u5728 main \u51fd\u6570\u4e2d\u6dfb\u52a0 <code>runtime.SetBlockProfileRate(1)<\/code>\u3002<\/p>\n<h3>\u5e38\u89c1\u95ee\u9898\uff1achannel \u963b\u585e<\/h3>\n<pre><code class=\"language-go\">\n\/\/ \u65e0\u7f13\u51b2 channel\uff0c\u751f\u4ea7\u8005\u5feb\u4e8e\u6d88\u8d39\u8005\nch := make(chan int)\nfor i := 0; i &lt; 100; i++ {\n\tgo func() { ch &lt;- i }() \/\/ \u963b\u585e\u76f4\u5230\u88ab\u6d88\u8d39\n}\n<\/code><\/pre>\n<p>\u4f18\u5316\uff1a\u4f7f\u7528\u5e26\u7f13\u51b2 channel \u6216\u8c03\u6574\u6d88\u8d39\u534f\u7a0b\u6570\u91cf\u3002<\/p>\n<h2>\u5b9e\u6218\u6848\u4f8b\uff1a\u4f18\u5316\u4e00\u4e2a REST API<\/h2>\n<p>\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a <code>\/users<\/code> \u63a5\u53e3\uff0c\u8fd4\u56de\u7528\u6237\u5217\u8868\uff0c\u4f46\u54cd\u5e94\u65f6\u95f4\u5f88\u6162\u3002<\/p>\n<h3>\u6b65\u9aa4 1\uff1a\u91c7\u96c6 CPU profile<\/h3>\n<p>\u53d1\u73b0 <code>json.Marshal<\/code> \u548c\u6570\u636e\u5e93\u67e5\u8be2\u5360\u7528\u4e86\u5927\u91cf CPU\u3002<\/p>\n<h3>\u6b65\u9aa4 2\uff1a\u4f18\u5316\u6570\u636e\u5e93\u67e5\u8be2<\/h3>\n<pre><code class=\"language-go\">\n\/\/ \u4f18\u5316\u524d\uff1aN+1 \u67e5\u8be2\nusers := getUsers()\nfor i := range users {\n\tusers[i].Orders = getOrders(users[i].ID) \/\/ \u591a\u6b21\u67e5\u8be2\n}\n\n\/\/ \u4f18\u5316\u540e\uff1a\u6279\u91cf\u67e5\u8be2\nuserIDs := getIDs(users)\nordersMap := getOrdersByIDs(userIDs)\nfor i := range users {\n\tusers[i].Orders = ordersMap[users[i].ID]\n}\n<\/code><\/pre>\n<h3>\u6b65\u9aa4 3\uff1a\u51cf\u5c11 JSON \u5e8f\u5217\u5316\u5f00\u9500<\/h3>\n<ul>\n<li>\u4f7f\u7528 <code>jsoniter<\/code> \u66ff\u4ee3\u6807\u51c6\u5e93\uff08\u5feb 2-3 \u500d\uff09<\/li>\n<li>\u9884\u5206\u914d\u5207\u7247\u5bb9\u91cf\uff1a<code>users := make([]User, 0, expectedCount)<\/code><\/li>\n<\/ul>\n<h3>\u6b65\u9aa4 4\uff1a\u4f7f\u7528 sync.Pool \u590d\u7528\u5bf9\u8c61<\/h3>\n<pre><code class=\"language-go\">\nvar pool = sync.Pool{\n\tNew: func() interface{} { return &amp;User{} },\n}\n\nfunc handler(w http.ResponseWriter, r *http.Request) {\n\tu := pool.Get().(*User)\n\tdefer pool.Put(u)\n\t\/\/ \u4f7f\u7528 u\n}\n<\/code><\/pre>\n<h3>\u7ed3\u679c\u5bf9\u6bd4<\/h3>\n<p>\u4f18\u5316\u540e\uff0c\u63a5\u53e3\u54cd\u5e94\u65f6\u95f4\u4ece 500ms \u964d\u81f3 150ms\uff0cQPS \u63d0\u5347 3 \u500d\u3002<\/p>\n<h2>\u603b\u7ed3<\/h2>\n<ul>\n<li><strong>pprof \u662f\u6027\u80fd\u4f18\u5316\u7684\u8d77\u70b9<\/strong>\uff1a\u5148\u6d4b\u91cf\uff0c\u540e\u4f18\u5316\u3002<\/li>\n<li><strong>\u5173\u6ce8\u70ed\u70b9<\/strong>\uff1aTop \u8868\u3001\u706b\u7130\u56fe\u5b9a\u4f4d\u6d88\u8017\u6700\u5927\u7684\u51fd\u6570\u3002<\/li>\n<li><strong>\u5e38\u89c1\u4f18\u5316\u70b9<\/strong>\uff1a\u5b57\u7b26\u4e32\u62fc\u63a5\u7528 Builder\u3001\u907f\u514d N+1 \u67e5\u8be2\u3001\u590d\u7528\u5bf9\u8c61\u3001\u9884\u5206\u914d\u5185\u5b58\u3002<\/li>\n<li><strong>\u6301\u7eed\u76d1\u63a7<\/strong>\uff1a\u5728\u751f\u4ea7\u73af\u5883\u5b9a\u671f\u91c7\u96c6 profile\uff0c\u5bf9\u6bd4\u4e0d\u540c\u7248\u672c\u3002<\/li>\n<\/ul>\n<h2>\u5ef6\u4f38\u9605\u8bfb<\/h2>\n<ul>\n<li><a href=\"https:\/\/pkg.go.dev\/net\/http\/pprof\">Go \u5b98\u65b9 pprof \u6587\u6863<\/a><\/li>\n<li><a href=\"https:\/\/dave.cheney.net\/2014\/06\/07\/five-suggestions-for-optimizing-go-code\">Dave Cheney \u7684\u6027\u80fd\u4f18\u5316\u5efa\u8bae<\/a><\/li>\n<li>\u4f7f\u7528 <code>benchstat<\/code> \u5bf9\u6bd4\u57fa\u51c6\u6d4b\u8bd5\u7ed3\u679c<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\u672c\u6587\u624b\u628a\u624b\u6559\u4f60\u4f7f\u7528 Go \u5185\u7f6e\u7684 pprof \u5de5\u5177\u8fdb\u884c CPU\u3001\u5185\u5b58\u3001goroutine \u7b49\u6027\u80fd\u5206\u6790\uff0c\u5e76\u901a\u8fc7\u4e00\u4e2a\u5b9e\u9645 Web \u670d\u52a1\u6848\u4f8b\u6f14\u793a\u5982\u4f55\u5b9a\u4f4d\u74f6\u9888\u3001\u4f18\u5316\u4ee3\u7801\uff0c\u6700\u7ec8\u63d0\u5347 3 \u500d\u6027\u80fd\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,4],"tags":[92,93,95,6,94],"class_list":["post-192","post","type-post","status-publish","format-standard","hentry","category-3","category-4","tag-go","tag-pprof","tag-95","tag-6","tag-94"],"_links":{"self":[{"href":"https:\/\/yykcj.com\/index.php?rest_route=\/wp\/v2\/posts\/192","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/yykcj.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/yykcj.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/yykcj.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/yykcj.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=192"}],"version-history":[{"count":0,"href":"https:\/\/yykcj.com\/index.php?rest_route=\/wp\/v2\/posts\/192\/revisions"}],"wp:attachment":[{"href":"https:\/\/yykcj.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=192"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/yykcj.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=192"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/yykcj.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=192"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}