Files
magpie/internal/usage/pair_bench_test.go
T
yetone 17c90b6f2f usage: pairFixture's chunks take serials of their own (#2278)
TestPairScratchKeepsNothingBetweenCalls was red after any test that
paired another pairFixture of other sizes first: both fixtures named
their chunks 100, 101, ..., so msgKeysOf, which keeps a chunk's message
keys by its serial, read the other fixture's ids for this one's chunks
and left 18 of 108 calls out as copies ("a first call matched 90 of
108"). It passed alone and in the package's full order, so it showed
only under a -run of the pairing tests.

Run at the rebased tree (on origin/main 387e8572a, on mote): gofmt -l clean;
go vet -tags nogui ./internal/usage/ for linux, windows and darwin ok;
go test ./internal/usage/ ok (34.5s), ./internal/usageapi/ ok;
-run 'VisibleLocal|Pair|PageSplit|Heat|Effort|QueryPage' ok.
Broken by hand (serials 1 and 100+f again): that -run is red,
TestPairScratchKeepsNothingBetweenCalls "a first call matched 90 of 108".
2026-10-30 20:21:52 +08:00

50 lines
2.0 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)
// serials of their own: two fixtures' chunks under one serial read each
// other's kept message keys (msgKeysOf), whichever test ran first
g := &rowChunk{serial: nextChunkSerial()}
n := 0
for f := 0; f < files; f++ {
c := &rowChunk{serial: nextChunkSerial()}
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())
}
}
}