mirror of
https://github.com/golang/go
synced 2024-11-26 11:38:01 -07:00
testing: add Elapsed method to testing.B
Elapsed returns the measured elapsed time of the benchmark, but does not change the running state of the timer. Fixes #43620. Change-Id: Idd9f64c4632518eec759d2ffccbf0050d84fcc03 Reviewed-on: https://go-review.googlesource.com/c/go/+/420254 Reviewed-by: Dmitri Shuralyov <dmitshur@golang.org> TryBot-Result: Gopher Robot <gobot@golang.org> Auto-Submit: Bryan Mills <bcmills@google.com> Reviewed-by: Dmitri Shuralyov <dmitshur@google.com> Run-TryBot: hopehook <hopehook@qq.com> Reviewed-by: Bryan Mills <bcmills@google.com>
This commit is contained in:
parent
83b5fe6351
commit
396b153ec4
1
api/next/43620.txt
Normal file
1
api/next/43620.txt
Normal file
@ -0,0 +1 @@
|
|||||||
|
pkg testing, method (*B) Elapsed() time.Duration #43620
|
@ -337,6 +337,17 @@ func (b *B) launch() {
|
|||||||
b.result = BenchmarkResult{b.N, b.duration, b.bytes, b.netAllocs, b.netBytes, b.extra}
|
b.result = BenchmarkResult{b.N, b.duration, b.bytes, b.netAllocs, b.netBytes, b.extra}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Elapsed returns the measured elapsed time of the benchmark.
|
||||||
|
// The duration reported by Elapsed matches the one measured by
|
||||||
|
// StartTimer, StopTimer, and ResetTimer.
|
||||||
|
func (b *B) Elapsed() time.Duration {
|
||||||
|
d := b.duration
|
||||||
|
if b.timerOn {
|
||||||
|
d += time.Since(b.start)
|
||||||
|
}
|
||||||
|
return d
|
||||||
|
}
|
||||||
|
|
||||||
// ReportMetric adds "n unit" to the reported benchmark results.
|
// ReportMetric adds "n unit" to the reported benchmark results.
|
||||||
// If the metric is per-iteration, the caller should divide by b.N,
|
// If the metric is per-iteration, the caller should divide by b.N,
|
||||||
// and by convention units should end in "/op".
|
// and by convention units should end in "/op".
|
||||||
|
@ -176,5 +176,8 @@ func ExampleB_ReportMetric() {
|
|||||||
// This metric is per-operation, so divide by b.N and
|
// This metric is per-operation, so divide by b.N and
|
||||||
// report it as a "/op" unit.
|
// report it as a "/op" unit.
|
||||||
b.ReportMetric(float64(compares)/float64(b.N), "compares/op")
|
b.ReportMetric(float64(compares)/float64(b.N), "compares/op")
|
||||||
|
// This metric is per-time, so divide by b.Elapsed and
|
||||||
|
// report it as a "/ns" unit.
|
||||||
|
b.ReportMetric(float64(compares)/float64(b.Elapsed().Nanoseconds()), "compares/ns")
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user