func (b *B) stopOrScaleBLoop() bool {
timeElapsed := highPrecisionTimeSince(b.start)
if timeElapsed >= b.benchTime.d {
+ // Stop the timer so we don't count cleanup time
+ b.StopTimer()
return false
}
// Loop scaling
b.loopN++
return true
}
+ b.StopTimer()
return false
}
// Handles fixed time case
//
// Loop resets the benchmark timer the first time it is called in a benchmark,
// so any setup performed prior to starting the benchmark loop does not count
-// toward the benchmark measurement.
+// toward the benchmark measurement. Likewise, when it returns false, it stops
+// the timer so cleanup code is not measured.
//
// The compiler never optimizes away calls to functions within the body of a
// "for b.Loop() { ... }" loop. This prevents surprises that can otherwise occur
var initialStart highPrecisionTime
var firstStart highPrecisionTime
var lastStart highPrecisionTime
+ var runningEnd bool
runs := 0
iters := 0
finalBN := 0
iters++
}
finalBN = b.N
+ runningEnd = b.timerOn
})
// Verify that a b.Loop benchmark is invoked just once.
if runs != 1 {
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.