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:
parent
80fb5ebddf
commit
dadb6bdeae
@ -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) {
|
||||
// Typically the benchmark runs a function we're
|
||||
// benchmarking in a loop `b.N` times.
|
||||
for i := 0; i < b.N; i++ {
|
||||
IntMin(1, 2)
|
||||
}
|
||||
|
@ -16,3 +16,14 @@ $ 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 -bench=.
|
||||
goos: darwin
|
||||
goarch: arm64
|
||||
pkg: examples/testing
|
||||
BenchmarkIntMin-8 1000000000 0.3136 ns/op
|
||||
PASS
|
||||
ok examples/testing 0.351s
|
||||
|
@ -1,2 +1,2 @@
|
||||
25e8941d63b555a590e6d44a95ae0e41ecadadca
|
||||
ALL2BVLkYEr
|
||||
3671aaf0eee9f6d2b68e51b09997be767edfe97c
|
||||
PlzU16wwEWE
|
||||
|
46
public/testing
generated
46
public/testing
generated
@ -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/PlzU16wwEWE"><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,29 @@ when executing <code>go test -v</code>.</p>
|
||||
|
||||
<tr>
|
||||
<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’re
|
||||
benchmarking in a loop <code>b.N</code> times.</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="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"><</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 +224,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 +247,26 @@ 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 -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>
|
||||
|
||||
|
||||
@ -244,7 +282,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');codeLines.push(' 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>
|
||||
|
Loading…
x
Reference in New Issue
Block a user