aboutsummaryrefslogtreecommitdiff
path: root/src/testing/testing_test.go
diff options
context:
space:
mode:
authorJunyang Shao <shaojunyang@google.com>2024-11-14 07:34:51 +0800
committerGo LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>2024-11-20 23:19:48 +0000
commit558f5372fc524e69ade3ab3fe36b1913a4095398 (patch)
treeb95143bb0a6d8db5e4811626afb2b26575dbf8df /src/testing/testing_test.go
parent60299c251364c6f4a830721e4953230260d118a0 (diff)
downloadgo-558f5372fc524e69ade3ab3fe36b1913a4095398.tar.xz
cmd/compile,testing: implement one-time rampup logic for testing.B.Loop
testing.B.Loop now does its own loop scheduling without interaction with b.N. b.N will be updated to the actual iterations b.Loop controls when b.Loop returns false. This CL also added tests for fixed iteration count (benchtime=100x case). This CL also ensured that b.Loop() is inlined. For #61515 Change-Id: Ia15f4462f4830ef4ec51327520ff59910eb4bb58 Reviewed-on: https://go-review.googlesource.com/c/go/+/627755 Reviewed-by: Michael Pratt <mpratt@google.com> Commit-Queue: Junyang Shao <shaojunyang@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Cherry Mui <cherryyz@google.com>
Diffstat (limited to 'src/testing/testing_test.go')
-rw-r--r--src/testing/testing_test.go87
1 files changed, 84 insertions, 3 deletions
diff --git a/src/testing/testing_test.go b/src/testing/testing_test.go
index d62455baa8..4bf6378782 100644
--- a/src/testing/testing_test.go
+++ b/src/testing/testing_test.go
@@ -700,6 +700,20 @@ func TestBenchmarkRace(t *testing.T) {
}
}
+func TestBenchmarkRaceBLoop(t *testing.T) {
+ out := runTest(t, "BenchmarkBLoopRacy")
+ c := bytes.Count(out, []byte("race detected during execution of test"))
+
+ want := 0
+ // We should see one race detector report.
+ if race.Enabled {
+ want = 1
+ }
+ if c != want {
+ t.Errorf("got %d race reports; want %d", c, want)
+ }
+}
+
func BenchmarkRacy(b *testing.B) {
if os.Getenv("GO_WANT_HELPER_PROCESS") != "1" {
b.Skipf("skipping intentionally-racy benchmark")
@@ -709,15 +723,25 @@ func BenchmarkRacy(b *testing.B) {
}
}
+func BenchmarkBLoopRacy(b *testing.B) {
+ if os.Getenv("GO_WANT_HELPER_PROCESS") != "1" {
+ b.Skipf("skipping intentionally-racy benchmark")
+ }
+ for b.Loop() {
+ doRace()
+ }
+}
+
func TestBenchmarkSubRace(t *testing.T) {
out := runTest(t, "BenchmarkSubRacy")
c := bytes.Count(out, []byte("race detected during execution of test"))
want := 0
- // We should see two race detector reports:
- // one in the sub-bencmark, and one in the parent afterward.
+ // We should see 3 race detector reports:
+ // one in the sub-bencmark, one in the parent afterward,
+ // and one in b.Loop.
if race.Enabled {
- want = 2
+ want = 3
}
if c != want {
t.Errorf("got %d race reports; want %d", c, want)
@@ -743,6 +767,12 @@ func BenchmarkSubRacy(b *testing.B) {
}
})
+ b.Run("racy-bLoop", func(b *testing.B) {
+ for b.Loop() {
+ doRace()
+ }
+ })
+
doRace() // should be reported separately
}
@@ -943,3 +973,54 @@ func TestContext(t *testing.T) {
}
})
}
+
+func TestBenchmarkBLoopIterationCorrect(t *testing.T) {
+ out := runTest(t, "BenchmarkBLoopPrint")
+ c := bytes.Count(out, []byte("Printing from BenchmarkBLoopPrint"))
+
+ want := 2
+ if c != want {
+ t.Errorf("got %d loop iterations; want %d", c, want)
+ }
+
+ // b.Loop() will only rampup once.
+ c = bytes.Count(out, []byte("Ramping up from BenchmarkBLoopPrint"))
+ want = 1
+ if c != want {
+ t.Errorf("got %d loop rampup; want %d", c, want)
+ }
+}
+
+func TestBenchmarkBNIterationCorrect(t *testing.T) {
+ out := runTest(t, "BenchmarkBNPrint")
+ c := bytes.Count(out, []byte("Printing from BenchmarkBNPrint"))
+
+ // runTest sets benchtime=2x, with semantics specified in #32051 it should
+ // run 3 times.
+ want := 3
+ if c != want {
+ t.Errorf("got %d loop iterations; want %d", c, want)
+ }
+
+ // b.N style fixed iteration loop will rampup twice:
+ // One in run1(), the other in launch
+ c = bytes.Count(out, []byte("Ramping up from BenchmarkBNPrint"))
+ want = 2
+ if c != want {
+ t.Errorf("got %d loop rampup; want %d", c, want)
+ }
+}
+
+func BenchmarkBLoopPrint(b *testing.B) {
+ b.Logf("Ramping up from BenchmarkBLoopPrint")
+ for b.Loop() {
+ b.Logf("Printing from BenchmarkBLoopPrint")
+ }
+}
+
+func BenchmarkBNPrint(b *testing.B) {
+ b.Logf("Ramping up from BenchmarkBNPrint")
+ for i := 0; i < b.N; i++ {
+ b.Logf("Printing from BenchmarkBNPrint")
+ }
+}