Skip to content

Commit c1f2542

Browse files
aclementsgopherbot
authored andcommitted
testing: improve B.Loop test
This moves the B.Loop test from package testing_test to package testing, where it can check on more of the internals of the benchmark state. Updates #61515. Change-Id: Ia32d7104526125c5e8a1e35dab7660008afcbf80 Reviewed-on: https://go-review.googlesource.com/c/go/+/635897 Auto-Submit: Austin Clements <austin@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Junyang Shao <shaojunyang@google.com>
1 parent 6bd56fc commit c1f2542

File tree

2 files changed

+51
-22
lines changed

2 files changed

+51
-22
lines changed

Diff for: src/testing/benchmark_test.go

-22
Original file line numberDiff line numberDiff line change
@@ -127,28 +127,6 @@ func TestRunParallelSkipNow(t *testing.T) {
127127
})
128128
}
129129

130-
func TestBLoopHasResults(t *testing.T) {
131-
// Verify that b.N and the b.Loop() iteration count match.
132-
var nIterated int
133-
bRet := testing.Benchmark(func(b *testing.B) {
134-
i := 0
135-
for b.Loop() {
136-
i++
137-
}
138-
nIterated = i
139-
})
140-
if nIterated == 0 {
141-
t.Fatalf("Iteration count zero")
142-
}
143-
if bRet.N != nIterated {
144-
t.Fatalf("Benchmark result N incorrect, got %d want %d", bRet.N, nIterated)
145-
}
146-
// We only need to check duration to make sure benchmark result is written.
147-
if bRet.T == 0 {
148-
t.Fatalf("Benchmark result duration unset")
149-
}
150-
}
151-
152130
func ExampleB_RunParallel() {
153131
// Parallel benchmark for text/template.Template.Execute on a single object.
154132
testing.Benchmark(func(b *testing.B) {

Diff for: src/testing/loop_test.go

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// Copyright 2024 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package testing
6+
7+
func TestBenchmarkBLoop(t *T) {
8+
var initialStart highPrecisionTime
9+
var firstStart highPrecisionTime
10+
var lastStart highPrecisionTime
11+
runs := 0
12+
iters := 0
13+
finalBN := 0
14+
bRet := Benchmark(func(b *B) {
15+
initialStart = b.start
16+
runs++
17+
for b.Loop() {
18+
if iters == 0 {
19+
firstStart = b.start
20+
}
21+
lastStart = b.start
22+
iters++
23+
}
24+
finalBN = b.N
25+
})
26+
// Verify that a b.Loop benchmark is invoked just once.
27+
if runs != 1 {
28+
t.Errorf("want runs == 1, got %d", runs)
29+
}
30+
// Verify that at least one iteration ran.
31+
if iters == 0 {
32+
t.Fatalf("no iterations ran")
33+
}
34+
// Verify that b.N, bRet.N, and the b.Loop() iteration count match.
35+
if finalBN != iters || bRet.N != iters {
36+
t.Errorf("benchmark iterations mismatch: %d loop iterations, final b.N=%d, bRet.N=%d", iters, finalBN, bRet.N)
37+
}
38+
// Make sure the benchmark ran for an appropriate amount of time.
39+
if bRet.T < benchTime.d {
40+
t.Fatalf("benchmark ran for %s, want >= %s", bRet.T, benchTime.d)
41+
}
42+
// Verify that the timer is reset on the first loop, and then left alone.
43+
if firstStart == initialStart {
44+
t.Errorf("b.Loop did not reset the timer")
45+
}
46+
if lastStart != firstStart {
47+
t.Errorf("timer was reset during iteration")
48+
}
49+
}
50+
51+
// See also TestBenchmarkBLoop* in other files.

0 commit comments

Comments
 (0)