Skip to content

Commit 944050f

Browse files
committed
fix(testing): dont append to list before stopping testing timer
1 parent e674755 commit 944050f

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

testing/testing/benchmark.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -165,19 +165,21 @@ func (b *B) StartTimer() {
165165
// while performing steps that you don't want to measure.
166166
func (b *B) StopTimer() {
167167
if b.timerOn {
168-
// For b.N loops: This will be called in runN which sets b.N to the number of iterations.
169-
// For b.Loop() loops: loopSlowPath sets b.N to 0 to prevent b.N loops within b.Loop. However, since
170-
// we're starting/stopping the timer for each iteration in the b.Loop() loop, we can use 1 as
171-
// the number of iterations for this round.
172-
b.codspeedItersPerRound = append(b.codspeedItersPerRound, max(int64(b.N), 1))
173-
b.codspeedTimePerRoundNs = append(b.codspeedTimePerRoundNs, highPrecisionTimeSince(b.start))
174-
b.duration += highPrecisionTimeSince(b.start)
168+
timeSinceStart := highPrecisionTimeSince(b.start)
169+
b.duration += timeSinceStart
175170
// runtime.ReadMemStats(&memStats)
176171
// b.netAllocs += memStats.Mallocs - b.startAllocs
177172
// b.netBytes += memStats.TotalAlloc - b.startBytes
178173
b.timerOn = false
179174
// If we hit B.Loop with the timer stopped, fail.
180175
// b.loop.i |= loopPoisonTimer
176+
177+
// For b.N loops: This will be called in runN which sets b.N to the number of iterations.
178+
// For b.Loop() loops: loopSlowPath sets b.N to 0 to prevent b.N loops within b.Loop. However, since
179+
// we're starting/stopping the timer for each iteration in the b.Loop() loop, we can use 1 as
180+
// the number of iterations for this round.
181+
b.codspeedItersPerRound = append(b.codspeedItersPerRound, max(int64(b.N), 1))
182+
b.codspeedTimePerRoundNs = append(b.codspeedTimePerRoundNs, timeSinceStart)
181183
}
182184
}
183185

0 commit comments

Comments
 (0)