Files
magpie/internal/usage/pair_bench_test.go
T
yetone fddd7ff08f usage: pairing keeps its rows as bits and its groups by sums, and lets its pairs go (#2278)
A cold start pairs the whole history, and pairing held its row sets as
map[rowRef]bool, ~40 bytes a row, several sets of most rows at once.
They are now refSet: a bitset a chunk, a bit a row. matchScratch's
groupOf was keyed by the whole matchKey (a session string and its
tokens); it is keyed by a 128-bit maphash sum of them now, and the
sessions map is filled when a group is made. pairedRows' pairs and
compactions maps were filled and never read; they are gone.

Measured on mote, a copy of the real full history (52G Codex, 13G
Claude), GODEBUG=gctrace=1, 120s of /api polls, 3 rounds each,
alternating, v0.1.2143 (807048470) against this change:
  peak live heap  459 -> 439 MB (512/426/440 vs 470/433/413)
  heap at GC      543 -> 517 MB
  footprint peak  677 -> 641 MB
The inuse profile at the peak shows groupOf 28.7 -> 11.8 MB and the
matched set gone from matchedBlocks. This is a small part of the peak;
the rest is the full re-pair itself, which is next.

Run at this tree (rebased onto dca3e2d71 from 807048470), on mote:
go vet ./internal/usage; GOOS=linux|windows|darwin go vet -tags nogui
./... all ok; go test -count=1 ./internal/usage ok (30.1s), with the
new TestRefSet and TestMatchKeySum (no allocation).
Not run: the full suite, the GUI suite.
2026-10-30 13:42:11 +08:00

48 lines
1.8 KiB
Go

package usage
import (
"fmt"
"testing"
"time"
)
// pairFixture is a long history as pairRows meets it on a poll: files
// session files of calls Codex calls each, every one also through the
// gateway by its response id, and as many Claude Code calls matched by
// session and tokens.
func pairFixture(files, calls int) (gateways, locals []*rowChunk) {
now := time.Date(2026, 10, 1, 12, 0, 0, 0, time.UTC)
g := &rowChunk{serial: 1}
n := 0
for f := 0; f < files; f++ {
c := &rowChunk{serial: uint64(100 + f)}
for i := 0; i < calls; i++ {
at := now.Add(-time.Duration(n) * 20 * time.Second)
n++
if f%2 == 0 {
id := fmt.Sprintf("resp_%d", n)
g.add(Row{Record: Record{Time: at, Agent: "codex", Provider: "openai", Model: "m", Input: 100 + i, Output: 5, ResponseID: id, Session: fmt.Sprint("s", f)}}, "", int64(n), false)
c.add(Row{Record: Record{Time: at, Agent: "codex", Provider: "openai", Model: "m", Input: 100 + i, Output: 5, ResponseID: id, Session: fmt.Sprint("s", f)}, Source: "log"}, "", int64(i), false)
} else {
g.add(Row{Record: Record{Time: at, Agent: "claude-code", Provider: "anthropic", Model: "c", Input: 100 + i, Output: 7, Session: fmt.Sprint("c", f)}}, "", int64(n), false)
c.add(Row{Record: Record{Time: at, Agent: "claude-code", Provider: "anthropic", Model: "c", Input: 100 + i, Output: 7, Session: fmt.Sprint("c", f)}, Source: "log"}, fmt.Sprint("msg_", n), int64(i), false)
}
}
locals = append(locals, c)
}
return []*rowChunk{g}, locals
}
func BenchmarkPairRows(b *testing.B) {
gateways, locals := pairFixture(2000, 100)
now := time.Date(2026, 10, 1, 13, 0, 0, 0, time.UTC)
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
ps := pairRows(All, now, gateways, locals, nil, nil)
if ps.matched.count() != 200000 {
b.Fatal(ps.matched.count())
}
}
}