The gateway's projection of all time (gatewayProjection) was built again from every call in the log whenever the log grew, every poll with calls coming in: buildGatewaySessions was ~8% of CPU on the #2278 bench. It is now counted on from the sessions its full blocks counted, which never change: a session the new blocks touch is copied from the prefix and counted on, the rest are reused as they are, and the list is sorted again. The prefix is kept per price meta (usagePriceCacheKey, which holds aliases and clients) and zone, and dropped when the log is read afresh. Ranges and native exclusions still read out of the projection, and buildGatewaySessions stays for a since. finishGatewayModels now sorts a session's models before it sums their cost, so the sum has one order however the models were gathered (it was map order before). TestGatewaySessionsCountedOnAreCountedAfresh DeepEquals every session counted on with buildGatewaySessions afresh, after appends over a block's edge, a new session and a call older than the rest, a zone change and the log written over, and checks it was counted on. Broken by hand (a prefix session's days dropped): red at "first". Run at this tree on mote: go test -count=1 ./internal/usage/... ./internal/usageapi/... ./internal/tui/... ok; ./internal/gui/ red only on TestMainWindowCorners/macOS_26_SDK (#3949, open); GOOS=linux,windows, darwin go vet -tags nogui ./internal/usage/... ./internal/gui/ ok. The #2278 bench on mote (15G .codex, 10 endpoints polled every 5s, 4 calls/s appended, 240s), v0.1.1966 then this tree, back to back: - CPU profile 60s: 24.17s -> 20.83s samples; buildGatewaySessions (1.07s) and buildVias (0.79s) gone - poll mean 0.57s -> 0.51s; /api/sessions 1.47 -> 1.04s, overview 0.93 -> 0.63s, stats 0.49-0.52 -> 0.26-0.32s, heatmap 1.48 -> 1.19s - cpu after feed 43.6% -> 41.7%; RSS peak 982 -> 1125 MiB, footprint peak 742.5 -> 744.1M (the cold-start peak swings run to run; not this) Not tried: a real account's log (the bench log is generated). Refs #2278
61 lines
1.9 KiB
Go
61 lines
1.9 KiB
Go
package usage
|
|
|
|
import (
|
|
"fmt"
|
|
"reflect"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/yetone/magpie/internal/provider"
|
|
)
|
|
|
|
// The gateway's sessions of all time, counted on from the full blocks
|
|
// counted before, are the sessions counted afresh, to the last bit of
|
|
// every cost: as calls come in over a block's edge, when a zone moves
|
|
// every day, and when the log is written over.
|
|
func TestGatewaySessionsCountedOnAreCountedAfresh(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 := gatewayProjection().sessions
|
|
want, _ := buildGatewaySessions(readLogSnapshot(), time.Time{}, nil, provider.UsageAlias())
|
|
if len(got) == 0 {
|
|
t.Fatalf("%s: no sessions", step)
|
|
}
|
|
if len(got) != len(want) {
|
|
t.Fatalf("%s: counted on %d sessions, afresh %d", step, len(got), len(want))
|
|
}
|
|
for i := range want {
|
|
if !reflect.DeepEqual(got[i], want[i]) {
|
|
t.Fatalf("%s: session %d counted on\n%+v\nafresh\n%+v", step, i, got[i], want[i])
|
|
}
|
|
}
|
|
}
|
|
check("first")
|
|
before := gatewayFolds
|
|
for i := 0; i < logBlockRows+50; i++ {
|
|
r := foldRecord(now, i)
|
|
r.Time = now.Add(-time.Duration(i) * time.Second)
|
|
Append(r)
|
|
if i == 0 || i == 600 {
|
|
check(fmt.Sprint("appended ", i+1))
|
|
}
|
|
}
|
|
check("appended a block")
|
|
if gatewayFolds-before < 2 {
|
|
t.Fatalf("only %d projections counted on from what they had counted", gatewayFolds-before)
|
|
}
|
|
Append(Record{Time: now, Agent: "codex", Session: "new", Provider: "openai", Model: "m3", Input: 5, Status: 200})
|
|
Append(Record{Time: now.Add(-40 * 24 * time.Hour), Agent: "codex", Session: "s0-0", Provider: "openai", Model: "m3", Input: 5, Status: 200})
|
|
check("a new session, and an older call")
|
|
zone := time.Local
|
|
time.Local = time.FixedZone("far", 13*3600)
|
|
t.Cleanup(func() { time.Local = zone })
|
|
check("another zone")
|
|
time.Local = zone
|
|
writeFoldLog(t, now, 2*logBlockRows+7)
|
|
check("written over")
|
|
}
|