feat: Add benchmark example (#403)

* feat: Add benchmark example

Adds a simple benchmark example and its invocation.

* fix: Address PR comments

- Clarify meaning of `b.N`.
- Nix verbose flag in run example.

* fix: Reword b.N comment

* fix: Missing period
This commit is contained in:
Johanan Idicula 2021-12-02 10:04:59 -05:00 committed by GitHub
parent 80fb5ebddf
commit dadb6bdeae
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 61 additions and 6 deletions

View File

@ -67,7 +67,13 @@ func TestIntMinTableDriven(t *testing.T) {
} }
} }
// Benchmark tests typically go in `_test.go` files and are
// named beginning with `Benchmark`. The `testing` runner
// executes each benchmark function several times, increasing
// `b.N` on each run until it collects a precise measurement.
func BenchmarkIntMin(b *testing.B) { func BenchmarkIntMin(b *testing.B) {
// Typically the benchmark runs a function we're
// benchmarking in a loop `b.N` times.
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
IntMin(1, 2) IntMin(1, 2)
} }

View File

@ -16,3 +16,14 @@ $ go test -v
--- PASS: TestIntMinTableDriven/-1,0 (0.00s) --- PASS: TestIntMinTableDriven/-1,0 (0.00s)
PASS PASS
ok examples/testing 0.023s ok examples/testing 0.023s
# Run all benchmarks in the current project in verbose
# mode. All tests are run prior to benchmarks. The `bench`
# flag receives a regex for benchmark function names.
$ go test -bench=.
goos: darwin
goarch: arm64
pkg: examples/testing
BenchmarkIntMin-8 1000000000 0.3136 ns/op
PASS
ok examples/testing 0.351s

View File

@ -1,2 +1,2 @@
25e8941d63b555a590e6d44a95ae0e41ecadadca 3671aaf0eee9f6d2b68e51b09997be767edfe97c
ALL2BVLkYEr PlzU16wwEWE

46
public/testing generated
View File

