{"id":323107,"date":"2023-02-10T07:00:00","date_gmt":"2023-02-10T06:00:00","guid":{"rendered":"https:\/\/blog.jetbrains.com\/?post_type=go&#038;p=323107"},"modified":"2023-02-10T05:45:18","modified_gmt":"2023-02-10T04:45:18","slug":"profiling-go-code-with-goland","status":"publish","type":"go","link":"https:\/\/blog.jetbrains.com\/zh-hans\/go\/2023\/02\/10\/profiling-go-code-with-goland\/","title":{"rendered":"\u4f7f\u7528 GoLand \u5206\u6790 Go \u4ee3\u7801"},"content":{"rendered":"<p><em>\u672c\u6587\u7531\u5916\u90e8\u8d21\u732e\u8005\u64b0\u5199\u3002<\/em><\/p>\n<div class=\"about-author \">\n<div class=\"about-author__box\">\n<div class=\"row\">\n<div class=\"about-author__box-img\">\n                            <img decoding=\"async\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2022\/12\/ipL348tmFljyoHv_vX7nmLSFAWYayGOvBl-Qn1tS3HI.webp\" alt=\"\" loading=\"lazy\">\n                        <\/div>\n<div class=\"about-author__box-text\">\n<h4>Hrittik Roy<\/h4>\n<p>Hrittik \u662f\u4e00\u4f4d\u4f5c\u5bb6\u548c\u8f6f\u4ef6\u5de5\u7a0b\u5e08\uff0c\u4e13\u95e8\u7814\u7a76\u4e91\u539f\u751f\u751f\u6001\u7cfb\u7edf\u3002 \u4ed6\u66fe\u53c2\u4e0e\u8fc7\u8bb8\u591a\u5927\u578b\u9879\u76ee\uff0c\u5728\u4e91\u8ba1\u7b97\u7684\u6280\u672f\u548c\u5546\u4e1a\u65b9\u9762\u90fd\u6709\u7ecf\u9a8c\u3002 \u4ed6\u7ecf\u5e38\u5728\u4f1a\u8bae\u4e0a\u53d1\u8a00\uff0c\u5e76\u5199\u8fc7\u8bb8\u591a\u5173\u4e8e\u8f6f\u4ef6\u5f00\u53d1\u548c\u5206\u5e03\u5f0f\u7cfb\u7edf\u7684\u6587\u7ae0\u3002 \u5728\u4ed6\u7684\u7a7a\u95f2\u65f6\u95f4\uff0c\u4ed6\u559c\u6b22\u6563\u6b65\u3002<\/p>\n<p><a href=\"https:\/\/www.linkedin.com\/in\/hrittikhere\/\" target=\"_blank\" rel=\"nofollow noopener\">LinkedIn<\/a>&nbsp;<a href=\"https:\/\/twitter.com\/hrittikhere\" target=\"_blank\" rel=\"nofollow noopener\">Twitter<\/a><\/p>\n<p><\/p><\/div>\n<p><\/p><\/div>\n<p><\/p><\/div>\n<p><\/p><\/div>\n<p>\u73b0\u4ee3\u8f6f\u4ef6\u5806\u6808\u5305\u542b\u8bb8\u591a\u79fb\u52a8\u7684\u90e8\u5206\u3002 \u7531\u4e8e\u5404\u4e2a\u90e8\u5206\u53ef\u80fd\u4f9d\u8d56\u4e8e\u4e0d\u540c\u7684\u4f9d\u8d56\u9879\uff0c\u5e94\u7528\u7a0b\u5e8f\u6027\u80fd\u4e2d\u7684\u74f6\u9888\u4f1a\u5f88\u96be\u786e\u5b9a\u3002<\/p>\n<p>\u5206\u6790\u53ef\u4ee5\u4e3a\u5e94\u7528\u7a0b\u5e8f\u7684\u8fd0\u884c\u60c5\u51b5\u548c\u6539\u8fdb\u65b9\u5411\u63d0\u4f9b\u6df1\u5165\u89c1\u89e3\u3002 \u901a\u8fc7\u8bc6\u522b\u4ee3\u7801\u95ee\u9898\uff08\u4f8b\u5982\u5185\u5b58\u3001CPU\u3001I\/O \u548c\u5176\u4ed6\u7ec4\u4ef6\u4e2d\u7684\u74f6\u9888\uff09\uff0c\u5e94\u7528\u7a0b\u5e8f\u5206\u6790\u53ef\u4ee5\u5e2e\u52a9\u63d0\u5347\u6027\u80fd\uff0c\u8fd9\u5bf9\u4e8e\u5b9e\u65bd\u4f18\u5316\u548c\u63d0\u5347\u7528\u6237\u4f53\u9a8c\u81f3\u5173\u91cd\u8981\u3002<\/p>\n<p>\u6b64\u5916\uff0c\u5728<a href=\"https:\/\/www.brendangregg.com\/flamegraphs.html\" target=\"_blank\" rel=\"noopener\">\u706b\u7130\u56fe<\/a>\u548c\u5176\u4ed6\u5de5\u5177\u7684\u5e2e\u52a9\u4e0b\uff0c\u5206\u6790\u8fd8\u53ef\u4ee5\u5c55\u793a\u7a0b\u5e8f\u5728\u4f55\u5904\u82b1\u8d39\u4e86\u6700\u591a\u65f6\u95f4\uff0c\u4ece\u800c\u5e2e\u52a9\u8c03\u8bd5\u3002 \u4ece\u5206\u6790\u6587\u4ef6\u83b7\u5f97\u6570\u636e\u540e\uff0c\u8f6f\u4ef6\u5de5\u7a0b\u5e08\u53ef\u4ee5\u4f7f\u7528\u8fd9\u4e9b\u4fe1\u606f\u5b9e\u73b0\u6709\u52a9\u4e8e\u63d0\u5347\u6027\u80fd\u7684\u4ee3\u7801\u66f4\u6539\u3001\u68c0\u6d4b\u5185\u5b58\u6cc4\u6f0f\uff0c\u4ee5\u53ca\u4f18\u5316\u6574\u4e2a\u5e94\u7528\u7a0b\u5e8f\u3002<\/p>\n<p>\u5728\u8fd9\u7bc7\u535a\u6587\u4e2d\uff0c\u6211\u4eec\u5c06\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528 <a href=\"https:\/\/www.jetbrains.com\/go\/\" target=\"_blank\" rel=\"noopener\">GoLand<\/a> \u53ca\u5176\u5185\u7f6e\u529f\u80fd\uff0c\u4f8b\u5982 CPU\u3001\u5185\u5b58\u3001\u4e92\u65a5\u548c\u963b\u585e\u5206\u6790\u5668\u3002 \u672c\u6587\u8fd8\u5c06\u8bf4\u660e\u5982\u4f55\u7406\u89e3\u6765\u81ea\u8fd9\u4e9b\u5206\u6790\u5668\u7684\u6570\u636e\u4ee5\u63d0\u5347\u793a\u4f8b\u7a0b\u5e8f\u7684\u6027\u80fd\u548c\u8fd0\u884c\u65f6\u3002<\/p>\n<h2><strong>\u5206\u6790 Go \u51fd\u6570<\/strong><\/h2>\n<p>Go \u751f\u6001\u7cfb\u7edf\u63d0\u4f9b\u4e86\u5927\u91cf API \u548c\u5de5\u5177\uff0c\u60a8\u53ef\u4ee5\u501f\u52a9 CPU\u3001\u5185\u5b58\u3001\u5757\u548c\u4e92\u65a5\u5206\u6790\u5668\u5206\u6790 Go \u7a0b\u5e8f\uff0c\u4ee5\u53ca\u5229\u7528 <a href=\"https:\/\/github.com\/google\/pprof\" target=\"_blank\" rel=\"noopener\">pprof<\/a> \u7b49\u5de5\u5177\u53ef\u89c6\u5316\u548c\u89e3\u6790\u5206\u6790\u6570\u636e\u3002<\/p>\n<p>\u751f\u6210 CPU \u5206\u6790\u6587\u4ef6\u540e\uff0c\u60a8\u53ef\u4ee5\u5728\u6d4f\u89c8\u5668\u6216\u6587\u672c\u7f16\u8f91\u5668\u4e2d\u5c06\u5176\u6253\u5f00\u3002 CPU \u5206\u6790\u6587\u4ef6\u7684\u751f\u6210\u76f8\u5f53\u5197\u957f\uff0c\u4f46 GoLand \u4e2d\u7684\u5185\u7f6e\u5206\u6790\u5668\u53ef\u4ee5\u5927\u5e45\u7b80\u5316\u8fd9\u4e00\u8fc7\u7a0b\u3002<\/p>\n<p>GoLand \u4f7f\u7528 pprof \u8f6f\u4ef6\u5305\u6536\u96c6\u6240\u6709\u5fc5\u8981\u6570\u636e\u3002 GoLand \u63d0\u4f9b\u5206\u6790\u6570\u636e\u53ef\u89c6\u5316\uff0c\u4f7f\u8bc6\u522b\u74f6\u9888\u548c\u4f18\u5316\u4ee3\u7801\u66f4\u52a0\u7b80\u5355\u3002 \u5b83\u8fd8\u53ef\u4ee5\u6bd4\u8f83\u4e0d\u540c\u7684\u5206\u6790\u6587\u4ef6\uff0c\u901a\u8fc7\u6d4b\u8bd5\u548c\u5206\u6790\u5355\u4e2a\u51fd\u6570\u6765\u6bd4\u8f83\u5e94\u7528\u7a0b\u5e8f\u4e0d\u540c\u90e8\u5206\u7684\u6027\u80fd\u3002<\/p>\n<h3><strong>\u524d\u63d0<\/strong><\/h3>\n<p>\u5728\u5f00\u59cb\u63a2\u7d22\u8fd9\u4e9b\u5de5\u5177\u4e4b\u524d\uff0c\u9996\u5148\u8981\u51c6\u5907\u597d\uff1a<\/p>\n<ul>\n<li>\u4e00\u53f0\u88c5\u6709 Go \u7684\u673a\u5668\u3002<\/li>\n<li>GoLand \u4f5c\u4e3a IDE \u5b89\u88c5\u5728\u673a\u5668\u4e0a\u3002<\/li>\n<\/ul>\n<p>\u60a8\u53ef\u4ee5\u5728<a href=\"https:\/\/github.com\/hrittikhere\/goland-profiling\" target=\"_blank\" rel=\"noopener\">\u6b64 GitHub \u4ed3\u5e93<\/a>\u4e2d\u627e\u5230\u672c\u6587\u4f7f\u7528\u7684\u6240\u6709\u4ee3\u7801\u3002<\/p>\n<h3><strong>CPU \u5206\u6790\u5668<\/strong><\/h3>\n<p><a href=\"https:\/\/www.jetbrains.com\/help\/go\/cpu-profiler.html\" target=\"_blank\" rel=\"noopener\">CPU \u5206\u6790\u5668<\/a>\u6709\u52a9\u4e8e\u7406\u89e3\u51fd\u6570\u5360\u7528\u7684 CPU \u65f6\u95f4\u767e\u5206\u6bd4\u3002<\/p>\n<h3><strong>\u8bbe\u7f6e\u793a\u4f8b\u7a0b\u5e8f<\/strong><\/h3>\n<p>\u9996\u5148\uff0c\u51c6\u5907\u4e00\u4e2a\u8981\u5206\u6790\u7684\u793a\u4f8b\u7a0b\u5e8f\u3002 \u672c\u793a\u4f8b\u4f7f\u7528\u4e86\u4e00\u4e2a\u8ba1\u7b97\u7b2c n \u4e2a\u6590\u6ce2\u90a3\u5951\u6570\u7684 Go \u7a0b\u5e8f\u3002 \u6590\u6ce2\u90a3\u5951\u6570\u5217\u662f\u4e00\u7cfb\u5217\u6570\u5b57\uff0c\u5176\u4e2d\u6bcf\u4e2a\u6570\u5b57\u90fd\u662f\u524d\u4e24\u4e2a\u6570\u5b57\u7684\u548c\u3002 \u6570\u5217\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<p><code>1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, ...<\/code><\/p>\n<p>\u6590\u6ce2\u90a3\u5951\u7a0b\u5e8f\u7684\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"golang\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">package main\n\/\/ Package main is the entry point\n\nimport \"fmt\"\n\/\/ Import fmt to use Println to show output\n\nfunc FibRecursive(n int) uint64 {\n    \/\/ Function Creation to accept integer till which the Fibonacci series runs\n    if n == 0 {\n        return 0\n        \/\/ Base case for the recursive call\n    } else if n == 1 {\n        return 1\n        \/\/ Base case for the recursive call\n    } else {\n        return FibRecursive(n-1) + FibRecursive(n-2)\n        \/\/ Recursive call for finding the Fibonacci number\n    }\n}\n\n\nfunc main() {\n    \/\/ Function call to print out the nth term of the Fibonacci series\n    fmt.Println(FibRecursive(3))\n}<\/pre>\n<p>\u5c06\u7a0b\u5e8f\u4fdd\u5b58\u4e3a <code>main.go<\/code> \u540e\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528 <code>go run main.go<\/code> \u8fd0\u884c\u3002 \u4e5f\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684 GoLand \u8fd0\u884c\u5668\u3002<\/p>\n<h3><strong>\u8bbe\u7f6e\u793a\u4f8b\u5355\u5143\u6d4b\u8bd5<\/strong><\/h3>\n<p>\u63a5\u4e0b\u6765\uff0c\u521b\u5efa\u4e00\u4e2a\u5355\u5143\u6d4b\u8bd5\uff0c\u5e2e\u52a9\u5bf9\u7a0b\u5e8f\u8fdb\u884c\u6d4b\u8bd5\u548c\u5206\u6790\u6216\u57fa\u51c6\u6d4b\u8bd5\u3002<\/p>\n<p>\u4ee5\u4e0b\u4ee3\u7801\u6f14\u793a\u7684\u6d4b\u8bd5\u903b\u8f91\u57fa\u4e8e\u6570\u5217\u4e2d\u7b2c 30 \u4e2a\u5143\u7d20\u7b49\u4e8e\u7b2c 40 \u4e2a\u6590\u6ce2\u90a3\u5951\u6570 102334155\uff1a<\/p>\n<div>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"golang\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">package main\nimport \"testing\"\nfunc TestFib2(t *testing.T) {\n        if FibRecursive(40) != 102334155 {\n            t.Error(\"Incorrect!\")\n    }\n}<\/pre>\n<\/div>\n<p>\u8981\u8fdb\u884c\u624b\u52a8\u6d4b\u8bd5\uff0c\u53ef\u4ee5\u4f7f\u7528 go test \u547d\u4ee4\uff0c\u6216\u8005\u8fd0\u884c CPU \u5206\u6790\uff0c\u6253\u5f00 <code>_test.go<\/code> \u6587\u4ef6\uff0c\u8f6c\u5230 <code>TestFib2<\/code> \u51fd\u6570\uff0c\u7136\u540e\u70b9\u51fb\u95f4\u8ddd\u533a\u57df\u4e2d\u7684 <em>Run<\/em>\uff08\u8fd0\u884c\uff09\u56fe\u6807\uff0c\u9009\u62e9 <em>Profile with CPU Profiler<\/em>\uff08\u4f7f\u7528 CPU \u5206\u6790\u5668\u5206\u6790\uff09\uff1a<\/p>\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" loading=\"lazy\" width=\"2800\" height=\"1596\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2023\/02\/1-2800x1596.png\" alt=\"\" class=\"wp-image-320403\"><\/figure>\n<p>\u73b0\u5728\uff0c\u5206\u6790\u6570\u636e\u4ee5\u706b\u7130\u56fe\u3001\u8c03\u7528\u6811\u548c\u65b9\u6cd5\u5217\u8868\u7684\u5f62\u5f0f\u8868\u793a\u3002 \u706b\u7130\u56fe\u501f\u52a9\u5806\u6808\u8ddf\u8e2a\u663e\u793a\u51fd\u6570\u7684 CPU \u4f7f\u7528\u60c5\u51b5\u3002 \u53ef\u4ee5\u770b\u51fa\uff0c\u6d4b\u8bd5\u51fd\u6570\u548c\u5176\u4ed6\u9012\u5f52\u8c03\u7528\u82b1\u8d39\u7684\u65f6\u95f4\u6700\u591a\u3002 \u8fd9\u4e2a\u65f6\u95f4\u7531\u56fe\u4e2d\u6700\u5bbd\u7684\u5757\u8868\u793a\uff1a<\/p>\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" loading=\"lazy\" width=\"2800\" height=\"1596\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2023\/02\/2-2800x1596.png\" alt=\"\" class=\"wp-image-320414\"><\/figure>\n<p>\u8c03\u7528\u6811\u6709\u52a9\u4e8e\u4ece\u51fd\u6570\u548c\u5173\u8054\u7684\u5143\u6570\u636e\u7406\u89e3\u793a\u4f8b\u8c03\u7528\u5806\u6808\uff0c\u8fd8\u5177\u6709\u7b5b\u9009\u8c03\u7528\u5e8f\u5217\u7684\u9009\u9879\u3002<\/p>\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" loading=\"lazy\" width=\"2800\" height=\"1596\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2023\/02\/3-2800x1596.png\" alt=\"\" class=\"wp-image-320426\"><\/figure>\n<p>\u65b9\u6cd5\u5217\u8868\u53ef\u7528\u4e8e\u6536\u96c6\u6240\u6709\u51fd\u6570\uff0c\u5e76\u5728\u7d2f\u79ef\u91c7\u6837\u65f6\u95f4\u5185\u5bf9\u5176\u8fdb\u884c\u6392\u5e8f\uff0c\u6bcf\u4e2a\u65b9\u6cd5\u90fd\u5e26\u6709\u56de\u6eaf\u4e0e\u5408\u5e76\u88ab\u8c03\u7528\u65b9\u652f\u6301\u3002 \u4ee5\u4e0b\u662f\u9009\u62e9 CPU \u65f6\u95f4\u65f6\u9012\u5f52 <code>FibRecursive<\/code> \u51fd\u6570\u7684\u5408\u5e76\u88ab\u8c03\u7528\u65b9\u548c\u8c03\u7528\u6811\uff1a<\/p>\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" loading=\"lazy\" width=\"2800\" height=\"1596\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2023\/02\/4-2800x1596.png\" alt=\"\" class=\"wp-image-320437\"><\/figure>\n<h3><strong>\u5206\u6790\u7684\u4f5c\u7528<\/strong><\/h3>\n<p>\u5206\u6790\u6709\u52a9\u4e8e\u4e86\u89e3\u5e94\u7528\u7a0b\u5e8f\u7684\u8fd0\u884c\u60c5\u51b5\u4ee5\u53ca\u4f18\u5316\u65b9\u5411\u3002 \u4ee5\u6590\u6ce2\u90a3\u5951\u6570\u5217\u7a0b\u5e8f\u4f5c\u4e3a\u5b9e\u4f8b\u3002 \u7a0b\u5e8f\u7684 CPU \u5206\u6790\u6587\u4ef6\u663e\u793a\uff0c\u5bf9 <code>FibRecursive<\/code> \u51fd\u6570\u7684\u9012\u5f52\u8c03\u7528\u5360\u7528\u4e86\u5927\u90e8\u5206 CPU \u65f6\u95f4\u3002 \u56e0\u6b64\uff0c\u4f18\u5316\u9012\u5f52\u8c03\u7528\u5e94\u8be5\u662f\u7b2c\u4e00\u8981\u52a1\u3002<\/p>\n<p>\u8981\u4f18\u5316\u5177\u6709\u8bb8\u591a\u9012\u5f52\u8c03\u7528\u7684\u7a0b\u5e8f\uff0c\u60a8\u53ef\u4ee5\u8bbe\u7f6e\u4e00\u4e2a\u6570\u7ec4\u6765\u5b58\u50a8\u8c03\u7528\u503c\u5e76\u4f7f\u7528\u8bb0\u5fc6\u5316\u7f13\u5b58\u7ed3\u679c\uff1a<\/p>\n<pre class=\"EnlighterJSRAW\">package main\n\nimport \"fmt\"\n\nvar f [100010]uint64\n\nfunc Fib(n int) uint64 {\n    f[0] = 0\n    f[1] = 1\n\n    for i := 2; i &lt;= n; i++ {\n        f[i] = f[i-1] + f[i-2]\n    }\n\n    return f[n]\n}\n\nfunc main() {\n    fmt.Println(Fib(3))\n}<\/pre>\n<p>\u4f7f\u7528\u66f4\u65b0\u540e\u7684\u7a0b\u5e8f\u8fd0\u884c CPU \u5206\u6790\u5668\u4e0d\u4f1a\u663e\u793a\u4efb\u4f55\u5206\u6790\u6570\u636e\uff0c\u56e0\u4e3a\u7a0b\u5e8f\u73b0\u5728\u7684\u8fd0\u884c\u901f\u5ea6\u592a\u5feb\uff0c\u5206\u6790\u5668\u65e0\u6cd5\u6536\u96c6\u6837\u672c\u3002 \u7531\u4e8e\u60a8\u80fd\u591f\u6d88\u9664\u9012\u5f52\u8c03\u7528\uff0c\u8fd9\u663e\u7136\u610f\u5473\u7740\u6539\u8fdb\u3002<\/p>\n<p>\u8981\u9a8c\u8bc1\u66f4\u6539\u786e\u5b9e\u63d0\u5347\u4e86\u6027\u80fd\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u57fa\u51c6\u6d4b\u8bd5\u3002<\/p>\n<p>Go \u6807\u51c6\u5e93\u63d0\u4f9b\u4e86\u4e00\u4e2a <code>testing<\/code> \u8f6f\u4ef6\u5305\uff0c\u53ef\u4f9b\u521b\u5efa\u57fa\u51c6\u548c\u8861\u91cf\u4ee3\u7801\u6027\u80fd\u3002&nbsp;<\/p>\n<p>\u4ee5\u4e0b\u662f\u5bf9\u6590\u6ce2\u90a3\u5951\u793a\u4f8b\u4e2d\u7684\u66f4\u6539\u8fdb\u884c\u6d4b\u8bd5\u7684\u793a\u4f8b\u57fa\u51c6\uff1a<\/p>\n<pre class=\"EnlighterJSRAW\">func BenchmarkFib(b *testing.B) {\n    for i := 0; i &lt; b.N; i++ {\n        Fib(1)\n    }\n}<\/pre>\n<p>\u8fd0\u884c\u57fa\u51c6\u6d4b\u8bd5\u65f6\uff0c\u60a8\u5c06\u83b7\u5f97\u6307\u793a\u7a0b\u5e8f\u6548\u7387\u7684\u4ee5\u4e0b\u7ed3\u679c\uff1a<\/p>\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" loading=\"lazy\" width=\"2800\" height=\"732\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2023\/02\/5-2800x732.png\" alt=\"\" class=\"wp-image-320448\"><\/figure>\n<p>\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\uff0c\u60a8\u53ef\u4ee5\u5bf9\u521d\u59cb <code>FibRecursive<\/code> \u51fd\u6570\u8fdb\u884c\u57fa\u51c6\u6d4b\u8bd5\uff1a<\/p>\n<pre class=\"EnlighterJSRAW\">func BenchmarkFibRecursive(b *testing.B) {\n    for i := 0; i &lt; b.N; i++ {\n        FibRecursive(1)\n    }\n}<\/pre>\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" loading=\"lazy\" width=\"2800\" height=\"737\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2023\/02\/6-2800x737.png\" alt=\"\" class=\"wp-image-320459\"><\/figure>\n<p>\u53ef\u4ee5\u770b\u5230\uff0c<code>Fib<\/code> \u51fd\u6570\u82b1\u8d39\u7684\u65f6\u95f4 (0.5367 ns\/op) \u6bd4 <code>FibRecursive<\/code> \u51fd\u6570 (1.636 ns\/op) \u5c11\u5f97\u591a\uff0c\u8fd9\u8868\u660e\u6027\u80fd\u663e\u8457\u63d0\u5347\u3002<\/p>\n<h2><strong>\u5185\u5b58\u5206\u6790\u5668<\/strong><\/h2>\n<p><a href=\"https:\/\/www.jetbrains.com\/help\/go\/memory-profiler.html\" target=\"_blank\" rel=\"noopener\">\u5185\u5b58\u5206\u6790\u5668<\/a>\u901a\u8fc7\u663e\u793a\u51fd\u6570\u548c\u5206\u914d\u7684\u5806\u6765\u5206\u6790\u548c\u8bc6\u522b\u5185\u5b58\u6cc4\u6f0f\u548c\u603b\u4f53\u5185\u5b58\u4f7f\u7528\u60c5\u51b5\u3002 \u8981\u8fd0\u884c\u5185\u5b58\u5206\u6790\uff0c\u8bf7\u6253\u5f00 <code>_test.go<\/code> \u6587\u4ef6\uff0c\u7136\u540e\u70b9\u51fb\u95f4\u8ddd\u533a\u57df\u4e2d\u7684 <em>Run<\/em>\uff08\u8fd0\u884c\uff09\u56fe\u6807\u5e76\u9009\u62e9 <em>Profile with Memory Profiler<\/em>\uff08\u4f7f\u7528\u5185\u5b58\u5206\u6790\u5668\u5206\u6790\uff09\u3002<\/p>\n<p>\u8fd9\u91cc\u7684\u5206\u6790\u6570\u636e\u4e5f\u4ee5\u76f8\u540c\u7684\u4e09\u79cd\u65b9\u5f0f\u8868\u793a\uff1a\u706b\u7130\u56fe\u3001\u8c03\u7528\u6811\u548c\u65b9\u6cd5\u5217\u8868\u3002 \u4e0b\u9762\u7684\u706b\u7130\u56fe\u6570\u636e\u663e\u793a\u4e86\u5206\u914d\u7684\u7a7a\u95f4\u548c\u5bf9\u8c61\uff0c\u5e76\u4ee5\u66f4\u8be6\u7ec6\u7684\u89c6\u56fe\u663e\u793a\u4e86\u6b63\u5728\u4f7f\u7528\u7684\u5bf9\u8c61\u548c\u7a7a\u95f4\uff1a<\/p>\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" loading=\"lazy\" width=\"2800\" height=\"1596\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2023\/02\/7-2800x1596.png\" alt=\"\" class=\"wp-image-320470\"><\/figure>\n<p>\u7531\u4e8e\u5c0f\u5206\u914d\u76f4\u63a5\u5f71\u54cd\u6267\u884c\u901f\u5ea6\uff0c\u53ef\u4ee5\u901a\u8fc7\u51cf\u5c11\u5c0f\u5bf9\u8c61\u7684\u6570\u91cf\u6765\u5b9e\u73b0\u5185\u5b58\u4f18\u5316\u3002<\/p>\n<p>\u8c03\u7528\u6811\u663e\u793a\u4e86\u5185\u5b58\u4e2d\u5bf9\u8c61\u7684\u5185\u5b58\u4f7f\u7528\u60c5\u51b5\uff0c\u65b9\u6cd5\u5217\u8868\u663e\u793a\u7684\u6570\u636e\u4e0e\u706b\u7130\u56fe\u4e2d\u7684\u76f8\u540c\uff0c\u4f46\u663e\u793a\u7684\u65b9\u5f0f\u662f\u8868\u683c\uff0c\u53ef\u4f9b\u6d4f\u89c8\u5404\u4e2a\u65b9\u6cd5\u548c\u7b5b\u9009\u8c03\u7528\uff1a<\/p>\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" loading=\"lazy\" width=\"2800\" height=\"1596\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2023\/02\/8-2800x1596.png\" alt=\"\" class=\"wp-image-320481\"><\/figure>\n<h2><strong>\u963b\u585e\u5206\u6790\u5668<\/strong><\/h2>\n<p><a href=\"https:\/\/www.jetbrains.com\/help\/go\/blocking-profiler.html\" target=\"_blank\" rel=\"noopener\">\u963b\u585e\u5206\u6790\u5668<\/a>\u5bf9\u4e8e\u5b9a\u4f4d\u672a\u7f13\u51b2\u6216\u5b8c\u6574\u901a\u9053\u3001<code>sync.Mutex<\/code> \u9501\u6216\u5176\u4ed6\u74f6\u9888\u975e\u5e38\u6709\u7528\uff0c\u56e0\u4e3a\u5b83\u4f1a\u663e\u793a goroutine \u672a\u8fd0\u884c\uff08\u7b49\u5f85\uff09\u7684\u65f6\u95f4\u6bb5\u3002 \u5c06\u4ee5\u4e0b\u4ee3\u7801\u6dfb\u52a0\u5230 <code>main.go<\/code> \u540e\uff0c\u9009\u62e9 <em>Profile with Blocking Profiler<\/em>\uff08\u4f7f\u7528\u963b\u585e\u5206\u6790\u5668\u5206\u6790\uff09\u8fd0\u884c\u5206\u6790\u5668\uff1a<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"golang\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">package main\n\nimport (\n    \"sync\"\n    \"time\"\n)\n\nvar ch = make(chan int)\n\nfunc Block() {\n    \/\/ WaitGroup is used to make the function wait for G1 and G2 to finish\n    wg := sync.WaitGroup{}\n    wg.Add(2)\n    go G1(&amp;wg)\n    go G2(&amp;wg)\n    wg.Wait()\n}\n\nfunc G1(wg *sync.WaitGroup) {\n    \/\/ Write to the channel\n    ch &lt;- 100\n    wg.Done()\n}\n\nfunc G2(wg *sync.WaitGroup) {\n    \/\/ Sleep for 1 second\n    time.Sleep(time.Second)\n    \/\/ Read from the channel\n    &lt;-ch\n    wg.Done()\n}\n\n<\/pre>\n<p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c<code>G1<\/code> \u88ab\u963b\u585e\uff0c\u56e0\u4e3a\u5b83\u65e0\u6cd5\u5728 <code>G2<\/code> \u4ece\u4f11\u7720\u4e2d\u9192\u6765\u5e76\u51c6\u5907\u597d\u8bfb\u53d6\u4e4b\u524d\u5199\u5165\u901a\u9053\u3002<\/p>\n<p>\u4ee5\u4e0b\u6d4b\u8bd5\u4f1a\u68c0\u67e5 <code>Block<\/code> \u51fd\u6570\uff1a<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"golang\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">func TestBlock(t *testing.T) {\n\/\/ Call the block function\n    Block()\n}\n<\/pre>\n<p>\u4e0b\u9762\u7684\u706b\u7130\u56fe\u663e\u793a\u4e86\u51fd\u6570\u7b49\u5f85\u7684\u65f6\u95f4\uff08\u5982\u679c\u9009\u62e9\u4e86 <em>Delay<\/em>\uff08\u5ef6\u8fdf\uff09\uff09\u6216\u4e89\u7528\u7684\u6570\u91cf\uff08\u5982\u679c\u9009\u62e9\u4e86 Contentions\uff08\u4e89\u7528\uff09\uff09\u4ee5\u53ca\u8c03\u7528\u6811\u4e2d\u5c5e\u4e8e\u540c\u4e00\u7236\u9879\u7684\u8fc7\u7a0b\u4e4b\u95f4\u7684\u767e\u5206\u6bd4\u5dee\u5f02\uff1a<\/p>\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" loading=\"lazy\" width=\"2800\" height=\"1596\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2023\/02\/9-1-2800x1596.png\" alt=\"\" class=\"wp-image-320503\"><\/figure>\n<p>\u65b9\u6cd5\u5217\u8868\u663e\u793a\u4e89\u7528\u7684\u6570\u91cf\uff0c\u8c03\u7528\u6811\u663e\u793a\u6bcf\u4e2a\u51fd\u6570\u4e2d\u5177\u6709\u5ef6\u8fdf\u7684\u4e89\u7528\uff0c\u5177\u4f53\u53d6\u51b3\u4e8e\u9009\u62e9\u7684\u662f Delay\uff08\u5ef6\u8fdf\uff09\u8fd8\u662f Contentions\uff08\u4e89\u7528\uff09\u3002<\/p>\n<h2><strong>Mutex \u5206\u6790\u5668<\/strong><\/h2>\n<p>\u60a8\u53ef\u4ee5\u4f7f\u7528<a href=\"https:\/\/www.jetbrains.com\/help\/go\/mutex-profiler.html\" target=\"_blank\" rel=\"noopener\">\u4e92\u65a5\u5206\u6790\u5668<\/a>\u5206\u6790\u5e76\u53d1\u4ee3\u7801\uff0c\u5e76\u67e5\u770b\u5177\u6709\u5e76\u53d1\u4e92\u65a5\u7684 goroutine \u7684\u5806\u6808\u8ddf\u8e2a\u7247\u6bb5\uff0c\u4e86\u89e3\u7ebf\u7a0b\u963b\u585e\u7684\u539f\u56e0\u3002<\/p>\n<p>\u4e92\u65a5\u5206\u6790\u5668\u4e5f\u6709\u4e09\u79cd\u7c7b\u578b\u7684\u6570\u636e\u8868\u793a\u5f62\u5f0f\u3002 \u60a8\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u793a\u4f8b\u751f\u6210\u5206\u6790\u6587\u4ef6\uff1a<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"golang\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">package main\n\nimport (\n    \"sync\"\n    \"time\"\n)\n\nfunc Mutx(s int) int {\n    var mu sync.Mutex\n    wg := sync.WaitGroup{}\n    wg.Add(1000 * 1000)\n    \/\/ Launching 1,000,000 goroutines \n    for i := 0; i &lt; 1000*1000; i++ {\n        go func(i int) {\n            \/\/ Locking the mutex\n            mu.Lock()\n            \/\/ Unlocking the mutex\n            defer mu.Unlock()\n            defer wg.Done()\n            s++\n        }(i)\n    }\n    wg.Wait()\n    return s\n}<\/pre>\n<p>\u4e0a\u9762\u7684\u4ee3\u7801\u542f\u52a8\u4e86\u4e00\u767e\u4e07\u4e2a goroutine\uff0c\u5b83\u4eec\u90fd\u5c1d\u8bd5\u5c06\u5171\u4eab\u53d8\u91cf s \u9012\u589e 1\u3002 \u4e3a\u6b64\uff0c\u521b\u5efa\u4e00\u4e2a\u4e92\u65a5\uff0c\u5e76\u5728\u9012\u589e\u53d8\u91cf\u4e4b\u524d\u5c06\u5176\u9501\u5b9a\u3002 \u9012\u589e\u53d8\u91cf\u540e\uff0c\u5c06\u4e92\u65a5\u89e3\u9501\u3002 \u8bbe\u7f6e\u4e00\u4e2a <code>WaitGroup<\/code> \u4ee5\u7b49\u5f85\u6240\u6709 goroutine \u6267\u884c\u5b8c\u6210\u3002<\/p>\n<p>\u4f7f\u7528\u4ee5\u4e0b\u6d4b\u8bd5\u68c0\u67e5 <code>Mutx<\/code> \u51fd\u6570\uff1a<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"golang\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">package main\n\nimport \"testing\"\n\nfunc TestMutx(t *testing.T) {\n    result := Mutx(4)\n    t.Logf(\"%d\", result)\n}<\/pre>\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" loading=\"lazy\" width=\"2800\" height=\"1596\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2023\/02\/10-2800x1596.png\" alt=\"\" class=\"wp-image-320514\"><\/figure>\n<p>\u8981\u8fd0\u884c\u6b64\u5206\u6790\u5668\uff0c\u8bf7\u6253\u5f00 <code>_test.go<\/code> \u6587\u4ef6\uff0c\u7136\u540e\u70b9\u51fb\u95f4\u8ddd\u533a\u57df\u4e2d\u7684 <em>Run<\/em>\uff08\u8fd0\u884c\uff09\u56fe\u6807\u5e76\u9009\u62e9 <em>Profile with Mutex Profiler<\/em>\uff08\u4f7f\u7528\u4e92\u65a5\u5206\u6790\u5668\u5206\u6790\uff09\u3002 \u706b\u7130\u56fe\u663e\u793a\u5ef6\u8fdf\u7684\u6570\u91cf\u6216\u7b49\u5f85\u7684\u65f6\u95f4\uff0c\u5177\u4f53\u53d6\u51b3\u4e8e\u9009\u62e9\u7684\u662f <em>Contentions<\/em>\uff08\u4e89\u7528\uff09\u8fd8\u662f <em>Delay<\/em>\uff08\u5ef6\u8fdf\uff09\u3002<\/p>\n<h2><strong>\u603b\u7ed3<\/strong><\/h2>\n<p>\u5206\u6790\u662f\u4e00\u9879\u5173\u952e\u8f6f\u4ef6\u4f18\u5316\u6280\u672f\uff0c\u638c\u63e1\u57fa\u7840\u77e5\u8bc6\u5373\u53ef\u63d0\u9ad8\u6548\u7387\u3002 <a href=\"https:\/\/www.jetbrains.com\/go\/\" target=\"_blank\" rel=\"noopener\">GoLand<\/a> \u7b49\u5de5\u5177\u5c06\u4f7f\u8fd9\u4e2a\u8fc7\u7a0b\u66f4\u52a0\u9ad8\u6548\u76f4\u89c2\u3002 \u53e6\u5916\uff0c\u8fd8\u8bf7\u7262\u8bb0\uff0c\u5206\u6790\u53ea\u662f\u5e2e\u52a9\u60a8\u7406\u89e3\u4ee3\u7801\u7684\u5de5\u5177\u3002 \u5b9e\u9645\u5de5\u4f5c\u4ecd\u5728\u60a8\u8fd9\u8fb9\uff1aCPU\u3001\u5185\u5b58\u3001\u4e92\u65a5\u548c\u963b\u585e\u5206\u6790\u5668\u53ef\u4ee5\u5e2e\u52a9\u60a8\u68c0\u67e5\u4ee3\u7801\u662f\u5426\u9ad8\u6548\uff0c\u4f46\u5982\u679c\u53d1\u73b0\u5b58\u5728\u4e0d\u8db3\u7684\u5730\u65b9\uff0c\u8fd8\u662f\u8981\u7531\u60a8\u6765\u4e0a\u624b\u4fee\u6b63\u3002<\/p>\n<p>\u8981\u8be6\u7ec6\u4e86\u89e3 Go \u4e2d\u7684\u5206\u6790\u548c\u6027\u80fd\uff0c\u8bf7\u67e5\u9605<a href=\"https:\/\/go.dev\/doc\/diagnostics#profiling\" target=\"_blank\" rel=\"noopener\">\u5b98\u65b9\u6587\u6863<\/a>\u3002<\/p>\n\n\n<p><\/p>\n\n\n\n<p>\u672c\u535a\u6587\u82f1\u6587\u539f\u4f5c\u8005\uff1a<\/p>\n\n\n    <div class=\"about-author \">\n        <div class=\"about-author__box\">\n            <div class=\"row\">\n                <div class=\"about-author__box-img\">\n                    <img decoding=\"async\" src=\"https:\/\/secure.gravatar.com\/avatar\/?s=200&#038;r=g\" width=\"200\" height=\"200\" alt=\"\" loading=\"lazy\"  class=\"avatar avatar-200 wp-user-avatar wp-user-avatar-200 photo avatar-default\">\n                <\/div>\n                <div class=\"about-author__box-text\">\n                                                        <\/div>\n            <\/div>\n        <\/div>\n    <\/div>\n","protected":false},"author":1297,"featured_media":316636,"comment_status":"closed","ping_status":"closed","template":"","categories":[2347],"tags":[1123,489],"cross-post-tag":[],"acf":[],"_links":{"self":[{"href":"https:\/\/blog.jetbrains.com\/zh-hans\/wp-json\/wp\/v2\/go\/323107"}],"collection":[{"href":"https:\/\/blog.jetbrains.com\/zh-hans\/wp-json\/wp\/v2\/go"}],"about":[{"href":"https:\/\/blog.jetbrains.com\/zh-hans\/wp-json\/wp\/v2\/types\/go"}],"author":[{"embeddable":true,"href":"https:\/\/blog.jetbrains.com\/zh-hans\/wp-json\/wp\/v2\/users\/1297"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.jetbrains.com\/zh-hans\/wp-json\/wp\/v2\/comments?post=323107"}],"version-history":[{"count":4,"href":"https:\/\/blog.jetbrains.com\/zh-hans\/wp-json\/wp\/v2\/go\/323107\/revisions"}],"predecessor-version":[{"id":357780,"href":"https:\/\/blog.jetbrains.com\/zh-hans\/wp-json\/wp\/v2\/go\/323107\/revisions\/357780"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.jetbrains.com\/zh-hans\/wp-json\/wp\/v2\/media\/316636"}],"wp:attachment":[{"href":"https:\/\/blog.jetbrains.com\/zh-hans\/wp-json\/wp\/v2\/media?parent=323107"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.jetbrains.com\/zh-hans\/wp-json\/wp\/v2\/categories?post=323107"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.jetbrains.com\/zh-hans\/wp-json\/wp\/v2\/tags?post=323107"},{"taxonomy":"cross-post-tag","embeddable":true,"href":"https:\/\/blog.jetbrains.com\/zh-hans\/wp-json\/wp\/v2\/cross-post-tag?post=323107"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}