From 6600a871ef7376b151cb7e4073c4095209f112ca Mon Sep 17 00:00:00 2001 From: sunnymilk Date: Tue, 27 Aug 2024 17:23:10 -0400 Subject: testing: implement testing.B.Loop Initial implementation for testing.B.Loop, right now the calculation of b.N are still done in the old fasion way, as of now b.Loop is merely an alias for the old loop over b.N. For #61515. Change-Id: If211d0acc5f0c33df530096dceafe0b947ab0c8e Reviewed-on: https://go-review.googlesource.com/c/go/+/608798 TryBot-Result: Gopher Robot Reviewed-by: Junyang Shao Run-TryBot: Junyang Shao LUCI-TryBot-Result: Go LUCI Reviewed-by: Michael Pratt --- src/testing/benchmark_test.go | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) (limited to 'src/testing/benchmark_test.go') diff --git a/src/testing/benchmark_test.go b/src/testing/benchmark_test.go index 66f555d1f1..b5ad213fb3 100644 --- a/src/testing/benchmark_test.go +++ b/src/testing/benchmark_test.go @@ -127,6 +127,22 @@ func TestRunParallelSkipNow(t *testing.T) { }) } +func TestLoopEqualsRangeOverBN(t *testing.T) { + // Verify that b.N and the b.Loop() iteration count match. + var nIterated, nInfered int + testing.Benchmark(func(b *testing.B) { + i := 0 + for b.Loop() { + i++ + } + nIterated = i + nInfered = b.N + }) + if nIterated != nInfered { + t.Fatalf("Iteration of the two different benchmark loop flavor differs, got %d iterations want %d", nIterated, nInfered) + } +} + func ExampleB_RunParallel() { // Parallel benchmark for text/template.Template.Execute on a single object. testing.Benchmark(func(b *testing.B) { -- cgit v1.3