]> Cypherpunks repositories - gostls13.git/commitdiff
[release-branch.go1.24] testing: allow manual timer control in testing.B.Loop
authorJunyang Shao <shaojunyang@google.com>
Tue, 18 Mar 2025 21:13:23 +0000 (21:13 +0000)
committerGopher Robot <gobot@golang.org>
Tue, 25 Mar 2025 15:50:14 +0000 (08:50 -0700)
Fixes #72934

Change-Id: I56610d2d11d151a8f95b6434bbedbfcd5c11c317
Reviewed-on: https://go-review.googlesource.com/c/go/+/658975
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Commit-Queue: Junyang Shao <shaojunyang@google.com>
Reviewed-by: Austin Clements <austin@google.com>
Reviewed-on: https://go-review.googlesource.com/c/go/+/660555
Reviewed-by: Junyang Shao <shaojunyang@google.com>
Auto-Submit: Dmitri Shuralyov <dmitshur@google.com>
Reviewed-by: Cherry Mui <cherryyz@google.com>
src/testing/benchmark.go
src/testing/loop_test.go

index 3a7da9e54012b7fc559cf1836f3538b596d80dbf..166e3a0d16b5801047d7db361b48b298de66435e 100644 (file)
@@ -368,8 +368,8 @@ func (b *B) ReportMetric(n float64, unit string) {
 }
 
 func (b *B) stopOrScaleBLoop() bool {
-       timeElapsed := highPrecisionTimeSince(b.start)
-       if timeElapsed >= b.benchTime.d {
+       t := b.Elapsed()
+       if t >= b.benchTime.d {
                // Stop the timer so we don't count cleanup time
                b.StopTimer()
                return false
@@ -377,7 +377,7 @@ func (b *B) stopOrScaleBLoop() bool {
        // Loop scaling
        goalns := b.benchTime.d.Nanoseconds()
        prevIters := int64(b.N)
-       b.N = predictN(goalns, prevIters, timeElapsed.Nanoseconds(), prevIters)
+       b.N = predictN(goalns, prevIters, t.Nanoseconds(), prevIters)
        b.loopN++
        return true
 }
index 7a1a93fceee7ea3a51fa254725a4b6710cf04079..781a8566e8e4d8fee7531250d10b47fc6fdfcb29 100644 (file)
@@ -7,7 +7,7 @@ package testing
 func TestBenchmarkBLoop(t *T) {
        var initialStart highPrecisionTime
        var firstStart highPrecisionTime
-       var lastStart highPrecisionTime
+       var scaledStart highPrecisionTime
        var runningEnd bool
        runs := 0
        iters := 0
@@ -19,7 +19,9 @@ func TestBenchmarkBLoop(t *T) {
                        if iters == 0 {
                                firstStart = b.start
                        }
-                       lastStart = b.start
+                       if iters == 1 {
+                               scaledStart = b.start
+                       }
                        iters++
                }
                finalBN = b.N
@@ -45,8 +47,8 @@ func TestBenchmarkBLoop(t *T) {
        if firstStart == initialStart {
                t.Errorf("b.Loop did not reset the timer")
        }
-       if lastStart != firstStart {
-               t.Errorf("timer was reset during iteration")
+       if scaledStart != firstStart {
+               t.Errorf("b.Loop stops and restarts the timer during iteration")
        }
        // Verify that it stopped the timer after the last loop.
        if runningEnd {