testing: don't measure cleanup time after B.Loop

B.Loop resets the timer on the first iteration so that setup code
isn't measured, but it currently leaves the timer running after the
last iteration, meaning that cleanup code will still be measured. Fix
this by stopping the timer when B.Loop returns false to indicate the
end of the benchmark.

Updates #61515

Change-Id: I0e0502cb2ce3c24cf872682b88d74e8be2c4529b
Reviewed-on: https://go-review.googlesource.com/c/go/+/635898
Reviewed-by: Junyang Shao <shaojunyang@google.com>
Auto-Submit: Austin Clements <austin@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Cherry Mui <cherryyz@google.com>
This commit is contained in:
Austin Clements 2024-12-12 21:18:44 -05:00 committed by Gopher Robot
parent c1f2542c8b
commit 18b5435fc8
2 changed files with 11 additions and 1 deletions

View file

@ -8,6 +8,7 @@ func TestBenchmarkBLoop(t *T) {
var initialStart highPrecisionTime
var firstStart highPrecisionTime
var lastStart highPrecisionTime
var runningEnd bool
runs := 0
iters := 0
finalBN := 0
@ -22,6 +23,7 @@ func TestBenchmarkBLoop(t *T) {
iters++
}
finalBN = b.N
runningEnd = b.timerOn
})
// Verify that a b.Loop benchmark is invoked just once.
if runs != 1 {
@ -46,6 +48,10 @@ func TestBenchmarkBLoop(t *T) {
if lastStart != firstStart {
t.Errorf("timer was reset during iteration")
}
// Verify that it stopped the timer after the last loop.
if runningEnd {
t.Errorf("timer was still running after last iteration")
}
}
// See also TestBenchmarkBLoop* in other files.