feat: Add benchmark example

Adds a simple benchmark example and its invocation.
This commit is contained in:
jidicula 2021-11-29 15:02:30 -05:00
parent 80fb5ebddf
commit 80a69a8974
No known key found for this signature in database
4 changed files with 75 additions and 7 deletions

View File

@ -67,6 +67,9 @@ func TestIntMinTableDriven(t *testing.T) {
}
}
// Benchmark tests typically go in `_test.go` files and begin with `Benchmark`.
// Each benchmark function is run several times, with `b.N` increasing on each
// run until the `testing` runner deems the benchmark to be accurate.
func BenchmarkIntMin(b *testing.B) {
for i := 0; i < b.N; i++ {
IntMin(1, 2)

View File

@ -16,3 +16,29 @@ $ go test -v
--- PASS: TestIntMinTableDriven/-1,0 (0.00s)
PASS
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 -v -bench=.
=== RUN TestIntMinBasic
--- PASS: TestIntMinBasic (0.00s)
=== RUN TestIntMinTableDriven
=== RUN TestIntMinTableDriven/0,1
=== RUN TestIntMinTableDriven/1,0
=== RUN TestIntMinTableDriven/2,-2
=== RUN TestIntMinTableDriven/0,-1
=== RUN TestIntMinTableDriven/-1,0
--- PASS: TestIntMinTableDriven (0.00s)
--- PASS: TestIntMinTableDriven/0,1 (0.00s)
--- PASS: TestIntMinTableDriven/1,0 (0.00s)
--- PASS: TestIntMinTableDriven/2,-2 (0.00s)
--- PASS: TestIntMinTableDriven/0,-1 (0.00s)
--- PASS: TestIntMinTableDriven/-1,0 (0.00s)
goos: darwin
goarch: arm64
pkg: examples/testing
BenchmarkIntMin
BenchmarkIntMin-8 1000000000 0.3136 ns/op
PASS
ok examples/testing 0.351s

View File

@ -1,2 +1,2 @@
25e8941d63b555a590e6d44a95ae0e41ecadadca
ALL2BVLkYEr
c3489e12554d4b865ae225bc8bad2d9e634fe429
Ct1lKa6FttL

47
public/testing generated
View File

@ -47,7 +47,7 @@ typically lives in the same package as the code it tests.</p>
</td>
<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/Ct1lKa6FttL"><img title="Run code" src="play.png" class="run" /></a><img title="Copy code" src="clipboard.png" class="copy" />
<pre class="chroma">
<span class="kn">package</span> <span class="nx">main</span>
</pre>
@ -184,11 +184,15 @@ when executing <code>go test -v</code>.</p>
<tr>
<td class="docs">
<p>Benchmark tests typically go in <code>_test.go</code> files and begin with <code>Benchmark</code>.
Each benchmark function is run several times, with <code>b.N</code> increasing on each
run until the <code>testing</code> runner deems the benchmark to be accurate.</p>
</td>
<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="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>
<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="p">}</span>
@ -206,7 +210,7 @@ when executing <code>go test -v</code>.</p>
<p>Run all tests in the current project in verbose mode.</p>
</td>
<td class="code">
<td class="code leading">
<pre class="chroma">
<span class="gp">$</span> go test -v
@ -229,6 +233,41 @@ when executing <code>go test -v</code>.</p>
</td>
</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 -v -bench=.
<span class="go">=== RUN TestIntMinBasic
</span><span class="go">--- PASS: TestIntMinBasic (0.00s)
</span><span class="go">=== RUN TestIntMinTableDriven
</span><span class="go">=== RUN TestIntMinTableDriven/0,1
</span><span class="go">=== RUN TestIntMinTableDriven/1,0
</span><span class="go">=== RUN TestIntMinTableDriven/2,-2
</span><span class="go">=== RUN TestIntMinTableDriven/0,-1
</span><span class="go">=== RUN TestIntMinTableDriven/-1,0
</span><span class="go">--- PASS: TestIntMinTableDriven (0.00s)
</span><span class="go"> --- PASS: TestIntMinTableDriven/0,1 (0.00s)
</span><span class="go"> --- PASS: TestIntMinTableDriven/1,0 (0.00s)
</span><span class="go"> --- PASS: TestIntMinTableDriven/2,-2 (0.00s)
</span><span class="go"> --- PASS: TestIntMinTableDriven/0,-1 (0.00s)
</span><span class="go"> --- PASS: TestIntMinTableDriven/-1,0 (0.00s)
</span><span class="go">goos: darwin
</span><span class="go">goarch: arm64
</span><span class="go">pkg: examples/testing
</span><span class="go">BenchmarkIntMin
</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>
@ -244,7 +283,7 @@ when executing <code>go test -v</code>.</p>
</div>
<script>
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 for i :\u003D 0; i \u003C b.N; i++ {\u000A IntMin(1, 2)\u000A }\u000A}\u000A');codeLines.push('');codeLines.push('');
</script>
<script src="site.js" async></script>
</body>