Skip to content

Commit b613d21

Browse files
JunyangShaoGo LUCI
authored and
Go LUCI
committed
testing: allow manual timer control in testing.B.Loop
Fixes #72922 Change-Id: I56610d2d11d151a8f95b6434bbedbfcd5c11c317 Reviewed-on: https://go-review.googlesource.com/c/go/+/658975 LUCI-TryBot-Result: Go LUCI <[email protected]> Commit-Queue: Junyang Shao <[email protected]> Reviewed-by: Austin Clements <[email protected]>
1 parent ba50de8 commit b613d21

File tree

2 files changed

+9
-7
lines changed

2 files changed

+9
-7
lines changed

src/testing/benchmark.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -368,16 +368,16 @@ func (b *B) ReportMetric(n float64, unit string) {
368368
}
369369

370370
func (b *B) stopOrScaleBLoop() bool {
371-
timeElapsed := highPrecisionTimeSince(b.start)
372-
if timeElapsed >= b.benchTime.d {
371+
t := b.Elapsed()
372+
if t >= b.benchTime.d {
373373
// Stop the timer so we don't count cleanup time
374374
b.StopTimer()
375375
return false
376376
}
377377
// Loop scaling
378378
goalns := b.benchTime.d.Nanoseconds()
379379
prevIters := int64(b.N)
380-
b.N = predictN(goalns, prevIters, timeElapsed.Nanoseconds(), prevIters)
380+
b.N = predictN(goalns, prevIters, t.Nanoseconds(), prevIters)
381381
b.loopN++
382382
return true
383383
}

src/testing/loop_test.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ package testing
77
func TestBenchmarkBLoop(t *T) {
88
var initialStart highPrecisionTime
99
var firstStart highPrecisionTime
10-
var lastStart highPrecisionTime
10+
var scaledStart highPrecisionTime
1111
var runningEnd bool
1212
runs := 0
1313
iters := 0
@@ -19,7 +19,9 @@ func TestBenchmarkBLoop(t *T) {
1919
if iters == 0 {
2020
firstStart = b.start
2121
}
22-
lastStart = b.start
22+
if iters == 1 {
23+
scaledStart = b.start
24+
}
2325
iters++
2426
}
2527
finalBN = b.N
@@ -45,8 +47,8 @@ func TestBenchmarkBLoop(t *T) {
4547
if firstStart == initialStart {
4648
t.Errorf("b.Loop did not reset the timer")
4749
}
48-
if lastStart != firstStart {
49-
t.Errorf("timer was reset during iteration")
50+
if scaledStart != firstStart {
51+
t.Errorf("b.Loop stops and restarts the timer during iteration")
5052
}
5153
// Verify that it stopped the timer after the last loop.
5254
if runningEnd {

0 commit comments

Comments
 (0)