{"id":205685,"date":"2025-06-12T08:36:11","date_gmt":"2025-06-12T00:36:11","guid":{"rendered":"https:\/\/server.hk\/cnblog\/205685\/"},"modified":"2025-06-12T08:36:11","modified_gmt":"2025-06-12T00:36:11","slug":"%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8pprof%e5%af%b9golang%e6%a1%86%e6%9e%b6%e8%bf%9b%e8%a1%8c%e6%80%a7%e8%83%bd%e5%88%86%e6%9e%90%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/205685\/","title":{"rendered":"\u5982\u4f55\u4f7f\u7528pprof\u5bf9golang\u6846\u67b6\u8fdb\u884c\u6027\u80fd\u5206\u6790\uff1f"},"content":{"rendered":"<p>IT\u884c\u4e1a\u76f8\u5bf9\u4e8e\u4e00\u822c\u4f20\u7edf\u884c\u4e1a\uff0c\u53d1\u5c55\u66f4\u65b0\u901f\u5ea6\u66f4\u5feb\uff0c\u4e00\u65e6\u505c\u6b62\u4e86\u5b66\u4e60\uff0c\u5f88\u5feb\u5c31\u4f1a\u88ab\u884c\u4e1a\u6240\u6dd8\u6c70\u3002\u6240\u4ee5\u6211\u4eec\u9700\u8981\u8e0f\u8e0f\u5b9e\u5b9e\u7684\u4e0d\u65ad\u5b66\u4e60\uff0c\u7cbe\u8fdb\u81ea\u5df1\u7684\u6280\u672f\uff0c\u5c24\u5176\u662f\u521d\u5b66\u8005\u3002\u4eca\u5929GOLANG\u7ed9\u5927\u5bb6\u6574\u7406\u4e86\u300a\u5982\u4f55\u4f7f\u7528pprof\u5bf9golang\u6846\u67b6\u8fdb\u884c\u6027\u80fd\u5206\u6790\uff1f\u300b\uff0c\u804a\u804a\uff0c\u6211\u4eec\u4e00\u8d77\u6765\u770b\u770b\u5427\uff01,pprof \u662f\u4e00\u6b3e\u7528\u4e8e\u5206\u6790 Go \u7a0b\u5e8f\u6027\u80fd\u7684\u5de5\u5177\u3002\u4f7f\u7528\u6b65\u9aa4\uff1a\u5b89\u88c5 pprof\uff1ago install golang.org\/x\/tools\/cmd\/pprof\u5bfc\u5165 pprof \u5305\u5e76\u6ce8\u518c HTTP \u5904\u7406\u7a0b\u5e8f\u4ee5\u6355\u83b7\u6027\u80fd\u6570\u636e\u4f7f\u7528 pprof.Profile \u51fd\u6570\u6536\u96c6\u6027\u80fd\u6570\u636e\u8bbf\u95ee URL \u67e5\u770b\u6027\u80fd\u6570\u636e\u5e76\u5728\u6d4f\u89c8\u5668\u4e2d\u4e0b\u8f7d\u6587\u4ef6\u4f7f\u7528 go tool pprof \u547d\u4ee4\u67e5\u770b\u6027\u80fd\u6570\u636e\u4f7f\u7528\u4ea4\u4e92\u5f0f\u547d\u4ee4\u5206\u6790\u6570\u636e\u4ee5\u627e\u51fa\u74f6\u9888,<br \/>\n<img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241026\/1729943366671cd7465edfa.jpg\" class=\"aligncenter\">,<br \/>\n<strong>\u5982\u4f55\u4f7f\u7528 pprof \u5bf9 Go \u6846\u67b6\u8fdb\u884c\u6027\u80fd\u5206\u6790\uff1f<\/strong>,<br \/>\n<strong>\u7b80\u4ecb<\/strong>,pprof \u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u5de5\u5177\uff0c\u7528\u4e8e\u5206\u6790 Go \u7a0b\u5e8f\u7684\u6027\u80fd\u3002\u5b83\u53ef\u4ee5\u901a\u8fc7\u63d0\u4f9b\u6709\u5173 CPU \u4f7f\u7528\u60c5\u51b5\u3001\u5185\u5b58\u5206\u914d\u548c\u5806\u6808\u8ddf\u8e2a\u4fe1\u606f\uff0c\u5e2e\u52a9\u8bc6\u522b\u6027\u80fd\u74f6\u9888\u3002\u672c\u6587\u5c06\u6307\u5bfc\u60a8\u5728 Go \u9879\u76ee\u4e2d\u4f7f\u7528 pprof \u8fdb\u884c\u6027\u80fd\u5206\u6790\uff0c\u5e76\u63d0\u4f9b\u4e00\u4e2a\u5b9e\u6218\u6848\u4f8b\u3002,<br \/>\n<strong>\u5b89\u88c5<\/strong>,\u5728\u4f7f\u7528 pprof \u4e4b\u524d\uff0c\u9700\u8981\u5c06\u5176\u5b89\u88c5\u5230\u60a8\u7684\u7cfb\u7edf\u4e2d\uff0c\u547d\u4ee4\u5982\u4e0b\uff1a,<br \/>\n<strong>\u7528\u6cd5<\/strong>,\u8981\u5bf9\u6b63\u5728\u8fd0\u884c\u7684 Go \u7a0b\u5e8f\u8fdb\u884c\u6027\u80fd\u5206\u6790\uff0c\u53ef\u4ee5\u4f7f\u7528<br \/>\n<code>pprof.Lookup(<\/code>)<br \/>\n<code> \u51fd\u6570\uff0c\u5b83\u5c06\u8fd4\u56de\u4e00\u4e2a <\/code>Profile` \u7ed3\u6784\u3002\u8be5\u7ed3\u6784\u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u6cd5\uff0c\u7528\u4e8e\u5206\u6790\u7a0b\u5e8f\u7684\u884c\u4e3a\u3002,<br \/>\n<strong>\u5b9e\u6218\u6848\u4f8b<\/strong>,\u8ba9\u6211\u4eec\u901a\u8fc7\u4e00\u4e2a\u5b9e\u9645\u6848\u4f8b\u6765\u4e86\u89e3\u5982\u4f55\u4f7f\u7528 pprof\u3002\u5047\u8bbe\u60a8\u6709\u4e00\u4e2a\u5904\u7406 HTTP \u8bf7\u6c42\u7684 Go \u6846\u67b6\uff0c\u5e76\u4e14\u60a8\u5e0c\u671b\u627e\u51fa\u5bfc\u81f4\u9ad8 CPU \u4f7f\u7528\u7387\u7684\u74f6\u9888\u3002,<br \/>\n<strong>\u7ed3\u8bba<\/strong>,pprof \u662f\u5206\u6790 Go \u7a0b\u5e8f\u6027\u80fd\u7684\u5b9d\u8d35\u5de5\u5177\u3002\u901a\u8fc7\u6309\u7167\u672c\u6587\u4e2d\u7684\u6b65\u9aa4\uff0c\u60a8\u53ef\u4ee5\u8f7b\u677e\u5730\u96c6\u6210 pprof \u5230\u60a8\u7684\u6846\u67b6\u4e2d\uff0c\u5e76\u627e\u51fa\u5bfc\u81f4\u6027\u80fd\u95ee\u9898\u7684\u74f6\u9888\u3002,\u4eca\u5929\u5173\u4e8e\u300a\u5982\u4f55\u4f7f\u7528pprof\u5bf9golang\u6846\u67b6\u8fdb\u884c\u6027\u80fd\u5206\u6790\uff1f\u300b\u7684\u5185\u5bb9\u4ecb\u7ecd\u5c31\u5230\u6b64\u7ed3\u675f\uff0c\u5982\u679c\u6709\u4ec0\u4e48\u7591\u95ee\u6216\u8005\u5efa\u8bae\uff0c\u53ef\u4ee5\u5728GOLANG\u516c\u4f17\u53f7\u4e0b\u591a\u591a\u56de\u590d\u4ea4\u6d41\uff1b\u6587\u4e2d\u82e5\u6709\u4e0d\u6b63\u4e4b\u5904\uff0c\u4e5f\u5e0c\u671b\u56de\u590d\u7559\u8a00\u4ee5\u544a\u77e5\uff01,IT\u884c\u4e1a\u76f8\u5bf9\u4e8e\u4e00\u822c\u4f20\u7edf\u884c\u4e1a\uff0c\u53d1\u5c55\u66f4\u65b0\u901f\u5ea6\u66f4\u5feb\uff0c\u4e00\u65e6\u505c\u6b62\u4e86\u5b66\u4e60\uff0c\u5f88\u5feb\u5c31\u4f1a\u88ab\u884c\u4e1a\u6240\u6dd8\u6c70\u3002\u6240\u4ee5\u6211\u4eec\u9700\u8981\u8e0f\u8e0f\u5b9e\u5b9e\u7684\u4e0d\u65ad\u5b66\u4e60\uff0c\u7cbe\u8fdb\u81ea\u5df1\u7684\u6280\u672f\uff0c\u5c24\u5176\u662f\u521d\u5b66\u8005\u3002\u4eca\u5929GOLANG\u7ed9\u5927\u5bb6\u6574\u7406\u4e86\u300a\u5982\u4f55\u4f7f\u7528pprof\u5bf9golang\u6846\u67b6\u8fdb\u884c\u6027\u80fd\u5206\u6790\uff1f\u300b\uff0c\u804a\u804a\uff0c\u6211\u4eec\u4e00\u8d77\u6765\u770b\u770b\u5427\uff01,<br \/>\n<b><\/b> <\/p>\n<p>\u5f53\u524d\u4f4d\u7f6e\uff1a <span>&gt;<\/span>  <span>&gt;<\/span>  <span>&gt;<\/span>  <span>&gt;<\/span> <span>\u5982\u4f55\u4f7f\u7528pprof\u5bf9golang\u6846\u67b6\u8fdb\u884c\u6027\u80fd\u5206\u6790\uff1f<\/span><\/p>\n<h1>\u5982\u4f55\u4f7f\u7528pprof\u5bf9golang\u6846\u67b6\u8fdb\u884c\u6027\u80fd\u5206\u6790\uff1f<\/h1>\n<p><span>2024-10-26 19:49:45<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>IT\u884c\u4e1a\u76f8\u5bf9\u4e8e\u4e00\u822c\u4f20\u7edf\u884c\u4e1a\uff0c\u53d1\u5c55\u66f4\u65b0\u901f\u5ea6\u66f4\u5feb\uff0c\u4e00\u65e6\u505c\u6b62\u4e86\u5b66\u4e60\uff0c\u5f88\u5feb\u5c31\u4f1a\u88ab\u884c\u4e1a\u6240\u6dd8\u6c70\u3002\u6240\u4ee5\u6211\u4eec\u9700\u8981\u8e0f\u8e0f\u5b9e\u5b9e\u7684\u4e0d\u65ad\u5b66\u4e60\uff0c\u7cbe\u8fdb\u81ea\u5df1\u7684\u6280\u672f\uff0c\u5c24\u5176\u662f\u521d\u5b66\u8005\u3002\u4eca\u5929GOLANG\u7ed9\u5927\u5bb6\u6574\u7406\u4e86\u300a\u5982\u4f55\u4f7f\u7528pprof\u5bf9golang\u6846\u67b6\u8fdb\u884c\u6027\u80fd\u5206\u6790\uff1f\u300b\uff0c\u804a\u804a\uff0c\u6211\u4eec\u4e00\u8d77\u6765\u770b\u770b\u5427\uff01<\/p>\n<p>pprof \u662f\u4e00\u6b3e\u7528\u4e8e\u5206\u6790 Go \u7a0b\u5e8f\u6027\u80fd\u7684\u5de5\u5177\u3002\u4f7f\u7528\u6b65\u9aa4\uff1a\u5b89\u88c5 pprof\uff1ago install golang.org\/x\/tools\/cmd\/pprof\u5bfc\u5165 pprof \u5305\u5e76\u6ce8\u518c HTTP \u5904\u7406\u7a0b\u5e8f\u4ee5\u6355\u83b7\u6027\u80fd\u6570\u636e\u4f7f\u7528 pprof.Profile \u51fd\u6570\u6536\u96c6\u6027\u80fd\u6570\u636e\u8bbf\u95ee URL \u67e5\u770b\u6027\u80fd\u6570\u636e\u5e76\u5728\u6d4f\u89c8\u5668\u4e2d\u4e0b\u8f7d\u6587\u4ef6\u4f7f\u7528 go tool pprof \u547d\u4ee4\u67e5\u770b\u6027\u80fd\u6570\u636e\u4f7f\u7528\u4ea4\u4e92\u5f0f\u547d\u4ee4\u5206\u6790\u6570\u636e\u4ee5\u627e\u51fa\u74f6\u9888<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241026\/1729943366671cd7465edfa.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>\u5982\u4f55\u4f7f\u7528 pprof \u5bf9 Go \u6846\u67b6\u8fdb\u884c\u6027\u80fd\u5206\u6790\uff1f<\/strong><\/p>\n<p><strong>\u7b80\u4ecb<\/strong><\/p>\n<p>pprof \u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u5de5\u5177\uff0c\u7528\u4e8e\u5206\u6790 Go \u7a0b\u5e8f\u7684\u6027\u80fd\u3002\u5b83\u53ef\u4ee5\u901a\u8fc7\u63d0\u4f9b\u6709\u5173 CPU \u4f7f\u7528\u60c5\u51b5\u3001\u5185\u5b58\u5206\u914d\u548c\u5806\u6808\u8ddf\u8e2a\u4fe1\u606f\uff0c\u5e2e\u52a9\u8bc6\u522b\u6027\u80fd\u74f6\u9888\u3002\u672c\u6587\u5c06\u6307\u5bfc\u60a8\u5728 Go \u9879\u76ee\u4e2d\u4f7f\u7528 pprof \u8fdb\u884c\u6027\u80fd\u5206\u6790\uff0c\u5e76\u63d0\u4f9b\u4e00\u4e2a\u5b9e\u6218\u6848\u4f8b\u3002<\/p>\n<p><strong>\u5b89\u88c5<\/strong><\/p>\n<p>\u5728\u4f7f\u7528 pprof \u4e4b\u524d\uff0c\u9700\u8981\u5c06\u5176\u5b89\u88c5\u5230\u60a8\u7684\u7cfb\u7edf\u4e2d\uff0c\u547d\u4ee4\u5982\u4e0b\uff1a<\/p>\n<pre>go install golang.org\/x\/tools\/cmd\/pprof<\/pre>\n<p><strong>\u7528\u6cd5<\/strong><\/p>\n<p>\u8981\u5bf9\u6b63\u5728\u8fd0\u884c\u7684 Go \u7a0b\u5e8f\u8fdb\u884c\u6027\u80fd\u5206\u6790\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>pprof.Lookup(<\/code>)<code> \u51fd\u6570\uff0c\u5b83\u5c06\u8fd4\u56de\u4e00\u4e2a <\/code>Profile` \u7ed3\u6784\u3002\u8be5\u7ed3\u6784\u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u6cd5\uff0c\u7528\u4e8e\u5206\u6790\u7a0b\u5e8f\u7684\u884c\u4e3a\u3002<\/p>\n<p><strong>\u5b9e\u6218\u6848\u4f8b<\/strong><\/p>\n<p>\u8ba9\u6211\u4eec\u901a\u8fc7\u4e00\u4e2a\u5b9e\u9645\u6848\u4f8b\u6765\u4e86\u89e3\u5982\u4f55\u4f7f\u7528 pprof\u3002\u5047\u8bbe\u60a8\u6709\u4e00\u4e2a\u5904\u7406 HTTP \u8bf7\u6c42\u7684 Go \u6846\u67b6\uff0c\u5e76\u4e14\u60a8\u5e0c\u671b\u627e\u51fa\u5bfc\u81f4\u9ad8 CPU \u4f7f\u7528\u7387\u7684\u74f6\u9888\u3002<\/p>\n<ol>\n<li>\n<p><strong>\u6536\u96c6\u6027\u80fd\u6570\u636e\uff1a<\/strong><\/p>\n<ul>\n<li>\u5728\u7a0b\u5e8f\u4e2d\u5bfc\u5165 <code>pprof<\/code> \u5305\u3002<\/li>\n<li>\u6ce8\u518c\u4e00\u4e2a HTTP \u5904\u7406\u7a0b\u5e8f\uff0c\u7528\u4e8e\u83b7\u53d6\u6027\u80fd\u6570\u636e\uff1a<\/li>\n<\/ul>\n<pre>import (\n    \"net\/http\/pprof\"\n)\n\nfunc main() {\n    go http.ListenAndServe(\"localhost:6060\", nil)\n}<\/pre>\n<ul>\n<li>\n<p>\u5728\u5904\u7406\u7a0b\u5e8f\u4e2d\u4f7f\u7528 <code>pprof.Profile<\/code> \u51fd\u6570\u6355\u83b7\u6027\u80fd\u6570\u636e\uff1a<\/p>\n<pre>func profileHandler(w http.ResponseWriter, r *http.Request) {\n  p := pprof.Lookup(\"goroutine\")\n  if p != nil {\n      p.WriteTo(w, 1)\n  }\n}<\/pre>\n<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u67e5\u770b\u6027\u80fd\u6570\u636e\uff1a<\/strong><\/p>\n<ul>\n<li>\u8bbf\u95ee http:\/\/localhost:6060\/debug\/pprof\/goroutine URL \u4ee5\u67e5\u770b\u6027\u80fd\u6570\u636e\u3002<\/li>\n<li>\u5c06\u4e0b\u8f7d\u4e00\u4e2a\u540d\u4e3a <code>goroutine.pb.gz<\/code> \u7684\u6587\u4ef6\uff0c\u5176\u4e2d\u5305\u542b\u6027\u80fd\u6570\u636e\u3002<\/li>\n<li>\u4f7f\u7528 <code>go tool pprof<\/code> \u547d\u4ee4\u67e5\u770b\u6027\u80fd\u6570\u636e\uff1a<\/li>\n<\/ul>\n<pre>go tool pprof http:\/\/localhost:6060\/debug\/pprof\/goroutine goroutine.pb.gz<\/pre>\n<\/li>\n<li>\n<p><strong>\u5206\u6790\u6027\u80fd\u6570\u636e\uff1a<\/strong><\/p>\n<ul>\n<li>pprof \u4f1a\u63d0\u4f9b\u4e00\u4e2a\u4ea4\u4e92\u5f0f\u754c\u9762\uff0c\u5176\u4e2d\u663e\u793a\u6709\u5173 CPU \u4f7f\u7528\u60c5\u51b5\u3001\u5185\u5b58\u5206\u914d\u548c\u5806\u6808\u8ddf\u8e2a\u4fe1\u606f\u3002<\/li>\n<li>\u4f7f\u7528\u4ea4\u4e92\u5f0f\u547d\u4ee4\uff08\u4f8b\u5982 <code>top<\/code>\u3001<code>web<\/code>\u3001<code>list<\/code>\uff09\u6765\u5206\u6790\u6570\u636e\u5e76\u627e\u51fa\u74f6\u9888\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<p><strong>\u7ed3\u8bba<\/strong><\/p>\n<p>pprof \u662f\u5206\u6790 Go \u7a0b\u5e8f\u6027\u80fd\u7684\u5b9d\u8d35\u5de5\u5177\u3002\u901a\u8fc7\u6309\u7167\u672c\u6587\u4e2d\u7684\u6b65\u9aa4\uff0c\u60a8\u53ef\u4ee5\u8f7b\u677e\u5730\u96c6\u6210 pprof \u5230\u60a8\u7684\u6846\u67b6\u4e2d\uff0c\u5e76\u627e\u51fa\u5bfc\u81f4\u6027\u80fd\u95ee\u9898\u7684\u74f6\u9888\u3002<\/p>\n<p>\u4eca\u5929\u5173\u4e8e\u300a\u5982\u4f55\u4f7f\u7528pprof\u5bf9golang\u6846\u67b6\u8fdb\u884c\u6027\u80fd\u5206\u6790\uff1f\u300b\u7684\u5185\u5bb9\u4ecb\u7ecd\u5c31\u5230\u6b64\u7ed3\u675f\uff0c\u5982\u679c\u6709\u4ec0\u4e48\u7591\u95ee\u6216\u8005\u5efa\u8bae\uff0c\u53ef\u4ee5\u5728GOLANG\u516c\u4f17\u53f7\u4e0b\u591a\u591a\u56de\u590d\u4ea4\u6d41\uff1b\u6587\u4e2d\u82e5\u6709\u4e0d\u6b63\u4e4b\u5904\uff0c\u4e5f\u5e0c\u671b\u56de\u590d\u7559\u8a00\u4ee5\u544a\u77e5\uff01<\/p>\n<dl>\n<dt>\n <\/dt>\n<dd>\n   \u5982\u4f55\u4e3a input \u6807\u7b7e\u6dfb\u52a0 checked \u548c checked:after \u6837\u5f0f\uff1f\n <\/dd>\n<\/dl>\n<dl>\n<dt>\n <\/dt>\n<dd>\n   golang\u6846\u67b6\u7684\u4ee3\u7801\u751f\u6210\u5668\uff1a\u5728\u9879\u76ee\u4e2d\u7684\u5e94\u7528\u573a\u666f\n <\/dd>\n<\/dl>\n","protected":false},"excerpt":{"rendered":"<p>IT\u884c\u4e1a\u76f8\u5bf9\u4e8e\u4e00\u822c\u4f20\u7edf\u884c\u4e1a\uff0c\u53d1\u5c55&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-205685","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/205685","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=205685"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/205685\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=205685"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=205685"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=205685"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}