{"id":57732,"date":"2024-10-10T20:42:29","date_gmt":"2024-10-10T12:42:29","guid":{"rendered":"https:\/\/server.hk\/cnblog\/57732\/"},"modified":"2024-10-10T20:42:30","modified_gmt":"2024-10-10T12:42:30","slug":"go%e6%95%99%e7%a8%8b-go-pprof-%e5%91%bd%e4%bb%a4%ef%bc%88go-%e8%aa%9e%e8%a8%80%e6%80%a7%e8%83%bd%e5%88%86%e6%9e%90%e5%91%bd%e4%bb%a4%ef%bc%89%e5%ae%8c%e5%85%a8%e6%94%bb%e7%95%a5","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/57732\/","title":{"rendered":"GO\u6559\u7a0b: go pprof \u547d\u4ee4\uff08Go \u8a9e\u8a00\u6027\u80fd\u5206\u6790\u547d\u4ee4\uff09\u5b8c\u5168\u653b\u7565"},"content":{"rendered":"<h1 id=\"go%e6%95%99%e7%a8%8b-go-pprof-%e5%91%bd%e4%bb%a4%ef%bc%88go-%e8%aa%9e%e8%a8%80%e6%80%a7%e8%83%bd%e5%88%86%e6%9e%90%e5%91%bd%e4%bb%a4%ef%bc%89%e5%ae%8c%e5%85%a8%e6%94%bb%e7%95%a5-SJteNhrgOK\">GO\u6559\u7a0b: go pprof \u547d\u4ee4\uff08Go \u8a9e\u8a00\u6027\u80fd\u5206\u6790\u547d\u4ee4\uff09\u5b8c\u5168\u653b\u7565<\/h1>\n<p>\u5728\u7576\u4eca\u7684\u8edf\u9ad4\u958b\u767c\u4e2d\uff0c\u6027\u80fd\u512a\u5316\u662f\u78ba\u4fdd\u61c9\u7528\u7a0b\u5f0f\u9ad8\u6548\u904b\u884c\u7684\u95dc\u9375\u3002Go \u8a9e\u8a00\u4f5c\u70ba\u4e00\u7a2e\u9ad8\u6548\u7684\u7de8\u7a0b\u8a9e\u8a00\uff0c\u63d0\u4f9b\u4e86\u5f37\u5927\u7684\u6027\u80fd\u5206\u6790\u5de5\u5177\uff0c\u5176\u4e2d\u6700\u91cd\u8981\u7684\u5c31\u662f <code>pprof<\/code>\u3002\u672c\u6587\u5c07\u6df1\u5165\u63a2\u8a0e <code>go pprof<\/code> \u547d\u4ee4\u7684\u4f7f\u7528\u65b9\u6cd5\u53ca\u5176\u5728\u6027\u80fd\u5206\u6790\u4e2d\u7684\u61c9\u7528\u3002<\/p>\n<h2 id=\"%e4%bb%80%e9%ba%bc%e6%98%af-pprof%ef%bc%9f-SJteNhrgOK\">\u4ec0\u9ebc\u662f pprof\uff1f<\/h2>\n<p><code>pprof<\/code> \u662f Go \u8a9e\u8a00\u5167\u5efa\u7684\u4e00\u500b\u6027\u80fd\u5206\u6790\u5de5\u5177\uff0c\u80fd\u5920\u5e6b\u52a9\u958b\u767c\u8005\u5206\u6790\u61c9\u7528\u7a0b\u5f0f\u7684 CPU \u548c\u5167\u5b58\u4f7f\u7528\u60c5\u6cc1\u3002\u901a\u904e <code>pprof<\/code>\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u7372\u53d6\u8a73\u7d30\u7684\u6027\u80fd\u6578\u64da\uff0c\u5f9e\u800c\u627e\u51fa\u6027\u80fd\u74f6\u9838\uff0c\u9032\u884c\u91dd\u5c0d\u6027\u7684\u512a\u5316\u3002<\/p>\n<h2 id=\"%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8-pprof%ef%bc%9f-SJteNhrgOK\">\u5982\u4f55\u4f7f\u7528 pprof\uff1f<\/h2>\n<p>\u4f7f\u7528 <code>pprof<\/code> \u9032\u884c\u6027\u80fd\u5206\u6790\u7684\u904e\u7a0b\u4e3b\u8981\u5206\u70ba\u4ee5\u4e0b\u5e7e\u500b\u6b65\u9a5f\uff1a<\/p>\n<h3 id=\"1-%e5%bc%95%e5%85%a5-pprof-%e5%8c%85-SJteNhrgOK\">1. \u5f15\u5165 pprof \u5305<\/h3>\n<p>\u5728\u4f60\u7684 Go \u61c9\u7528\u7a0b\u5f0f\u4e2d\uff0c\u9996\u5148\u9700\u8981\u5f15\u5165 <code>net\/http\/pprof<\/code> \u5305\u3002\u9019\u53ef\u4ee5\u901a\u904e\u4ee5\u4e0b\u4ee3\u78bc\u5be6\u73fe\uff1a<\/p>\n<pre><code>import _ \"net\/http\/pprof\"<\/code><\/pre>\n<h3 id=\"2-%e5%95%9f%e5%8b%95-http-%e6%9c%8d%e5%8b%99-SJteNhrgOK\">2. \u555f\u52d5 HTTP \u670d\u52d9<\/h3>\n<p>\u63a5\u4e0b\u4f86\uff0c\u4f60\u9700\u8981\u555f\u52d5\u4e00\u500b HTTP \u670d\u52d9\uff0c\u4ee5\u4fbf <code>pprof<\/code> \u53ef\u4ee5\u6536\u96c6\u6027\u80fd\u6578\u64da\u3002\u4ee5\u4e0b\u662f\u4e00\u500b\u7c21\u55ae\u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code>package main\n\nimport (\n    \"net\/http\"\n    _ \"net\/http\/pprof\"\n)\n\nfunc main() {\n    go func() {\n        log.Println(http.ListenAndServe(\"localhost:6060\", nil))\n    }()\n    \n    \/\/ \u4f60\u7684\u61c9\u7528\u7a0b\u5f0f\u908f\u8f2f\n}<\/code><\/pre>\n<h3 id=\"3-%e6%94%b6%e9%9b%86%e6%80%a7%e8%83%bd%e6%95%b8%e6%93%9a-SJteNhrgOK\">3. \u6536\u96c6\u6027\u80fd\u6578\u64da<\/h3>\n<p>\u555f\u52d5\u61c9\u7528\u7a0b\u5f0f\u5f8c\uff0c\u4f60\u53ef\u4ee5\u901a\u904e\u8a2a\u554f <code>http:\/\/localhost:6060\/debug\/pprof\/<\/code> \u4f86\u67e5\u770b\u53ef\u7528\u7684\u6027\u80fd\u5206\u6790\u5de5\u5177\u3002\u9019\u88e1\u6709\u5e7e\u500b\u91cd\u8981\u7684\u7aef\u9ede\uff1a<\/p>\n<ul>\n<li><code>\/debug\/pprof\/profile<\/code>\uff1a\u6536\u96c6 CPU \u6027\u80fd\u6578\u64da\u3002<\/li>\n<li><code>\/debug\/pprof\/heap<\/code>\uff1a\u6536\u96c6\u5167\u5b58\u4f7f\u7528\u60c5\u6cc1\u3002<\/li>\n<li><code>\/debug\/pprof\/goroutine<\/code>\uff1a\u67e5\u770b goroutine \u7684\u5806\u68e7\u4fe1\u606f\u3002<\/li>\n<\/ul>\n<h3 id=\"4-%e4%bd%bf%e7%94%a8-go-tool-pprof-%e5%91%bd%e4%bb%a4-SJteNhrgOK\">4. \u4f7f\u7528 go tool pprof \u547d\u4ee4<\/h3>\n<p>\u6536\u96c6\u5230\u6027\u80fd\u6578\u64da\u5f8c\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>go tool pprof<\/code> \u547d\u4ee4\u9032\u884c\u5206\u6790\u3002\u4f8b\u5982\uff0c\u6536\u96c6 CPU \u6578\u64da\u7684\u547d\u4ee4\u5982\u4e0b\uff1a<\/p>\n<pre><code>go tool pprof http:\/\/localhost:6060\/debug\/pprof\/profile<\/code><\/pre>\n<p>\u9019\u5c07\u555f\u52d5\u4e00\u500b\u4ea4\u4e92\u5f0f\u7684\u547d\u4ee4\u884c\u754c\u9762\uff0c\u8b93\u4f60\u53ef\u4ee5\u67e5\u770b\u6027\u80fd\u6578\u64da\u3002<\/p>\n<h2 id=\"pprof-%e7%9a%84%e5%b8%b8%e7%94%a8%e5%91%bd%e4%bb%a4-SJteNhrgOK\">pprof \u7684\u5e38\u7528\u547d\u4ee4<\/h2>\n<p>\u5728 <code>pprof<\/code> \u7684\u4ea4\u4e92\u5f0f\u754c\u9762\u4e2d\uff0c\u6709\u5e7e\u500b\u5e38\u7528\u7684\u547d\u4ee4\uff1a<\/p>\n<ul>\n<li><code>top<\/code>\uff1a\u986f\u793a CPU \u4f7f\u7528\u7387\u6700\u9ad8\u7684\u51fd\u6578\u3002<\/li>\n<li><code>list [function]<\/code>\uff1a\u986f\u793a\u6307\u5b9a\u51fd\u6578\u7684\u6e90\u4ee3\u78bc\u53ca\u5176\u6027\u80fd\u6578\u64da\u3002<\/li>\n<li><code>web<\/code>\uff1a\u751f\u6210\u6027\u80fd\u5716\u4e26\u5728\u700f\u89bd\u5668\u4e2d\u6253\u958b\u3002<\/li>\n<\/ul>\n<h2 id=\"%e5%af%a6%e9%9a%9b%e6%a1%88%e4%be%8b%e5%88%86%e6%9e%90-SJteNhrgOK\">\u5be6\u969b\u6848\u4f8b\u5206\u6790<\/h2>\n<p>\u5047\u8a2d\u4f60\u6709\u4e00\u500b\u7c21\u55ae\u7684 Web \u61c9\u7528\u7a0b\u5f0f\uff0c\u4e26\u4e14\u767c\u73fe\u5176\u6027\u80fd\u4e0d\u4f73\u3002\u901a\u904e\u4f7f\u7528 <code>pprof<\/code>\uff0c\u4f60\u53ef\u4ee5\u767c\u73fe\u67d0\u500b\u7279\u5b9a\u7684\u51fd\u6578\u6d88\u8017\u4e86\u904e\u591a\u7684 CPU \u6642\u9593\u3002\u9019\u6642\uff0c\u4f60\u53ef\u4ee5\u91dd\u5c0d\u8a72\u51fd\u6578\u9032\u884c\u512a\u5316\uff0c\u5f9e\u800c\u63d0\u9ad8\u6574\u9ad4\u6027\u80fd\u3002<\/p>\n<h2 id=\"%e7%b8%bd%e7%b5%90-SJteNhrgOK\">\u7e3d\u7d50<\/h2>\n<p>\u4f7f\u7528 <code>go pprof<\/code> \u547d\u4ee4\u9032\u884c\u6027\u80fd\u5206\u6790\u662f\u4e00\u500b\u5f37\u5927\u7684\u5de5\u5177\uff0c\u80fd\u5920\u5e6b\u52a9\u958b\u767c\u8005\u8b58\u5225\u548c\u89e3\u6c7a\u6027\u80fd\u74f6\u9838\u3002\u901a\u904e\u7c21\u55ae\u7684\u6b65\u9a5f\uff0c\u4f60\u53ef\u4ee5\u8f15\u9b06\u5730\u96c6\u6210 <code>pprof<\/code> \u5230\u4f60\u7684 Go \u61c9\u7528\u7a0b\u5f0f\u4e2d\uff0c\u4e26\u5229\u7528\u5176\u63d0\u4f9b\u7684\u6578\u64da\u9032\u884c\u6df1\u5165\u5206\u6790\u3002\u5c0d\u65bc\u9700\u8981\u9ad8\u6548\u904b\u884c\u7684\u61c9\u7528\u7a0b\u5f0f\uff0c\u638c\u63e1 <code>pprof<\/code> \u7684\u4f7f\u7528\u5c07\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002<\/p>\n<p>\u5982\u679c\u4f60\u6b63\u5728\u5c0b\u627e\u9ad8\u6548\u7684 <a href=\"https:\/\/server.hk\">VPS<\/a> \u89e3\u6c7a\u65b9\u6848\u4f86\u904b\u884c\u4f60\u7684 Go \u61c9\u7528\u7a0b\u5f0f\uff0cServer.HK \u63d0\u4f9b\u4e86\u591a\u7a2e\u9078\u64c7\uff0c\u5e6b\u52a9\u4f60\u8f15\u9b06\u90e8\u7f72\u548c\u7ba1\u7406\u4f60\u7684\u4f3a\u670d\u5668\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6df1\u5165\u4e86\u89e3Go\u8bed\u8a00\u7684pprof\u547d\u4ee4\uff0c\u638c\u63e1\u6027\u80fd\u5206\u6790\u6280\u5de7\uff0c\u4f18\u5316\u4ee3\u7801\u6548\u7387\uff0c\u63d0\u5347\u5e94\u7528\u7a0b\u5e8f\u6027\u80fd\u7684\u5b8c\u5168\u653b\u7565\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4924],"tags":[],"class_list":["post-57732","post","type-post","status-publish","format-standard","hentry","category-setup-tutorials"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/57732","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=57732"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/57732\/revisions"}],"predecessor-version":[{"id":57733,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/57732\/revisions\/57733"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=57732"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=57732"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=57732"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}