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(),
				)
			}
		})
	}
}
