fetch_ml/tests/benchmarks/response_packet_regression_test.go
Jeremie Fraeys 6c83bda608
test(benchmarks): add tolerance to response packet regression test
Add 5% tolerance for timing noise to prevent flaky failures from
nanosecond-level benchmark variations
2026-02-18 12:45:40 -05:00

42 lines
1.3 KiB
Go

package benchmarks
import "testing"
var packetAllocCeil = map[string]int64{
"success": 1,
"error": 1,
"progress": 1,
"data": 3,
}
func TestResponsePacketSerializationRegression(t *testing.T) {
for _, variant := range benchmarkPackets {
variant := variant
t.Run(variant.name, func(t *testing.T) {
current := testing.Benchmark(func(b *testing.B) {
benchmarkSerializePacket(b, variant.packet)
})
legacy := testing.Benchmark(func(b *testing.B) {
benchmarkLegacySerializePacket(b, variant.packet)
})
// Allow 5% tolerance for timing noise - single nanosecond differences are within measurement error
tolerance := int64(float64(legacy.NsPerOp()) * 0.05)
if current.NsPerOp() > legacy.NsPerOp()+tolerance {
t.Fatalf("current serialize slower than legacy: current=%dns legacy=%dns (tolerance=%dns)", current.NsPerOp(), legacy.NsPerOp(), tolerance)
}
if ceil, ok := packetAllocCeil[variant.name]; ok && current.AllocsPerOp() > ceil {
t.Fatalf("current serialize allocs/regression: got %d want <= %d", current.AllocsPerOp(), ceil)
}
if current.AllocsPerOp() > legacy.AllocsPerOp() {
t.Fatalf(
"current serialize uses more allocations than legacy: current %d legacy %d",
current.AllocsPerOp(),
legacy.AllocsPerOp(),
)
}
})
}
}