fix: Address PR comments

- Clarify meaning of `b.N`.
- Nix verbose flag in run example.
This commit is contained in:
jidicula 2021-12-01 20:16:16 -05:00
parent 80a69a8974
commit 690eb8a1ac
No known key found for this signature in database
4 changed files with 31 additions and 44 deletions

View File

@ -67,10 +67,13 @@ 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.
// 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) {
// To measure a benchmark, the `testing` runner calls the
// function in a loop `b.N` times.
for i := 0; i < b.N; i++ {
IntMin(1, 2)
}

View File

@ -20,25 +20,10 @@ 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)
$ go test -bench=.
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 @@
c3489e12554d4b865ae225bc8bad2d9e634fe429
Ct1lKa6FttL
d3b047089cf762a8aa9778a8365a29eb31363288
HryvnW3zHYJ

45
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/Ct1lKa6FttL"><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/HryvnW3zHYJ"><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,15 +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 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>
<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>To measure a benchmark, the <code>testing</code> runner calls the
function 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>
<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>
@ -243,25 +257,10 @@ flag receives a regex for benchmark function names.</p>
<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 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
</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>
@ -283,7 +282,7 @@ flag receives a regex for benchmark function names.</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('');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>