{"id":7081,"date":"2024-07-19T15:01:01","date_gmt":"2024-07-19T07:01:01","guid":{"rendered":""},"modified":"2024-07-19T15:01:01","modified_gmt":"2024-07-19T07:01:01","slug":"benchmark\u5206\u6790\u6a21\u578b_\u6d4b\u8bc4\u7535\u8111\u6027\u80fd\u7684\u8f6f\u4ef6\u6709\u54ea\u4e9b","status":"publish","type":"post","link":"https:\/\/mushiming.com\/7081.html","title":{"rendered":"benchmark\u5206\u6790\u6a21\u578b_\u6d4b\u8bc4\u7535\u8111\u6027\u80fd\u7684\u8f6f\u4ef6\u6709\u54ea\u4e9b"},"content":{"rendered":"
\n

\u5728 Go \u4e2d\uff0c\u901a\u8fc7\u64b0\u5199 Benchmark \u51fd\u6570\u53ef\u4ee5\u5f88\u65b9\u4fbf\u5730\u5bf9\u67d0\u4e2a\u529f\u80fd\u70b9\u8fdb\u884c\u6027\u80fd\u68c0\u6d4b\u3002\u5bf9\u4e8e\u91cd\u8981\u7684\u51fd\u6570\uff0c\u6211\u4eec\u53ef\u4ee5\u5728 CI\/CD \u4e2d\u6dfb\u52a0\u76f8\u5e94\u7684\u6d4b\u8bd5\u6d41\u7a0b\uff0c\u5f53\u51fd\u6570\u6027\u80fd\u53d1\u751f\u53d8\u5316\u65f6\u80fd\u591f\u53ca\u65f6\u611f\u77e5\u3002\u90a3\u95ee\u9898\u6765\u4e86\uff0c\u5982\u4f55\u68c0\u6d4b\u51fd\u6570\u7684\u6027\u80fd\u53d8\u5316\uff1f<\/p>\n

\u6362\u4e2a\u8bf4\u6cd5\uff0c\u4f60\u7f16\u5199\u4e86\u67d0\u529f\u80fd\u51fd\u6570\u4f46\u53d1\u73b0\u5b83\u8fd0\u884c\u5f88\u6162\uff0c\u9700\u8981\u5bf9\u8be5\u51fd\u6570\u8fdb\u884c\u4f18\u5316\uff0c\u5f53\u4f60\u5728\u8c37\u6b4c\u641c\u7d22\u627e\u5230\u66f4\u597d\u7684\u5b9e\u73b0\u65b9\u5f0f\uff0c\u901a\u8fc7 Benchmark \u51fd\u6570\u53d1\u73b0\u5b83\u7684\u786e\u53d8\u5feb\u4e86\u3002\u4f46\u4f60\u8bf4\u4e0d\u6e05\u695a\u5177\u4f53\u53d8\u5feb\u4e86\u591a\u5c11\uff0c\u4f60\u60f3\u77e5\u9053\u51fd\u6570\u4f18\u5316\u524d\u540e\u7684\u6027\u80fd\u5bf9\u6bd4\uff0c\u63d0\u9ad8\u591a\u5c11\u767e\u5206\u70b9\uff0c\u53ef\u4fe1\u5ea6\u9ad8\u5417\uff1f<\/p>\n

\u9488\u5bf9\u4ee5\u4e0a\u7684\u9700\u6c42\u573a\u666f\uff0c\u6709\u4e00\u4e2a\u5de5\u5177\u53ef\u4ee5\u5e2e\u52a9\u5230\u4f60\uff0c\u5b83\u5c31\u662f benchstat\u3002<\/p>\n

Benchmark \u793a\u4f8b<\/h3>\n

\u6211\u4eec\u5148\u56de\u987e\u4e00\u4e0b\u57fa\u51c6\u6d4b\u8bd5\u3002\u4e3a\u4e86\u65b9\u4fbf\u7406\u89e3\uff0c\u8fd9\u91cc\u4ee5\u8ba1\u7b97\u7ecf\u5178\u7684\u8ba1\u7b97\u6590\u6ce2\u90a3\u5951\u6570\u5217\u503c\u4e3a\u4f8b\u3002<\/p>\n

func FibSolution(n int) int {  if n < 2 {   return n  }  return FibSolution(n-1) + FibSolution(n-2) }<\/code><\/pre>\n

\u4e0a\u8ff0\u4ee3\u7801\u662f\u9012\u5f52\u5f0f\u5b9e\u73b0\uff0c\u5f88\u660e\u663e\uff0c\u5f53 n \u8d8a\u6765\u8d8a\u5927\u65f6\uff0c\u8be5\u51fd\u6570\u7684\u8fd0\u884c\u4f1a\u53d8\u5f97\u975e\u5e38\u8017\u65f6\u3002\u4ee5 n \u4e3a 20 \u4e3a\u4f8b\uff0cBenchmark \u51fd\u6570\u5982\u4e0b<\/p>\n

func BenchmarkFib20(b *testing.B) {  for i := 0; i < b.N; i++ {   FibSolution(20)  } }<\/code><\/pre>\n

\u547d\u4ee4\u884c\u6267\u884cgo test -bench=BenchmarkFib20<\/code>\u5f97\u5230\u6027\u80fd\u7ed3\u679c<\/p>\n

BenchmarkFib20-8           39452             30229 ns\/op<\/code><\/pre>\n

\u5176\u4e2d\uff0c-8 \u4ee3\u8868\u7684\u662f 8 cpu\uff0c\u51fd\u6570\u8fd0\u884c\u6b21\u6570\u4e3a 39452\uff0c\u6bcf\u6b21\u51fd\u6570\u7684\u5e73\u5747\u82b1\u8d39\u65f6\u95f4\u4e3a 30229ns\u3002\u5982\u679c\u6211\u4eec\u60f3\u5f97\u5230\u591a\u6b21\u6837\u672c\u6570\u636e\uff0c\u53ef\u4ee5\u6307\u5b9a go test \u7684 -count=N<\/code> \u53c2\u6570\u3002\u4f8b\u5982\u60f3\u5f97\u5230 5 \u6b21\u6837\u672c\u6570\u636e\uff0c\u5219\u6267\u884cgo test -bench=BenchmarkFib20 -count=5<\/code><\/p>\n

BenchmarkFib20-8           39325             30297 ns\/op BenchmarkFib20-8           39216             30349 ns\/op BenchmarkFib20-8           39901             30251 ns\/op BenchmarkFib20-8           39336             30455 ns\/op BenchmarkFib20-8           39423             30894 ns\/op<\/code><\/pre>\n

\u8ba1\u7b97\u6590\u6ce2\u90a3\u5951\u6570\u5217\u503c\u7684\u8fed\u4ee3\u5f0f\u5b9e\u73b0\u5982\u4e0b<\/p>\n

func FibSolution(n int) int {  if n < 2 {   return n  }  p, q, r := 0, 0, 1  for i := 2; i <= n; i++ {   p = q   q = r   r = p + q  }  return r }<\/code><\/pre>\n

\u5bf9\u6bd4\u8fd9\u4e24\u79cd\u51fd\u6570\u7684\u6027\u80fd\u5dee\u5f02\uff0c\u6700\u6734\u7d20\u7684\u65b9\u5f0f\u5c31\u662f\u5206\u522b\u5bf9\u8fd9\u4e24\u4e2a\u51fd\u6570\u8fdb\u884c\u57fa\u51c6\u6d4b\u8bd5\uff0c\u7136\u540e\u901a\u8fc7\u624b\u5de5\u5206\u6790\u8fd9\u4e9b\u57fa\u51c6\u6d4b\u8bd5\u7ed3\u679c\uff0c\u4f46\u662f\u8fd9\u5e76\u4e0d\u76f4\u89c2\u3002<\/p>\n

benchstat<\/h3>\n

benchstat \u662f Go \u5b98\u65b9\u63a8\u8350\u7684\u4e00\u6b3e\u547d\u4ee4\u884c\u5de5\u5177\uff0c\u5b83\u7528\u4e8e\u8ba1\u7b97\u548c\u6bd4\u8f83\u57fa\u51c6\u6d4b\u8bd5\u7684\u76f8\u5173\u7edf\u8ba1\u6570\u636e\u3002<\/p>\n

\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u8fdb\u884c\u5b89\u88c5<\/p>\n

go install golang.org\/x\/perf\/cmd\/benchstat@latest<\/code><\/pre>\n

\u6267\u884c -h \u53c2\u6570\u53ef\u4ee5\u770b\u5230\u8be5\u5de5\u5177\u7684\u4f7f\u7528\u63cf\u8ff0<\/p>\n

~ $ benchstat -h\nusage: benchstat [options] old.txt [new.txt] [more.txt ...]\noptions:\n  -alpha \u03b1\n     consider change significant if p < \u03b1 (default 0.05)\n  -csv\n     print results in CSV form\n  -delta-test test\n     significance test to apply to delta: utest, ttest, or none (default \"utest\")\n  -geomean\n     print the geometric mean of each file\n  -html\n     print results as an HTML table\n  -norange\n     suppress range columns (CSV only)\n  -sort order\n     sort by order: [-]delta, [-]name, none (default \"none\")\n  -split labels\n     split benchmarks by labels (default \"pkg,goos,goarch\")<\/code><\/pre>\n

\u6211\u4eec\u60f3\u6bd4\u8f83 FibSolution(n) \u4ece 15 \u5230 20\uff0c\u4e24\u79cd\u5b9e\u73b0\u65b9\u5f0f\u7684\u6027\u80fd\u57fa\u51c6\u6d4b\u8bd5\u3002<\/p>\n

$ go test -bench=. -count=5 | tee old.txt $ go test -bench=. -count=5 | tee new.txt<\/code><\/pre>\n

\u6ce8\u610f\uff0c\u8fd9\u4e24\u6761\u547d\u4ee4\u6267\u884c\u65f6\uff0c\u5206\u522b\u5bf9\u5e94 FibSolution \u51fd\u6570\u91c7\u7528\u9012\u5f52\u5f0f\u548c\u8fed\u4ee3\u5f0f\u5b9e\u73b0\u903b\u8f91\u3002<\/p>\n

\u6b64\u65f6\uff0c\u6211\u4eec\u53ef\u4ee5\u5bf9\u8fd9\u4e24\u4e2a\u51fd\u6570\u5b9e\u73b0\u903b\u8f91\u8fdb\u884c\u6027\u80fd\u5bf9\u6bd4<\/p>\n

$ benchstat old.txt new.txt  name     old time\/op  new time\/op  delta Fib15-8  2.67\u00b5s \u00b1 2%  0.01\u00b5s \u00b1 5%  -99.81%  (p=0.008 n=5+5) Fib16-8  4.20\u00b5s \u00b1 1%  0.01\u00b5s \u00b1 2%  -99.87%  (p=0.008 n=5+5) Fib17-8  6.81\u00b5s \u00b1 0%  0.01\u00b5s \u00b1 2%  -99.92%  (p=0.008 n=5+5) Fib18-8  11.1\u00b5s \u00b1 1%   0.0\u00b5s \u00b1 1%  -99.95%  (p=0.008 n=5+5) Fib19-8  18.0\u00b5s \u00b1 2%   0.0\u00b5s \u00b1 4%  -99.97%  (p=0.008 n=5+5) Fib20-8  29.2\u00b5s \u00b1 1%   0.0\u00b5s \u00b1 3%  -99.98%  (p=0.008 n=5+5)<\/code><\/pre>\n

\u53ef\u4ee5\u770b\u5230\uff0c\u9012\u5f52\u5f0f\u5b9e\u73b0\u7684\u51fd\u6570\uff0c\u4ed6\u7684\u6267\u884c\u65f6\u95f4\u968f\u7740 n \u503c\u53d8\u5927\u589e\u52a0\u975e\u5e38\u660e\u663e\u3002\u8fed\u4ee3\u5f0f\u5b9e\u73b0\u65b9\u5f0f\uff0c\u76f8\u8f83\u4e8e\u9012\u5f52\u5f0f\uff0c\u5b83\u7684\u5e73\u5747\u65f6\u95f4\u5f00\u9500\u964d\u4f4e\u4e86 99 % \u4ee5\u4e0a\uff0c\u4f18\u5316\u6548\u679c\u975e\u5e38\u660e\u663e\u3002<\/p>\n

\u53e6\u5916\uff0cp=0.008 \u8868\u793a\u7ed3\u679c\u7684\u53ef\u4fe1\u7a0b\u5ea6\uff0cp \u503c\u8d8a\u5927\u8868\u660e\u53ef\u4fe1\u5ea6\u8d8a\u4f4e\u3002\u4e00\u822c\u4ee5 0.05 \u4f5c\u4e3a\u4e34\u754c\u503c\uff0c\u8d85\u8fc7\u8be5\u503c\uff0c\u5219\u7ed3\u679c\u4e0d\u53ef\u4fe1\u3002n=5+5 \u8868\u793a\u5206\u522b\u4f7f\u7528\u7684\u6709\u6548\u6837\u672c\u6570\u91cf\u3002<\/p>\n

\u603b\u7ed3<\/h3>\n

benchstat \u662f\u4e00\u4e2a\u57fa\u51c6\u6d4b\u8bd5\u7edf\u8ba1\u5de5\u5177\uff0c\u5f53\u6211\u4eec\u505a\u4e00\u4e9b\u4f18\u5316\u5de5\u4f5c\u65f6\uff0c\u53ef\u4ee5\u5229\u7528\u5b83\u51cf\u8f7b\u4eba\u5de5\u5206\u6790\u6570\u636e\u6210\u672c\u3002<\/p>\n

\u5982\u679c\u4f60\u7684\u9879\u76ee\u5728 CI\/CD \u6d41\u7a0b\u4e2d\u6709\u90e8\u7f72\u81ea\u52a8\u5316\u6d4b\u8bd5\uff0c\u90a3\u4e0d\u59a8\u5c06\u8be5\u5de5\u5177\u52a0\u5165\u8fdb\u6765\u3002\u5728\u5bf9\u51fd\u6570\u6709\u6539\u52a8\u4e14\u52a0\u5267\u4e86\u6027\u80fd\u635f\u8017\u65f6\uff0c\u5b83\u6216\u8bb8\u80fd\u5e2e\u52a9\u4f60\u63d0\u524d\u53d1\u73b0\u95ee\u9898\u3002<\/p>\n

\"benchmark\u5206\u6790\u6a21\u578b_\u6d4b\u8bc4\u7535\u8111\u6027\u80fd\u7684\u8f6f\u4ef6\u6709\u54ea\u4e9b<\/p>\n

\u673a\u5668\u94c3\u780d\u83dc\u5200<\/strong><\/p>\n

\u6b22\u8fce\u6dfb\u52a0\u5c0f\u83dc\u5200\u5fae\u4fe1<\/p>\n

\u52a0\u5165Golang\u5206\u4eab\u7fa4\u5b66\u4e60\u4ea4\u6d41\uff01<\/p>\n

\u611f\u8c22\u4f60\u7684\u70b9\u8d5e<\/strong>\u548c\u5728\u770b<\/strong>\u54e6~<\/p>\n

\"benchmark\u5206\u6790\u6a21\u578b_\u6d4b\u8bc4\u7535\u8111\u6027\u80fd\u7684\u8f6f\u4ef6\u6709\u54ea\u4e9b<\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"benchmark\u5206\u6790\u6a21\u578b_\u6d4b\u8bc4\u7535\u8111\u6027\u80fd\u7684\u8f6f\u4ef6\u6709\u54ea\u4e9b\u5728Go\u4e2d\uff0c\u901a\u8fc7\u64b0\u5199Benchmark\u51fd\u6570\u53ef\u4ee5\u5f88\u65b9\u4fbf\u5730\u5bf9\u67d0\u4e2a\u529f\u80fd\u70b9\u8fdb\u884c\u6027\u80fd\u68c0\u6d4b","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"_links":{"self":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/7081"}],"collection":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/comments?post=7081"}],"version-history":[{"count":0,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/7081\/revisions"}],"wp:attachment":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/media?parent=7081"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/categories?post=7081"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/tags?post=7081"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}