Files
magpie/internal/usage/vias_fold_test.go
T
yetone 2be04305fd usage: a session's model lists count only the log's blocks since the last
The whole log's model lists (cachedVias) were counted again from every
call in the log whenever it grew, which with calls coming in is every
poll: buildVias was ~3% of CPU on the #2278 bench. They are now counted
on from what the log's full blocks counted, which never change, and only
the blocks after are counted again. A session's counts are copied from
the prefix before they are added to, so the prefix is never written.
Rows are counted in the log's order either way.

The prefix is kept for the same clients and renames key as the lists,
and is dropped when the log is read afresh (its blocks differ). Window
lists (a since) are built as before.

TestViasCountedOnAreCountedAfresh DeepEquals the lists counted on with
buildVias afresh, after appends over a block's edge, a new session and
model, and the log written over, and checks that lists were counted on.
Broken by hand (a prefix session's list cut in half): red at "first".

Run at the next commit's tree (this one plus the gateway projection's
fold, in other files): go test ./internal/usage/... ./internal/usageapi/...
./internal/tui/... ok; ./internal/gui/ red only on TestMainWindowCorners
(#3949); GOOS=linux,windows,darwin go vet -tags nogui ./internal/usage/...
./internal/gui/ ok. Bench numbers are in the next commit.

Refs #2278
2026-10-29 06:21:46 +08:00

52 lines
1.7 KiB
Go

package usage
import (
"fmt"
"reflect"
"testing"
"time"
"github.com/yetone/magpie/internal/provider"
)
// The whole log's model lists, counted on from the full blocks counted
// before, are the lists counted afresh: as calls come in over a block's
// edge, and when the log is written over.
func TestViasCountedOnAreCountedAfresh(t *testing.T) {
pageHome(t)
now := holdClock(t, time.Date(2026, 9, 30, 12, 0, 0, 0, time.Local))
writeFoldLog(t, now, 3*logBlockRows+100)
check := func(step string) {
t.Helper()
got := cachedVias(time.Time{}).all
want := buildVias(readLogSnapshot(), time.Time{}, provider.UsageRenames())
if len(got.m) == 0 {
t.Fatalf("%s: no lists", step)
}
if !reflect.DeepEqual(got.m, want.m) || !reflect.DeepEqual(got.first, want.first) {
t.Fatalf("%s: counted on %d lists, afresh %d, or they differ", step, len(got.m), len(want.m))
}
}
check("first")
before := viaFolds
for i := 0; i < logBlockRows+50; i++ {
r := foldRecord(now, i)
r.Time = now.Add(-time.Duration(i) * time.Second)
r.Effort = []string{"", "high"}[i%2]
Append(r)
if i == 0 || i == 600 {
check(fmt.Sprint("appended ", i+1))
}
}
check("appended a block")
if viaFolds-before < 2 {
t.Fatalf("only %d lists counted on from what they had counted", viaFolds-before)
}
// a session first seen in the last block, then a model it never used
Append(Record{Time: now, Agent: "codex", Session: "new", Provider: "openai", Model: "m3", Input: 5, Status: 200})
Append(Record{Time: now, Agent: "codex", Session: "s0-0", Provider: "openai", Model: "m3", Input: 5, Status: 200})
check("a new model")
writeFoldLog(t, now, 2*logBlockRows+7)
check("written over")
}