{"id":205710,"date":"2025-06-12T08:11:55","date_gmt":"2025-06-12T00:11:55","guid":{"rendered":"https:\/\/server.hk\/cnblog\/205710\/"},"modified":"2025-06-12T08:11:55","modified_gmt":"2025-06-12T00:11:55","slug":"%e5%a6%82%e4%bd%95%e5%9c%a8golang%e6%a1%86%e6%9e%b6%e4%b8%ad%e8%bf%9b%e8%a1%8c%e9%ab%98%e5%b9%b6%e5%8f%91%e5%9c%ba%e6%99%af%e4%b8%8b%e7%9a%84%e5%8e%8b%e5%8a%9b%e6%b5%8b%e8%af%95","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/205710\/","title":{"rendered":"\u5982\u4f55\u5728golang\u6846\u67b6\u4e2d\u8fdb\u884c\u9ad8\u5e76\u53d1\u573a\u666f\u4e0b\u7684\u538b\u529b\u6d4b\u8bd5"},"content":{"rendered":"<p>\u6765\u5230GOLANG\u7684\u5927\u5bb6\uff0c\u76f8\u4fe1\u90fd\u662f\u7f16\u7a0b\u5b66\u4e60\u7231\u597d\u8005\uff0c\u5e0c\u671b\u5728\u8fd9\u91cc\u5b66\u4e60GOLANG\u76f8\u5173\u7f16\u7a0b\u77e5\u8bc6\u3002\u4e0b\u9762\u672c\u7bc7\u6587\u7ae0\u5c31\u6765\u5e26\u5927\u5bb6\u804a\u804a\u300a\u5982\u4f55\u5728golang\u6846\u67b6\u4e2d\u8fdb\u884c\u9ad8\u5e76\u53d1\u573a\u666f\u4e0b\u7684\u538b\u529b\u6d4b\u8bd5\u300b\uff0c\u4ecb\u7ecd\u4e00\u4e0b\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff0c\u52a9\u529b\u5b9e\u6218\u5f00\u53d1\uff01,\u5728 Go \u6846\u67b6\u4e2d\u8fdb\u884c\u9ad8\u5e76\u53d1\u573a\u666f\u4e0b\u7684\u538b\u529b\u6d4b\u8bd5\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u6b65\u9aa4\u8fdb\u884c\uff1a\u5b89\u88c5 Go\u3001GoConvey \u548c httptest\u3002\u521b\u5efa\u6d4b\u8bd5\u6848\u4f8b\uff0c\u5305\u542b HTTP \u5904\u7406\u51fd\u6570\u3002\u5b89\u88c5 conv-stress \u5b50\u547d\u4ee4\u5e76\u4f7f\u7528\u5b83\u8fd0\u884c\u6d4b\u8bd5\uff0c\u6307\u5b9a\u5e76\u884c\u5de5\u4f5c\u8fdb\u7a0b\u6570\u91cf\u3001\u8fed\u4ee3\u6b21\u6570\u3001\u9884\u70ed\u9636\u6bb5\u548c\u62a5\u544a\u9891\u7387\u3002,<br \/>\n<img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241026\/1729947381671ce6f500ab7.jpg\" class=\"aligncenter\">,<br \/>\n<strong>\u5728 Go \u6846\u67b6\u4e2d\u8fdb\u884c\u9ad8\u5e76\u53d1\u573a\u666f\u4e0b\u7684\u538b\u529b\u6d4b\u8bd5<\/strong>,<br \/>\n<strong>\u5f15\u8a00<\/strong>,\u538b\u529b\u6d4b\u8bd5\u5bf9\u4e8e\u8bc4\u4f30 Web \u5e94\u7528\u7a0b\u5e8f\u5728\u9ad8\u5e76\u53d1\u573a\u666f\u4e0b\u7684\u6027\u80fd\u81f3\u5173\u91cd\u8981\u3002\u5728\u672c\u6587\u4e2d\uff0c\u6211\u4eec\u5c06\u5c55\u793a\u5982\u4f55\u4f7f\u7528 Go \u6846\u67b6\u8fdb\u884c\u538b\u529b\u6d4b\u8bd5\u3002,<br \/>\n<strong>1. \u5b89\u88c5\u5fc5\u8981\u7684\u5de5\u5177<\/strong>,<br \/>\n<strong>2. \u521b\u5efa\u6d4b\u8bd5\u6848\u4f8b<\/strong>,\u9996\u5148\uff0c\u521b\u5efa\u4e00\u4e2a\u5305\u542b\u88ab\u6d4b\u51fd\u6570\u7684 Go \u6d4b\u8bd5\u6587\u4ef6\u3002\u5bf9\u4e8e\u672c\u4f8b\uff0c\u6211\u4eec\u521b\u5efa\u4e00\u4e2a\u5904\u7406 HTTP \u8bf7\u6c42\u7684\u51fd\u6570\uff1a,<br \/>\n<strong>3. \u4f7f\u7528 GoConvey \u8fdb\u884c\u538b\u529b\u6d4b\u8bd5<\/strong>,GoConvey \u662f\u4e00\u4e2a Go \u5355\u5143\u6d4b\u8bd5\u6846\u67b6\uff0c\u63d0\u4f9b\u538b\u529b\u6d4b\u8bd5\u529f\u80fd\u3002\u8981\u4f7f\u7528 GoConvey \u8fdb\u884c\u538b\u529b\u6d4b\u8bd5\uff0c\u9996\u5148\u5b89\u88c5 conv-stress \u5b50\u547d\u4ee4\uff1a,\u7136\u540e\uff0c\u4f7f\u7528<br \/>\n<code>conv-stress<\/code> \u8fd0\u884c\u6d4b\u8bd5\uff1a,<br \/>\n<strong>4. \u5b9e\u6218\u6848\u4f8b<\/strong>,\u4ee5\u4e0b\u662f\u4e00\u4e2a\u5b9e\u6218\u6848\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u4f7f\u7528<br \/>\n<code>conv-stress<\/code> \u5bf9 Web \u670d\u52a1\u8fdb\u884c\u538b\u529b\u6d4b\u8bd5\uff1a,\u538b\u529b\u6d4b\u8bd5\u5c06\u4f7f\u7528 50 \u4e2a\u5e76\u53d1\u5de5\u4f5c\u8fdb\u7a0b\uff0c\u6bcf\u4e2a\u5de5\u4f5c\u8fdb\u7a0b\u8fed\u4ee3 1000 \u6b21\uff0c\u9884\u70ed\u9636\u6bb5\u6301\u7eed 10 \u79d2\uff0c\u5e76\u6bcf\u79d2\u8f93\u51fa\u4e00\u6b21\u62a5\u544a\u3002,<br \/>\n<strong>\u7ed3\u8bba<\/strong>,\u4f7f\u7528 Go \u6846\u67b6\u548c GoConvey \u8fdb\u884c\u538b\u529b\u6d4b\u8bd5\u53ef\u4ee5\u5e2e\u52a9\u60a8\u8bc4\u4f30\u5e94\u7528\u7a0b\u5e8f\u7684\u9ad8\u5e76\u53d1\u6027\u80fd\u3002\u901a\u8fc7\u8c03\u6574\u53c2\u6570\uff0c\u60a8\u53ef\u4ee5\u6839\u636e\u9700\u8981\u5b9a\u5236\u6d4b\u8bd5\u4ee5\u6ee1\u8db3\u60a8\u7684\u7279\u5b9a\u9700\u6c42\u3002,\u5230\u8fd9\u91cc\uff0c\u6211\u4eec\u4e5f\u5c31\u8bb2\u5b8c\u4e86\u300a\u5982\u4f55\u5728golang\u6846\u67b6\u4e2d\u8fdb\u884c\u9ad8\u5e76\u53d1\u573a\u666f\u4e0b\u7684\u538b\u529b\u6d4b\u8bd5\u300b\u7684\u5185\u5bb9\u4e86\u3002\u4e2a\u4eba\u8ba4\u4e3a\uff0c\u57fa\u7840\u77e5\u8bc6\u7684\u5b66\u4e60\u548c\u5de9\u56fa\uff0c\u662f\u4e3a\u4e86\u66f4\u597d\u7684\u5c06\u5176\u8fd0\u7528\u5230\u9879\u76ee\u4e2d\uff0c\u6b22\u8fce\u5173\u6ce8GOLANG\u516c\u4f17\u53f7\uff0c\u5e26\u4f60\u4e86\u89e3\u66f4\u591a\u5173\u4e8e\u538b\u529b\u6d4b\u8bd5,\u5e76\u53d1\u6d4b\u8bd5\u7684\u77e5\u8bc6\u70b9\uff01,\u4f7f\u7528<br \/>\n<code>conv-stress<\/code> \u8fd0\u884c\u538b\u529b\u6d4b\u8bd5\uff1a,\u521b\u5efa<br \/>\n<code>main.go<\/code> \u6587\u4ef6\uff0c\u4f7f\u7528<br \/>\n<code>net\/http<\/code> \u542f\u52a8 Web \u670d\u52a1\u5e76\u76d1\u542c\u7aef\u53e3<br \/>\n<code>8080<\/code>\u3002,GoConvey,Go,httptest,\u6765\u5230GOLANG\u7684\u5927\u5bb6\uff0c\u76f8\u4fe1\u90fd\u662f\u7f16\u7a0b\u5b66\u4e60\u7231\u597d\u8005\uff0c\u5e0c\u671b\u5728\u8fd9\u91cc\u5b66\u4e60GOLANG\u76f8\u5173\u7f16\u7a0b\u77e5\u8bc6\u3002\u4e0b\u9762\u672c\u7bc7\u6587\u7ae0\u5c31\u6765\u5e26\u5927\u5bb6\u804a\u804a\u300a\u5982\u4f55\u5728golang\u6846\u67b6\u4e2d\u8fdb\u884c\u9ad8\u5e76\u53d1\u573a\u666f\u4e0b\u7684\u538b\u529b\u6d4b\u8bd5\u300b\uff0c\u4ecb\u7ecd\u4e00\u4e0b\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff0c\u52a9\u529b\u5b9e\u6218\u5f00\u53d1\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\u5728golang\u6846\u67b6\u4e2d\u8fdb\u884c\u9ad8\u5e76\u53d1\u573a\u666f\u4e0b\u7684\u538b\u529b\u6d4b\u8bd5<\/span><\/p>\n<h1>\u5982\u4f55\u5728golang\u6846\u67b6\u4e2d\u8fdb\u884c\u9ad8\u5e76\u53d1\u573a\u666f\u4e0b\u7684\u538b\u529b\u6d4b\u8bd5<\/h1>\n<p><span>2024-10-26 20:56:42<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u6765\u5230GOLANG\u7684\u5927\u5bb6\uff0c\u76f8\u4fe1\u90fd\u662f\u7f16\u7a0b\u5b66\u4e60\u7231\u597d\u8005\uff0c\u5e0c\u671b\u5728\u8fd9\u91cc\u5b66\u4e60GOLANG\u76f8\u5173\u7f16\u7a0b\u77e5\u8bc6\u3002\u4e0b\u9762\u672c\u7bc7\u6587\u7ae0\u5c31\u6765\u5e26\u5927\u5bb6\u804a\u804a\u300a\u5982\u4f55\u5728golang\u6846\u67b6\u4e2d\u8fdb\u884c\u9ad8\u5e76\u53d1\u573a\u666f\u4e0b\u7684\u538b\u529b\u6d4b\u8bd5\u300b\uff0c\u4ecb\u7ecd\u4e00\u4e0b\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff0c\u52a9\u529b\u5b9e\u6218\u5f00\u53d1\uff01<\/p>\n<p>\u5728 Go \u6846\u67b6\u4e2d\u8fdb\u884c\u9ad8\u5e76\u53d1\u573a\u666f\u4e0b\u7684\u538b\u529b\u6d4b\u8bd5\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u6b65\u9aa4\u8fdb\u884c\uff1a\u5b89\u88c5 Go\u3001GoConvey \u548c httptest\u3002\u521b\u5efa\u6d4b\u8bd5\u6848\u4f8b\uff0c\u5305\u542b HTTP \u5904\u7406\u51fd\u6570\u3002\u5b89\u88c5 conv-stress \u5b50\u547d\u4ee4\u5e76\u4f7f\u7528\u5b83\u8fd0\u884c\u6d4b\u8bd5\uff0c\u6307\u5b9a\u5e76\u884c\u5de5\u4f5c\u8fdb\u7a0b\u6570\u91cf\u3001\u8fed\u4ee3\u6b21\u6570\u3001\u9884\u70ed\u9636\u6bb5\u548c\u62a5\u544a\u9891\u7387\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241026\/1729947381671ce6f500ab7.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>\u5728 Go \u6846\u67b6\u4e2d\u8fdb\u884c\u9ad8\u5e76\u53d1\u573a\u666f\u4e0b\u7684\u538b\u529b\u6d4b\u8bd5<\/strong><\/p>\n<p><strong>\u5f15\u8a00<\/strong><\/p>\n<p>\u538b\u529b\u6d4b\u8bd5\u5bf9\u4e8e\u8bc4\u4f30 Web \u5e94\u7528\u7a0b\u5e8f\u5728\u9ad8\u5e76\u53d1\u573a\u666f\u4e0b\u7684\u6027\u80fd\u81f3\u5173\u91cd\u8981\u3002\u5728\u672c\u6587\u4e2d\uff0c\u6211\u4eec\u5c06\u5c55\u793a\u5982\u4f55\u4f7f\u7528 Go \u6846\u67b6\u8fdb\u884c\u538b\u529b\u6d4b\u8bd5\u3002<\/p>\n<p><strong>1. \u5b89\u88c5\u5fc5\u8981\u7684\u5de5\u5177<\/strong><\/p>\n<ul>\n<li>Go<\/li>\n<li>GoConvey<\/li>\n<li>httptest<\/li>\n<\/ul>\n<p><strong>2. \u521b\u5efa\u6d4b\u8bd5\u6848\u4f8b<\/strong><\/p>\n<p>\u9996\u5148\uff0c\u521b\u5efa\u4e00\u4e2a\u5305\u542b\u88ab\u6d4b\u51fd\u6570\u7684 Go \u6d4b\u8bd5\u6587\u4ef6\u3002\u5bf9\u4e8e\u672c\u4f8b\uff0c\u6211\u4eec\u521b\u5efa\u4e00\u4e2a\u5904\u7406 HTTP \u8bf7\u6c42\u7684\u51fd\u6570\uff1a<\/p>\n<pre>import (\n    \"fmt\"\n    \"net\/http\"\n    \"net\/http\/httptest\"\n    \"testing\"\n)\n\nfunc TestMyHandler(t *testing.T) {\n    conv := So(t)\n\n    req, err := http.NewRequest(\"GET\", \"\/\", nil)\n    conv.So(err, ShouldBeNil)\n\n    rr := httptest.NewRecorder()\n    handler := http.HandlerFunc(MyHandler)\n\n    handler.ServeHTTP(rr, req)\n\n    conv.So(rr.Code, ShouldEqual, http.StatusOK)\n}<\/pre>\n<p><strong>3. \u4f7f\u7528 GoConvey \u8fdb\u884c\u538b\u529b\u6d4b\u8bd5<\/strong><\/p>\n<p>GoConvey \u662f\u4e00\u4e2a Go \u5355\u5143\u6d4b\u8bd5\u6846\u67b6\uff0c\u63d0\u4f9b\u538b\u529b\u6d4b\u8bd5\u529f\u80fd\u3002\u8981\u4f7f\u7528 GoConvey \u8fdb\u884c\u538b\u529b\u6d4b\u8bd5\uff0c\u9996\u5148\u5b89\u88c5 conv-stress \u5b50\u547d\u4ee4\uff1a<\/p>\n<pre>go get github.com\/smartystreets\/goconvey\/cmd\/conv-stress<\/pre>\n<p>\u7136\u540e\uff0c\u4f7f\u7528 <code>conv-stress<\/code> \u8fd0\u884c\u6d4b\u8bd5\uff1a<\/p>\n<pre>conv-stress \\\n    -wg={numWorkers} \\                                             \/\/ \u5e76\u884c\u5de5\u4f5c\u8fdb\u7a0b\u6570\u91cf\n    -loops={numIterations} \\                                        \/\/ \u6bcf\u4e2a\u5de5\u4f5c\u8fdb\u7a0b\u7684\u8fed\u4ee3\u6b21\u6570\n    -rampup={numWorkers} \\                                         \/\/ \u5e76\u884c\u5de5\u4f5c\u8fdb\u7a0b\u7684\u9884\u70ed\u9636\u6bb5\n    -report=1s \\                                                  \/\/ \u6bcf\u79d2\u8f93\u51fa\u4e00\u6b21\u62a5\u544a\n    -port={testPort} \\                                            \/\/ Web \u670d\u52a1\u76d1\u542c\u7684\u7aef\u53e3\n    .\/my_test.go:TestMyHandler \\                                   \/\/ \u8981\u6d4b\u8bd5\u7684\u6d4b\u8bd5\u51fd\u6570<\/pre>\n<p><strong>4. \u5b9e\u6218\u6848\u4f8b<\/strong><\/p>\n<p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u5b9e\u6218\u6848\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u4f7f\u7528 <code>conv-stress<\/code> \u5bf9 Web \u670d\u52a1\u8fdb\u884c\u538b\u529b\u6d4b\u8bd5\uff1a<\/p>\n<ul>\n<li>\u521b\u5efa <code>main.go<\/code> \u6587\u4ef6\uff0c\u4f7f\u7528 <code>net\/http<\/code> \u542f\u52a8 Web \u670d\u52a1\u5e76\u76d1\u542c\u7aef\u53e3 <code>8080<\/code>\u3002<\/li>\n<li>\u4f7f\u7528 <code>conv-stress<\/code> \u8fd0\u884c\u538b\u529b\u6d4b\u8bd5\uff1a<\/li>\n<\/ul>\n<pre>conv-stress \\\n    -wg=50 \\\n    -loops=1000 \\\n    -rampup=10 \\\n    -report=1s \\\n    -port=8080 \\\n    .\/main_test.go:TestMyHandler<\/pre>\n<p>\u538b\u529b\u6d4b\u8bd5\u5c06\u4f7f\u7528 50 \u4e2a\u5e76\u53d1\u5de5\u4f5c\u8fdb\u7a0b\uff0c\u6bcf\u4e2a\u5de5\u4f5c\u8fdb\u7a0b\u8fed\u4ee3 1000 \u6b21\uff0c\u9884\u70ed\u9636\u6bb5\u6301\u7eed 10 \u79d2\uff0c\u5e76\u6bcf\u79d2\u8f93\u51fa\u4e00\u6b21\u62a5\u544a\u3002<\/p>\n<p><strong>\u7ed3\u8bba<\/strong><\/p>\n<p>\u4f7f\u7528 Go \u6846\u67b6\u548c GoConvey \u8fdb\u884c\u538b\u529b\u6d4b\u8bd5\u53ef\u4ee5\u5e2e\u52a9\u60a8\u8bc4\u4f30\u5e94\u7528\u7a0b\u5e8f\u7684\u9ad8\u5e76\u53d1\u6027\u80fd\u3002\u901a\u8fc7\u8c03\u6574\u53c2\u6570\uff0c\u60a8\u53ef\u4ee5\u6839\u636e\u9700\u8981\u5b9a\u5236\u6d4b\u8bd5\u4ee5\u6ee1\u8db3\u60a8\u7684\u7279\u5b9a\u9700\u6c42\u3002<\/p>\n<p>\u5230\u8fd9\u91cc\uff0c\u6211\u4eec\u4e5f\u5c31\u8bb2\u5b8c\u4e86\u300a\u5982\u4f55\u5728golang\u6846\u67b6\u4e2d\u8fdb\u884c\u9ad8\u5e76\u53d1\u573a\u666f\u4e0b\u7684\u538b\u529b\u6d4b\u8bd5\u300b\u7684\u5185\u5bb9\u4e86\u3002\u4e2a\u4eba\u8ba4\u4e3a\uff0c\u57fa\u7840\u77e5\u8bc6\u7684\u5b66\u4e60\u548c\u5de9\u56fa\uff0c\u662f\u4e3a\u4e86\u66f4\u597d\u7684\u5c06\u5176\u8fd0\u7528\u5230\u9879\u76ee\u4e2d\uff0c\u6b22\u8fce\u5173\u6ce8GOLANG\u516c\u4f17\u53f7\uff0c\u5e26\u4f60\u4e86\u89e3\u66f4\u591a\u5173\u4e8e\u538b\u529b\u6d4b\u8bd5,\u5e76\u53d1\u6d4b\u8bd5\u7684\u77e5\u8bc6\u70b9\uff01<\/p>\n<dl>\n<dt>\n <\/dt>\n<dd>\n   PHP \u51fd\u6570\u4e2d\u5982\u4f55\u68c0\u6d4b\u53d8\u91cf\u7684\u7c7b\u578b\uff1f\n <\/dd>\n<\/dl>\n<dl>\n<dt>\n <\/dt>\n<dd>\n   \u5982\u4f55\u7528 JavaScript \u5b9e\u73b0\u9875\u9762\u6eda\u8f6e\u6eda\u52a8\u4e00\u683c\u89e6\u53d1\u5206\u9875\u6548\u679c\uff1f\n <\/dd>\n<\/dl>\n","protected":false},"excerpt":{"rendered":"<p>\u6765\u5230GOLANG\u7684\u5927\u5bb6\uff0c\u76f8\u4fe1\u90fd\u662f&#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-205710","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/205710","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=205710"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/205710\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=205710"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=205710"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=205710"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}