Skip to content

Commit 1347a6e

Browse files
committed
test: make GR-8 overhead gate robust (best-of-N; skip under -race)
1 parent 76ce9e3 commit 1347a6e

3 files changed

Lines changed: 50 additions & 8 deletions

File tree

bench_test.go

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -57,30 +57,56 @@ func BenchmarkBarePayloadJSON(b *testing.B) {
5757
// already pays), measured against a conservative broker round-trip. It is pure CPU
5858
// — no broker — so it is stable and environment-independent in CI.
5959
func TestCodecOverheadWithinBudget(t *testing.T) {
60+
// GR-8 is a pure-CPU timing gate. The race detector instruments every memory
61+
// access and inflates the codec disproportionately (it touches more memory than
62+
// bare JSON), so the measurement is meaningless under -race. CI runs this suite
63+
// with -race; the non-race coverage job enforces this gate instead.
64+
if raceEnabled {
65+
t.Skip("GR-8 overhead is a pure-CPU timing gate; skipped under -race (enforced in the non-race coverage job)")
66+
}
67+
6068
const iter = 200_000
69+
const rounds = 5
6170

62-
avg := func(fn func()) time.Duration {
63-
for i := 0; i < 20_000; i++ { // warm up
64-
fn()
65-
}
71+
// measure returns the average per-op duration for fn over `iter` calls.
72+
measure := func(fn func()) time.Duration {
6673
start := time.Now()
6774
for i := 0; i < iter; i++ {
6875
fn()
6976
}
7077
return time.Since(start) / iter
7178
}
7279

73-
envelope := avg(envelopeRoundTrip)
74-
bare := avg(barePayloadJSON)
80+
// best returns the fastest (minimum) average across several rounds. CPU
81+
// contention, GC and scheduler preemption on shared CI runners only ever ADD
82+
// time, so the minimum is the closest estimate of the true pure-CPU cost. Taking
83+
// the best round makes this gate robust against a noisy runner instead of flaking
84+
// on it — it cannot mask a real regression (a genuinely slow codec is slow in
85+
// every round, including the fastest).
86+
best := func(fn func()) time.Duration {
87+
for i := 0; i < 20_000; i++ { // warm up
88+
fn()
89+
}
90+
min := measure(fn)
91+
for r := 1; r < rounds; r++ {
92+
if d := measure(fn); d < min {
93+
min = d
94+
}
95+
}
96+
return min
97+
}
98+
99+
envelope := best(envelopeRoundTrip)
100+
bare := best(barePayloadJSON)
75101

76102
marginal := envelope - bare
77103
if marginal < 0 {
78104
marginal = 0
79105
}
80106
overhead := float64(marginal) / float64(referenceBrokerRoundTrip) * 100
81107

82-
t.Logf("envelope codec: %v/op bare JSON: %v/op marginal: %v overhead vs %v broker: %.2f%%",
83-
envelope, bare, marginal, referenceBrokerRoundTrip, overhead)
108+
t.Logf("envelope codec: %v/op bare JSON: %v/op marginal: %v overhead vs %v broker: %.2f%% (best of %d rounds)",
109+
envelope, bare, marginal, referenceBrokerRoundTrip, overhead, rounds)
84110

85111
if overhead > 2.0 {
86112
t.Fatalf("codec overhead %.2f%% exceeds the 2%% GR-8 budget (marginal %v over a %v round-trip)",

race_enabled_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
//go:build race
2+
3+
package babelqueue_test
4+
5+
// raceEnabled reports whether the test binary was built with the race detector
6+
// (`go test -race`). See race_norace_test.go for why the GR-8 gate consults it.
7+
const raceEnabled = true

race_norace_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
//go:build !race
2+
3+
package babelqueue_test
4+
5+
// raceEnabled reports whether the test binary was built with the race detector
6+
// (`go test -race`). The GR-8 overhead gate is a pure-CPU timing measurement, so it
7+
// is skipped under -race (which instruments every memory access and distorts timing)
8+
// and enforced in the non-race coverage job instead.
9+
const raceEnabled = false

0 commit comments

Comments
 (0)