Skip to content

Commit f8a0011

Browse files
committed
fixup: improve round time calc
1 parent fba63ab commit f8a0011

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

testing/testing/benchmark.go

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -373,10 +373,20 @@ func (b *B) launch() {
373373
// Final run:
374374
benchD := time.Second * b.benchTime.d
375375
benchN := predictN(benchD.Nanoseconds(), int64(b.N), b.duration.Nanoseconds(), warmupN)
376-
rounds := 100 // TODO: Compute the rounds in a better way
377-
// Ensure roundN is at least 1 to prevent division by zero in BenchmarkResult printing
378-
// when very slow benchmarks can't complete even 1 iteration per round
379-
roundN := max(benchN/int(rounds), 1)
376+
377+
// When we have a very slow benchmark (e.g. taking 500ms), we have to:
378+
// 1. Reduce the number of rounds to not slow down the process (e.g. by executing a 1s bench 100 times)
379+
// 2. Not end up with roundN of 0 when dividing benchN (which can be < 100) by rounds
380+
const minRounds = 100
381+
var rounds int
382+
var roundN int
383+
if benchN < minRounds {
384+
rounds = benchN
385+
roundN = 1
386+
} else {
387+
rounds = minRounds
388+
roundN = benchN / int(rounds)
389+
}
380390

381391
for range rounds {
382392
b.runN(int(roundN))

0 commit comments

Comments
 (0)