Files
magpie/internal/usage/pair_bench_test.go
T
yetone 06469694b7 usage: pairing a poll's calls allocates 40% less, in a few slabs (#2278)
Under polls, with calls coming in, GC was ~35% of magpie's CPU and
madvise another 16%, both driven by how much a poll allocates. The
pairing that the heatmap and every page run did most of it: in an 80 s
heap diff on a year's history, matchedBlocks allocated 3.2 GB and
codexChunkResponsePairs 2.6 GB.

- matchedBlocks: a match key holds a call or two, so each key's ends
  and range counts were two slices of their own. They are now one ends
  slab and one counts slab, a group an offset into them, and groupOf is
  made the size of the calls it keeps. The candidates and unique maps
  are a slice and a []bool over slab positions.
- codexChunkResponsePairs: picks and their slab are made the size of
  the gateway's response ids, and a gateway call is judged from its
  packed strings (codexGatewayCall) instead of being made a whole Row.

What a call pairs with is unchanged.

BenchmarkPairRows (new; 2,000 session files of 100 calls, half Codex by
response id, half Claude Code by session and tokens), on mote:
  e0ea15b8e: 170,154,300 B/op, 208,673 allocs/op
  this:      100,846,208 B/op,   7,056 allocs/op
(ns/op isn't comparable: mote was at load 20-35, both ~0.7-1.0 s/op.)

Not yet measured: the poll-and-feed A/B against a real build on mote
(load 86 from other runs); its numbers go on #2278.

Run at this head on mote: go test ./internal/usage ./internal/usageapi
ok; go test ./internal/sessions ok; ./internal/gui: TestMainWindowCorners/
macOS_26_SDK red only, #3949 (macOS 27 AppKit, untouched here); go vet
-tags nogui ./internal/usage/... ./internal/gui/... ./internal/usageapi/...
for linux, windows and darwin ok.

Broken by hand: the counts slab without the group's +k offset (groups'
range counts overlap) failed TestIndexedExportMatchesExhaustiveOracle,
TestRequestMatchIndexEquivalent, TestMatchOpensOnlyNamedChunks and
TestCodexCacheWriteMatchesGatewayCall; codexGatewayCall taking a
rejected call failed TestCodexRejectedGatewayDoesNotPairWithNativeCall.

Not run: the full ./... suite.
2026-10-29 03:40:18 +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 len(ps.matched) != 200000 {
b.Fatal(len(ps.matched))
}
}
}