aboutsummaryrefslogtreecommitdiff
path: root/src/testing/testing.go
diff options
context:
space:
mode:
authorAustin Clements <austin@google.com>2024-12-12 15:43:37 -0500
committerGopher Robot <gobot@golang.org>2024-12-15 21:41:19 -0800
commit090748d6c7973e9bb8f5efe069135c8ea0f0d89c (patch)
tree00936118503d506c20c88f0167451517e8297e8f /src/testing/testing.go
parente39e965e0e0cce65ca977fd0da35f5bfb68dc2b8 (diff)
downloadgo-090748d6c7973e9bb8f5efe069135c8ea0f0d89c.tar.xz
testing: improve B.Loop docs, use B.Loop in examples
This updates the testing documentation to frame B.Loop as the canonical way to write benchmarks. We retain documentation on b.N benchmarks because people will definitely continue to see them (and write them), but it's demoted to clearly second class. This also attempts to clarify and refine the B.Loop documentation itself. Updates #61515 Fixes #70787 Change-Id: If5123435bfe3a5883a753119ecdf7bbc41afd499 Reviewed-on: https://go-review.googlesource.com/c/go/+/635895 Reviewed-by: Junyang Shao <shaojunyang@google.com> Reviewed-by: Caleb Spare <cespare@gmail.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Auto-Submit: Austin Clements <austin@google.com>
Diffstat (limited to 'src/testing/testing.go')
-rw-r--r--src/testing/testing.go46
1 files changed, 37 insertions, 9 deletions
diff --git a/src/testing/testing.go b/src/testing/testing.go
index e353ceb741..8b4bdfbc39 100644
--- a/src/testing/testing.go
+++ b/src/testing/testing.go
@@ -72,27 +72,24 @@
// A sample benchmark function looks like this:
//
// func BenchmarkRandInt(b *testing.B) {
-// for range b.N {
+// for b.Loop() {
// rand.Int()
// }
// }
//
-// The benchmark function must run the target code b.N times.
-// It is called multiple times with b.N adjusted until the
-// benchmark function lasts long enough to be timed reliably.
// The output
//
// BenchmarkRandInt-8 68453040 17.8 ns/op
//
-// means that the loop ran 68453040 times at a speed of 17.8 ns per loop.
+// means that the body of the loop ran 68453040 times at a speed of 17.8 ns per loop.
//
-// If a benchmark needs some expensive setup before running, the timer
-// may be reset:
+// Only the body of the loop is timed, so benchmarks may do expensive
+// setup before calling b.Loop, which will not be counted toward the
+// benchmark measurement:
//
// func BenchmarkBigLen(b *testing.B) {
// big := NewBig()
-// b.ResetTimer()
-// for range b.N {
+// for b.Loop() {
// big.Len()
// }
// }
@@ -120,6 +117,37 @@
// In particular, https://golang.org/x/perf/cmd/benchstat performs
// statistically robust A/B comparisons.
//
+// # b.N-style benchmarks
+//
+// Prior to the introduction of [B.Loop], benchmarks were written in a
+// different style using [B.N]. For example:
+//
+// func BenchmarkRandInt(b *testing.B) {
+// for range b.N {
+// rand.Int()
+// }
+// }
+//
+// In this style of benchmark, the benchmark function must run
+// the target code b.N times. The benchmark function is called
+// multiple times with b.N adjusted until the benchmark function
+// lasts long enough to be timed reliably. This also means any setup
+// done before the loop may be run several times.
+//
+// If a benchmark needs some expensive setup before running, the timer
+// should be explicitly reset:
+//
+// func BenchmarkBigLen(b *testing.B) {
+// big := NewBig()
+// b.ResetTimer()
+// for range b.N {
+// big.Len()
+// }
+// }
+//
+// New benchmarks should prefer using [B.Loop], which is more robust
+// and more efficient.
+//
// # Examples
//
// The package also runs and verifies example code. Example functions may