package pipeline import ( "fmt" "math/rand" "net/netip" "testing" "evobgp/internal/store" ) // benchPrefixRows generates seed-stable pseudo-random prefixes for aggregation benchmarks. // v4 blocks are carved from TEST-NET-style ranges; v6 from 2001:db8::/32. func benchPrefixRows(n int, v6Share float64) []store.PrefixRow { r := rand.New(rand.NewSource(42)) rows := make([]store.PrefixRow, 0, n) seen := make(map[string]struct{}, n) for len(rows) < n { var pfx netip.Prefix if r.Float64() < v6Share { var a [16]byte a[0], a[1] = 0x20, 0x01 a[2], a[3] = 0x0d, 0xb8 for i := 4; i < 10; i++ { a[i] = byte(r.Intn(256)) } addr := netip.AddrFrom16(a) pfx = netip.PrefixFrom(addr, 48+r.Intn(17)) } else { b := []byte{203, 0, 113, 0} b[1] = byte(r.Intn(256)) b[2] = byte(r.Intn(256)) b[3] = byte(r.Intn(256)) addr := netip.AddrFrom4([4]byte{b[0], b[1], b[2], b[3]}) pfx = netip.PrefixFrom(addr, 16+r.Intn(9)) } s := pfx.Masked().String() if _, ok := seen[s]; ok { continue } seen[s] = struct{}{} rows = append(rows, store.PrefixRow{Prefix: s, Source: "ip_range"}) } return rows } func BenchmarkSmartAggregatePrefixRows(b *testing.B) { for _, n := range []int{1_000, 10_000, 50_000} { rows := benchPrefixRows(n, 0.3) b.Run(fmt.Sprintf("n=%d", n), func(b *testing.B) { for i := 0; i < b.N; i++ { _ = smartAggregatePrefixRows(rows) } }) } }