Commit 212859f359 for golang

commit 212859f359a3818b1fb156877ea4c41635e6ba6e
Author: Fernando Zuher <fernando.zuher@gmail.com>
Date:   Fri Dec 4 16:46:54 2026 +0000

    testing: document the B/op and allocs/op benchmark columns

    The package documentation explains the ns/op column of a benchmark
    result, but not the B/op and allocs/op columns that -benchmem and
    B.ReportAllocs add. These two metrics are defined only by formulas in
    the documentation of BenchmarkResult.AllocedBytesPerOp and
    BenchmarkResult.AllocsPerOp.

    Add a benchmark example that reports allocations, together with its
    output, to the Benchmarks section right after the ns/op example, and
    explain both columns there.

    Fixes #42239

    Change-Id: Ia0e9b9cb0024ec81cc477c57e75981cd58fe4b31
    GitHub-Last-Rev: fadc974dd04c3d67d8415d0064485e73d0341111
    GitHub-Pull-Request: golang/go#83671
    Reviewed-on: https://go-review.googlesource.com/c/go/+/870021
    LUCI-TryBot-Result: golang-scoped@luci-project-accounts.iam.gserviceaccount.com <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
    Reviewed-by: David Chase <drchase@google.com>
    Reviewed-by: Alan Donovan <adonovan@google.com>
    Auto-Submit: Alan Donovan <adonovan@google.com>

diff --git a/src/testing/testing.go b/src/testing/testing.go
index df133f3c66..b94b2d8449 100644
--- a/src/testing/testing.go
+++ b/src/testing/testing.go
@@ -82,6 +82,25 @@
 //
 // means that the body of the loop ran 68453040 times at a speed of 17.8 ns per loop.
 //
+// To also report memory allocations, run the benchmark with the -benchmem flag
+// or call [B.ReportAllocs] inside it:
+//
+//	func BenchmarkSprintf(b *testing.B) {
+//	    b.ReportAllocs()
+//	    for b.Loop() {
+//	        fmt.Sprintf("%d", 42)
+//	    }
+//	}
+//
+// The output
+//
+//	BenchmarkSprintf-8   	54145555	        21.62 ns/op	       2 B/op	       1 allocs/op
+//
+// has two more columns. B/op is the average number of bytes allocated on the heap
+// per iteration, and allocs/op is the average number of heap allocations per
+// iteration. Both are rounded down to an integer. In this example, each iteration
+// allocated 2 bytes in 1 allocation, on average.
+//
 // 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: