Radar · Go · Archive · Week 14 · Mar 31 – Apr 6, 2025
More predictable benchmarking with testing.B.Loop
Nice to knowstdlib
- What changed
- The
testing.B.Loophelper provides a more deterministic benchmark loop in Go 1.24. - Production impact
- The source does not say.
- Try it
- Create a benchmark that uses
b.Loop()instead of aforloop. - Source
- go.dev/blog/testing-b-loop
Understand it, then run it
Run it now
// This program demonstrates that the `testing.B.Loop` helper is available
// in Go 1.27.1. It does not run a benchmark; it simply compiles a
// benchmark function that uses `b.Loop()` and prints a confirmation.
package main
import (
"fmt"
"testing"
)
// BenchmarkExample shows the new `b.Loop()` syntax. It is not executed
// here, but the presence of this function proves that the API exists.
func BenchmarkExample(b *testing.B) {
for b.Loop() {
// The body would contain the code to benchmark.
}
}
func main() {
fmt.Println("testing.B.Loop is available in Go 1.27.1")
}
What it printed when we ran it on Go 1.27.1
testing.B.Loop is available in Go 1.27.1
Run sends this program (for Solidity, the contract and its tests) to our own sandbox, where it is compiled and run once, with no network, and what it printed or the test report comes back here. Nothing is kept. Runs are counted per visitor for the day so everyone gets a turn; the details are on the legal page.
Written by gpt-oss-20b from the linked source · claims checked against the sources · archive, not individually reviewed