aboutsummaryrefslogtreecommitdiff
path: root/src/testing/loop_test.go
diff options
context:
space:
mode:
authorAustin Clements <austin@google.com>2025-03-19 11:46:41 -0400
committerGopher Robot <gobot@golang.org>2025-03-21 16:12:09 -0700
commit90031542068c9443e8cfe48149179c35b4c4afb7 (patch)
tree54b1d30d33ae4e823462ffcd42c51baa4fbd0ad9 /src/testing/loop_test.go
parentafe11db4a7d61d6ec196577f39b45648f987927d (diff)
downloadgo-90031542068c9443e8cfe48149179c35b4c4afb7.tar.xz
testing: separate b.Loop counter from b.N
Currently, b.Loop uses b.N as the iteration count target. However, since it updates the target as it goes, the behavior is quite different from a b.N-style benchmark. To avoid user confusion, this CL gives b.Loop a separate, unexported iteration count target. It ensures b.N is 0 within the b.Loop loop to help catch misuses, and commits the final iteration count to b.N only once the loop is done (as the documentation states "After Loop returns false, b.N contains the total number of iterations that ran, so the benchmark may use b.N to compute other average metrics.") Since there are now two variables used by b.Loop, we put them in an unnamed struct. Also, we rename b.loopN to b.loop.i because this variable tracks the current iteration index (conventionally "i"), not the target (conventionally "n"). Unfortunately, a simple renaming causes B.Loop to be too large for the inliner. Thus, we make one simplification to B.Loop to keep it under the threshold. We're about to lean into that simplification anyway in a follow-up CL, so this is just temporary. Prep for #72933 and #72971. Change-Id: Ide1c4f1b9ca37f300f3beb0e60ba6202331b183e Reviewed-on: https://go-review.googlesource.com/c/go/+/659655 LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Junyang Shao <shaojunyang@google.com> Auto-Submit: Austin Clements <austin@google.com>
Diffstat (limited to 'src/testing/loop_test.go')
-rw-r--r--src/testing/loop_test.go12
1 files changed, 12 insertions, 0 deletions
diff --git a/src/testing/loop_test.go b/src/testing/loop_test.go
index 781a8566e8..7a42919643 100644
--- a/src/testing/loop_test.go
+++ b/src/testing/loop_test.go
@@ -11,6 +11,8 @@ func TestBenchmarkBLoop(t *T) {
var runningEnd bool
runs := 0
iters := 0
+ firstBN := 0
+ restBN := 0
finalBN := 0
bRet := Benchmark(func(b *B) {
initialStart = b.start
@@ -18,6 +20,9 @@ func TestBenchmarkBLoop(t *T) {
for b.Loop() {
if iters == 0 {
firstStart = b.start
+ firstBN = b.N
+ } else {
+ restBN = max(restBN, b.N)
}
if iters == 1 {
scaledStart = b.start
@@ -39,6 +44,13 @@ func TestBenchmarkBLoop(t *T) {
if finalBN != iters || bRet.N != iters {
t.Errorf("benchmark iterations mismatch: %d loop iterations, final b.N=%d, bRet.N=%d", iters, finalBN, bRet.N)
}
+ // Verify that b.N was 0 inside the loop
+ if firstBN != 0 {
+ t.Errorf("want b.N == 0 on first iteration, got %d", firstBN)
+ }
+ if restBN != 0 {
+ t.Errorf("want b.N == 0 on subsequent iterations, got %d", restBN)
+ }
// Make sure the benchmark ran for an appropriate amount of time.
if bRet.T < benchTime.d {
t.Fatalf("benchmark ran for %s, want >= %s", bRet.T, benchTime.d)