From bd3c53086a3fe3ac177f4c656d1e521a2f0595b1 Mon Sep 17 00:00:00 2001 From: Paul Buetow Date: Tue, 24 Feb 2026 12:18:54 +0200 Subject: statsengine: compact process accumulator at high cardinality --- internal/statsengine/process_test.go | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) (limited to 'internal/statsengine/process_test.go') diff --git a/internal/statsengine/process_test.go b/internal/statsengine/process_test.go index add5ae1..aa3c5d2 100644 --- a/internal/statsengine/process_test.go +++ b/internal/statsengine/process_test.go @@ -115,6 +115,35 @@ func TestProcessAccumulatorNilInputs(t *testing.T) { } } +func TestProcessAccumulatorCompactsHighCardinality(t *testing.T) { + acc := newProcessAccumulatorWithLimits(2, 4) + + for i := 0; i < 5; i++ { + acc.Add(newProcessPair(10, "hot-a", 10, 1)) + } + for i := 0; i < 4; i++ { + acc.Add(newProcessPair(20, "hot-b", 10, 1)) + } + acc.Add(newProcessPair(1, "cold-1", 10, 1)) + acc.Add(newProcessPair(2, "cold-2", 10, 1)) + acc.Add(newProcessPair(3, "cold-3", 10, 1)) + + if got := len(acc.byPID); got != 2 { + t.Fatalf("expected compaction to keep topN processes, got %d entries", got) + } + if acc.byPID[10] == nil || acc.byPID[20] == nil { + t.Fatalf("expected hot pids to survive compaction") + } + + snap := acc.Snapshot(time.Second) + if len(snap) != 2 { + t.Fatalf("expected 2 rows after compaction, got %d", len(snap)) + } + if snap[0].PID != 10 || snap[1].PID != 20 { + t.Fatalf("unexpected rank order after compaction: %+v", snap) + } +} + func newProcessPair(pid uint32, comm string, duration uint64, bytes uint64) *event.Pair { return &event.Pair{ EnterEv: &types.RetEvent{Pid: pid}, -- cgit v1.2.3