fetch_ml/tests/benchmarks/response_packet_regression_test.go

40 lines
1.1 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)
})
if current.NsPerOp() > legacy.NsPerOp() {
t.Fatalf("current serialize slower than legacy: current=%dns legacy=%dns", current.NsPerOp(), legacy.NsPerOp())
}
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(),
)
}
})
}
}