diff --git a/cmd/server/hidden_identity_packets_bench_test.go b/cmd/server/hidden_identity_packets_bench_test.go index 6ec2844f6..e8c0f1bdb 100644 --- a/cmd/server/hidden_identity_packets_bench_test.go +++ b/cmd/server/hidden_identity_packets_bench_test.go @@ -2,7 +2,11 @@ package main import ( "fmt" + "os" + "os/exec" + "strings" "testing" + "time" ) // #1023 perf proof (AGENTS.md rule 0): the hidden-identity predicate on the @@ -147,15 +151,87 @@ func BenchmarkHiddenRebuild30K(b *testing.B) { } } +// hidNoiseSink keeps hidAllocNoise's allocations on the heap. +var hidNoiseSink []byte + +// hidAllocNoise allocates in the background until the test ends. It stands +// in for the goroutines earlier tests leave running in the same binary: in +// the -race shard (#1088) 95 schema healers, each re-probing its DB every +// 2 s, allocated 5-11 times per filter call while it was measured. +func hidAllocNoise(t *testing.T) { + stop, done := make(chan struct{}), make(chan struct{}) + t.Cleanup(func() { close(stop); <-done }) + go func() { + defer close(done) + tick := time.NewTicker(time.Millisecond) + defer tick.Stop() + for { + select { + case <-stop: + return + case <-tick.C: + for i := 0; i < 1000; i++ { + hidNoiseSink = make([]byte, 16) + } + } + } + }() +} + // TestHiddenFilterPacketsNoExtraAllocs enforces the per-packet claim: the // filter over 30K packets allocates no more per call with nothing hidden, // or with 50 hidden identities, than with no hiding configured — a few -// constant allocations at most (the predicate), never one per packet. -// AllocsPerRun counts every goroutine's allocations, and a full test run -// leaves background goroutines behind, so each mode takes the least of a +// constant allocations at most (the predicate), never one per packet — and +// with no hiding configured less than one per 500 packets (the result +// slice's growth: 20 allocations at 30K, 23 under -race). AllocsPerRun +// counts every goroutine's allocations, and goroutines earlier tests leave +// running allocate while it counts (#1088), so the figures are taken in a +// child process that runs this test alone; each mode takes the least of a // few measurements and a small constant slack is allowed. func TestHiddenFilterPacketsNoExtraAllocs(t *testing.T) { + if os.Getenv("CORESCOPE_1088_ALLOCS_CHILD") == "1" { + off, nOff, none, nNone, hid, nHid := hidFilterAllocs(t) + fmt.Printf("HID1088 off=%g/%d none=%g/%d hid=%g/%d\n", off, nOff, none, nNone, hid, nHid) + return + } const slack = 3 + hidAllocNoise(t) + cmd := exec.Command(os.Args[0], "-test.run=^TestHiddenFilterPacketsNoExtraAllocs$", "-test.count=1") + cmd.Env = append(os.Environ(), "CORESCOPE_1088_ALLOCS_CHILD=1") + out, err := cmd.CombinedOutput() + if err != nil { + t.Fatalf("child: %v\n%s", err, out) + } + line := "" + for _, l := range strings.Split(string(out), "\n") { + if strings.HasPrefix(l, "HID1088 ") { + line = l + } + } + var off, none, hid float64 + var nOff, nNone, nHid int + if _, err := fmt.Sscanf(line, "HID1088 off=%g/%d none=%g/%d hid=%g/%d", &off, &nOff, &none, &nNone, &hid, &nHid); err != nil { + t.Fatalf("no measurement in the child's output (%v):\n%s", err, out) + } + t.Log(line) + if off > float64(nOff)/500 { + t.Errorf("no config: %v allocs for %d packets", off, nOff) + } + if none > off+slack || nNone != nOff { + t.Errorf("nothing hidden: %v allocs / %d packets, no config: %v / %d", none, nNone, off, nOff) + } + if hid > off+slack { + t.Errorf("50 hidden: %v allocs, no config: %v", hid, off) + } + if nHid >= nOff { + t.Errorf("50 hidden: %d packets of %d, none hidden", nHid, nOff) + } +} + +// hidFilterAllocs is the filter's allocations per call over 30K packets, +// and the packets it returns, with no hiding configured, with nothing +// hidden and with 50 hidden identities. +func hidFilterAllocs(t *testing.T) (off float64, nOff int, none float64, nNone int, hid float64, nHid int) { s := hidBenchStore(t, 30000, 2000, 50, "off") allocs := func(prefixes []string) (float64, int) { hidBenchConfigure(s, prefixes) @@ -173,16 +249,8 @@ func TestHiddenFilterPacketsNoExtraAllocs(t *testing.T) { } return least, n } - off, nOff := allocs(hidBenchModes[0].prefixes) - none, nNone := allocs(hidBenchModes[1].prefixes) - hid, nHid := allocs(hidBenchModes[2].prefixes) - if none > off+slack || nNone != nOff { - t.Errorf("nothing hidden: %v allocs / %d packets, no config: %v / %d", none, nNone, off, nOff) - } - if hid > off+slack { - t.Errorf("50 hidden: %v allocs, no config: %v", hid, off) - } - if nHid >= nOff { - t.Errorf("50 hidden: %d packets of %d, none hidden", nHid, nOff) - } + off, nOff = allocs(hidBenchModes[0].prefixes) + none, nNone = allocs(hidBenchModes[1].prefixes) + hid, nHid = allocs(hidBenchModes[2].prefixes) + return }