{"id":207147,"date":"2025-07-08T09:03:39","date_gmt":"2025-07-08T01:03:39","guid":{"rendered":"https:\/\/server.hk\/cnblog\/207147\/"},"modified":"2025-07-08T09:03:39","modified_gmt":"2025-07-08T01:03:39","slug":"%e7%9b%b4%e6%8e%a5%e4%bd%bf%e7%94%a8testing-benchmark%e6%97%b6%e8%ae%be%e7%bd%aebenchtime","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/207147\/","title":{"rendered":"\u76f4\u63a5\u4f7f\u7528testing.Benchmark()\u65f6\u8bbe\u7f6ebenchtime"},"content":{"rendered":"<p><b><\/b> <\/p>\n<p>\u5f53\u524d\u4f4d\u7f6e\uff1a <span>&gt;<\/span> <span>&gt;<\/span> <span>&gt;<\/span> <span>&gt;<\/span> <span>\u76f4\u63a5\u4f7f\u7528testing.Benchmark()\u65f6\u8bbe\u7f6ebenchtime<\/span><\/p>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-20 13:00:35<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>Golang\u5c0f\u767d\u4e00\u679a\uff0c\u6b63\u5728\u4e0d\u65ad\u5b66\u4e60\u79ef\u7d2f\u77e5\u8bc6\uff0c\u73b0\u5c06\u5b66\u4e60\u5230\u7684\u77e5\u8bc6\u8bb0\u5f55\u4e00\u4e0b\uff0c\u4e5f\u662f\u5c06\u6211\u7684\u6240\u5f97\u5206\u4eab\u7ed9\u5927\u5bb6\uff01\u800c\u4eca\u5929\u8fd9\u7bc7\u6587\u7ae0\u300a\u76f4\u63a5\u4f7f\u7528testing.Benchmark()\u65f6\u8bbe\u7f6ebenchtime\u300b\u5e26\u5927\u5bb6\u6765\u4e86\u89e3\u4e00\u4e0b##content_title##\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff0c\u4ece\u800c\u5f25\u8865\u81ea\u5df1\u7684\u4e0d\u8db3\uff0c\u52a9\u529b\u5b9e\u6218\u5f00\u53d1\uff01<\/p>\n<p><\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>\u5728 go \u4e2d\uff0c\u6211\u5728 <code>main()<\/code> \u4e2d\u624b\u52a8\u8fd0\u884c\u6d4b\u8bd5\uff0c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<pre>func main() {\n  br := testing.benchmark(benchmarkconnectionopen)\n  fmt.println(br.string() + br.memstring())\n}<\/pre>\n<p>\u6211\u60f3\u505a\u7684\u662f\u4e3a\u6240\u6709\u57fa\u51c6\u8bbe\u7f6e\u201c\u57fa\u51c6\u65f6\u95f4\u201d\u3002\u636e\u6211\u6240\u77e5\uff0c\u5728\u6d4b\u8bd5\u65f6\uff08\u4f8b\u5982\u8fd0\u884c <code>go test<\/code>\uff09\uff0c\u5b83\u4f1a\u4f5c\u4e3a\u5982\u4e0b\u6807\u5fd7\u4f20\u5165\uff1a<\/p>\n<pre>-benchtime=100x<\/pre>\n<p>\u4f46\u662f\u5f53\u6211\u521a\u521a\u5728 main() \u4e2d\u8fd0\u884c\u65f6\u5982\u4f55\u8bbe\u7f6e\u8fd9\u4e2a\u6807\u5fd7\uff1f\u6211\u5c1d\u8bd5\u8fc7 <code>flag.set()<\/code> \uff08\u5982\u4e0b\uff09\uff0c\u4f46\u6ca1\u6709\u9ab0\u5b50\u3002<\/p>\n<pre>flag.Set(\"test.benchtime\", \"100x\")<\/pre>\n<p> <\/p>\n<h2>\u89e3\u51b3\u65b9\u6848<\/h2>\n<p> <\/p>\n<p>\u60f3\u901a\u4e86\uff01\u533b\u751f\u4eec\u50ac\u4fc3\u4e86\u6211\uff0c\u4f46\u82b1\u4e86\u70b9\u65f6\u95f4\u624d\u628a\u5b83\u62fc\u51d1\u8d77\u6765\u3002\u6587\u6863\uff1a<\/p>\n<pre>\/\/ if f depends on testing flags, then init must be used to register\n\/\/ those flags before calling benchmark and before calling flag.parse.\n\/\/\n\/\/ if f calls run, the result will be an estimate of running all its\n\/\/ subbenchmarks that don't call run in sequence in a single benchmark.<\/pre>\n<p>\u8fd9\u8f6c\u5316\u4e3a\uff1a<\/p>\n<pre>func main() {\n  testing.init()\n  flag.parse()\n  br := testing.benchmark(benchmarkconnectionopen)\n  fmt.println(br.string() + br.memstring())\n}<\/pre>\n<p>\u7136\u540e\u8fd0\u884c\uff1a<\/p>\n<pre>go run main.go  -test.benchtime 100x<\/pre>\n<p>\u4ee5\u4e0a\u5c31\u662f\u300a\u76f4\u63a5\u4f7f\u7528testing.Benchmark()\u65f6\u8bbe\u7f6ebenchtime\u300b\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u5173\u4e8e\u7684\u8d44\u6599\u8bf7\u5173\u6ce8\u516c\u4f17\u53f7\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5f53\u524d\u4f4d\u7f6e\uff1a &gt; &gt; &#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-207147","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207147","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=207147"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207147\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=207147"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=207147"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=207147"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}