{"id":5059,"date":"2017-06-03T20:15:31","date_gmt":"2017-06-03T20:15:31","guid":{"rendered":"http:\/\/putridparrot.com\/blog\/?p=5059"},"modified":"2017-06-03T20:15:31","modified_gmt":"2017-06-03T20:15:31","slug":"benchmarking-code-in-go","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/benchmarking-code-in-go\/","title":{"rendered":"Benchmarking code in Go"},"content":{"rendered":"<p>To create a unit test in Go<, we simple create a function with Test as the first part of the name, see my previous post on <a href=\"http:\/\/putridparrot.com\/blog\/unit-tests-in-go\/\" target=\"_blank\">Unit testing in Go<\/a>.<\/p>\n<p>We can also write performance\/benchmarking test code by creating a function with Benchmark as the first part of the function name, i.e. BenchmarkDataRead and it takes on the same format as a unit test, for example<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\nfunc BenchmarkEcho(b *testing.B) {\r\n\texpected := &quot;Hello&quot;\r\n\tactual := test.Echo(&quot;Hello&quot;)\r\n\r\n\tif actual == expected {\r\n\t\tb.Error(&quot;Test failed&quot;)\r\n\t}\r\n}\r\n<\/pre>\n<p>Our benchmark is passed a testing.B type which gives us functionality as per testing.T, in that we can fail a benchmark test etc. Obviously, for a benchmarking type, we also have the ability to start and stop the timer, for example if we have initialization and cleanup code we might want to exclude these from the benchmark by just wrapping the key code in a b.StartTimer and b.StopTimer section, i.e. <\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\nfunc BenchmarkEcho(b *testing.B) {\r\n\texpected := &quot;Hello&quot;\r\n\r\n\tb.StartTimer()\r\n\tactual := test.Echo(&quot;Hello&quot;)\r\n\tb.StopTimer()\r\n\r\n\tif actual != expected {\r\n\t\tb.Error(&quot;Test failed&quot;)\r\n\t}\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>To create a unit test in Go<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[178],"tags":[],"class_list":["post-5059","post","type-post","status-publish","format-standard","hentry","category-go"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/5059","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/comments?post=5059"}],"version-history":[{"count":3,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/5059\/revisions"}],"predecessor-version":[{"id":5077,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/5059\/revisions\/5077"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=5059"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=5059"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=5059"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}