aboutsummaryrefslogtreecommitdiff
path: root/src/testing/benchmark.go
diff options
context:
space:
mode:
authorhopehook <hopehook.com@gmail.com>2022-07-30 12:47:51 +0800
committerGopher Robot <gobot@golang.org>2022-08-25 17:58:32 +0000
commit396b153ec454cd427f97be4d994a903e2c4b244f (patch)
tree2c95f827e57b82b7620aed67c2f69238d1a488cf /src/testing/benchmark.go
parent83b5fe63514411f425061967fd8c1d506f2ac40f (diff)
downloadgo-396b153ec454cd427f97be4d994a903e2c4b244f.tar.xz
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>
Diffstat (limited to 'src/testing/benchmark.go')
-rw-r--r--src/testing/benchmark.go11
1 files changed, 11 insertions, 0 deletions
diff --git a/src/testing/benchmark.go b/src/testing/benchmark.go
index 4fee421d39..ce1ab6da37 100644
--- a/src/testing/benchmark.go
+++ b/src/testing/benchmark.go
@@ -337,6 +337,17 @@ func (b *B) launch() {
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.
// If the metric is per-iteration, the caller should divide by b.N,
// and by convention units should end in "/op".