@ -47,7 +47,7 @@ typically lives in the same package as the code it tests.</p>
</td> </td>
<td class="code leading"> <td class="code leading">
<a href="http://play.golang.org/p/ALL2BVLkYEr"><img title="Run code" src="play.png" class="run" /></a><img title="Copy code" src="clipboard.png" class="copy" /> <a href="http://play.golang.org/p/PlzU16wwEWE"><img title="Run code" src="play.png" class="run" /></a><img title="Copy code" src="clipboard.png" class="copy" />
<pre class="chroma"> <pre class="chroma">
<span class="kn">package</span> <span class="nx">main</span> <span class="kn">package</span> <span class="nx">main</span>
</pre> </pre>
@ -184,11 +184,29 @@ when executing <code>go test -v</code>.</p>
<tr> <tr>
<td class="docs"> <td class="docs">
<p>Benchmark tests typically go in <code>_test.go</code> files and are
named beginning with <code>Benchmark</code>. The <code>testing</code> runner
executes each benchmark function several times, increasing
<code>b.N</code> on each run until it collects a precise measurement.</p>
</td>
<td class="code leading">
<pre class="chroma">
<span class="kd">func</span> <span class="nf">BenchmarkIntMin</span><span class="p">(</span><span class="nx">b</span> <span class="o">*</span><span class="nx">testing</span><span class="p">.</span><span class="nx">B</span><span class="p">)</span> <span class="p">{</span>
</pre>
</td>
</tr>
<tr>
<td class="docs">
<p>Typically the benchmark runs a function we&rsquo;re
benchmarking in a loop <code>b.N</code> times.</p>
</td> </td>
<td class="code"> <td class="code">
<pre class="chroma"><span class="kd">func</span> <span class="nf">BenchmarkIntMin</span><span class="p">(</span><span class="nx">b</span> <span class="o">*</span><span class="nx">testing</span><span class="p">.</span><span class="nx">B</span><span class="p">)</span> <span class="p">{</span> <pre class="chroma">
<span class="k">for</span> <span class="nx">i</span> <span class="o">:=</span> <span class="mi">0</span><span class="p">;</span> <span class="nx">i</span> <span class="p">&lt;</span> <span class="nx">b</span><span class="p">.</span><span class="nx">N</span><span class="p">;</span> <span class="nx">i</span><span class="o">++</span> <span class="p">{</span> <span class="k">for</span> <span class="nx">i</span> <span class="o">:=</span> <span class="mi">0</span><span class="p">;</span> <span class="nx">i</span> <span class="p">&lt;</span> <span class="nx">b</span><span class="p">.</span><span class="nx">N</span><span class="p">;</span> <span class="nx">i</span><span class="o">++</span> <span class="p">{</span>
<span class="nf">IntMin</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">)</span> <span class="nf">IntMin</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">)</span>
<span class="p">}</span> <span class="p">}</span>
@ -206,7 +224,7 @@ when executing <code>go test -v</code>.</p>
<p>Run all tests in the current project in verbose mode.</p> <p>Run all tests in the current project in verbose mode.</p>
</td> </td>
<td class="code"> <td class="code leading">
<pre class="chroma"> <pre class="chroma">
<span class="gp">$</span> go test -v <span class="gp">$</span> go test -v
@ -229,6 +247,26 @@ when executing <code>go test -v</code>.</p>
</td> </td>
</tr> </tr>
<tr>
<td class="docs">
<p>Run all benchmarks in the current project in verbose
mode. All tests are run prior to benchmarks. The <code>bench</code>
flag receives a regex for benchmark function names.</p>
</td>
<td class="code">
<pre class="chroma">
<span class="gp">$</span> go test -bench=.
<span class="go">goos: darwin
</span><span class="go">goarch: arm64
</span><span class="go">pkg: examples/testing
</span><span class="go">BenchmarkIntMin-8 1000000000 0.3136 ns/op
</span><span class="go">PASS
</span><span class="go">ok examples/testing 0.351s</span></pre>
</td>
</tr>
</table> </table>
@ -244,7 +282,7 @@ when executing <code>go test -v</code>.</p>
</div> </div>
<script> <script>
var codeLines = []; var codeLines = [];
codeLines.push('');codeLines.push('package main\u000A');codeLines.push('import (\u000A \"fmt\"\u000A \"testing\"\u000A)\u000A');codeLines.push('func IntMin(a, b int) int {\u000A if a \u003C b {\u000A return a\u000A }\u000A return b\u000A}\u000A');codeLines.push('func TestIntMinBasic(t *testing.T) {\u000A ans :\u003D IntMin(2, -2)\u000A if ans !\u003D -2 {\u000A');codeLines.push(' t.Errorf(\"IntMin(2, -2) \u003D %d; want -2\", ans)\u000A }\u000A}\u000A');codeLines.push('func TestIntMinTableDriven(t *testing.T) {\u000A var tests \u003D []struct {\u000A a, b int\u000A want int\u000A }{\u000A {0, 1, 0},\u000A {1, 0, 0},\u000A {2, -2, -2},\u000A {0, -1, -1},\u000A {-1, 0, -1},\u000A }\u000A');codeLines.push(' for _, tt :\u003D range tests {\u000A');codeLines.push(' testname :\u003D fmt.Sprintf(\"%d,%d\", tt.a, tt.b)\u000A t.Run(testname, func(t *testing.T) {\u000A ans :\u003D IntMin(tt.a, tt.b)\u000A if ans !\u003D tt.want {\u000A t.Errorf(\"got %d, want %d\", ans, tt.want)\u000A }\u000A })\u000A }\u000A}\u000A');codeLines.push('func BenchmarkIntMin(b *testing.B) {\u000A for i :\u003D 0; i \u003C b.N; i++ {\u000A IntMin(1, 2)\u000A }\u000A}\u000A');codeLines.push(''); codeLines.push('');codeLines.push('package main\u000A');codeLines.push('import (\u000A \"fmt\"\u000A \"testing\"\u000A)\u000A');codeLines.push('func IntMin(a, b int) int {\u000A if a \u003C b {\u000A return a\u000A }\u000A return b\u000A}\u000A');codeLines.push('func TestIntMinBasic(t *testing.T) {\u000A ans :\u003D IntMin(2, -2)\u000A if ans !\u003D -2 {\u000A');codeLines.push(' t.Errorf(\"IntMin(2, -2) \u003D %d; want -2\", ans)\u000A }\u000A}\u000A');codeLines.push('func TestIntMinTableDriven(t *testing.T) {\u000A var tests \u003D []struct {\u000A a, b int\u000A want int\u000A }{\u000A {0, 1, 0},\u000A {1, 0, 0},\u000A {2, -2, -2},\u000A {0, -1, -1},\u000A {-1, 0, -1},\u000A }\u000A');codeLines.push(' for _, tt :\u003D range tests {\u000A');codeLines.push(' testname :\u003D fmt.Sprintf(\"%d,%d\", tt.a, tt.b)\u000A t.Run(testname, func(t *testing.T) {\u000A ans :\u003D IntMin(tt.a, tt.b)\u000A if ans !\u003D tt.want {\u000A t.Errorf(\"got %d, want %d\", ans, tt.want)\u000A }\u000A })\u000A }\u000A}\u000A');codeLines.push('func BenchmarkIntMin(b *testing.B) {\u000A');codeLines.push(' for i :\u003D 0; i \u003C b.N; i++ {\u000A IntMin(1, 2)\u000A }\u000A}\u000A');codeLines.push('');codeLines.push('');
</script> </script>
<script src="site.js" async></script> <script src="site.js" async></script>
</body> </body>