diff options
Diffstat (limited to 'src/testing/benchmark.go')
| -rw-r--r-- | src/testing/benchmark.go | 13 |
1 files changed, 13 insertions, 0 deletions
diff --git a/src/testing/benchmark.go b/src/testing/benchmark.go index 5591cd4e4d..0271308346 100644 --- a/src/testing/benchmark.go +++ b/src/testing/benchmark.go @@ -113,6 +113,8 @@ type B struct { netBytes uint64 // Extra metrics collected by ReportMetric. extra map[string]float64 + // Remaining iterations of Loop() to be executed in benchFunc. + loopN int } // StartTimer starts timing a test. This function is called automatically @@ -187,6 +189,7 @@ func (b *B) runN(n int) { runtime.GC() b.resetRaces() b.N = n + b.loopN = n b.parallelism = 1 b.ResetTimer() b.StartTimer() @@ -349,6 +352,16 @@ func (b *B) ReportMetric(n float64, unit string) { b.extra[unit] = n } +// Loop returns true until b.N calls has been made to it. +// +// A benchmark should either use Loop or contain an explicit loop from 0 to b.N, but not both. +// After the benchmark finishes, b.N will contain the total number of calls to op, so the benchmark +// may use b.N to compute other average metrics. +func (b *B) Loop() bool { + b.loopN-- + return b.loopN >= 0 +} + // BenchmarkResult contains the results of a benchmark run. type BenchmarkResult struct { N int // The number of iterations. |
