aboutsummaryrefslogtreecommitdiff
path: root/src/testing/testing.go
diff options
context:
space:
mode:
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