Vias kept the lists for one since. The Sessions list asks from its
oldest listed session's start, each agent's manage page from that
agent's oldest, and a session's detail from that session's start, so
an open page's polls almost never asked the kept since, and each ask
read and grouped the whole log again. Idle with an open Sessions page
on the 720k-call sandbox, buildVias allocated 9.1 GB in 8 minutes,
78% of everything allocated, which is most of why the idle process
churned its heap and grew.
The cache now keeps one index from the whole log and each session's
first call. A session whose calls all came at or after the since is
answered from it. A session with calls before its since is answered
from one kept window for that since, built only when such a session
is read. Len counts the sessions with a call since the index's time.
Both are kept per log version and agent list, as before.
TestViasAnswerEverySinceFromOneIndex writes 20,001 sessions and asks
from three sinces in turn, as the three pages do: 2,677 bytes an ask.
On the old cache: red, 11,580,100 bytes an ask. It also checks that a
session with a call on each side of a since counts only the one after
it, and what Len says for a window and for every session.
TestGatewaySessionsCachePricesWindowsAndRewrite's window check and
TestUsageViasSkipsOldHistory stay green.
720k-call sandbox, idle with an open page's polls every 15 s (the
Sessions list, stats, Claude's manage page, Requests, state), before
and after side by side for 12 minutes, load ~170-250. Allocated from
minute 4 to minute 12: buildVias 9,100 MB before, 0 after (built once,
before minute 4). The faster answers let the poll loop run about twice
as often, so the totals (11.6 GB and 10.5 GB) aren't per poll. The
live heap ran 352-641 MB before and 514-724 MB after, and RSS 730-962
and 555-1217 MB; both swing with where a poll is when the snapshot is
taken and the machine's load, and neither shows a trend in 12 minutes.
Run: go test -p 4 -count=1 ./internal/usage/ -run
'Vias|KeptStrings|GatewaySessionsCachePrices|Incremental|Review': ok.
Full suite on this series' final tree (aa678b482 before this note was
added), rebased on origin/main f76201a84: go test -p 4 ./... -timeout
45m, exit 0, 64 packages ok and none failed (gateway 1783 s, gui 361 s),
41 minutes at load ~190-200. gofmt -l: no files. go vet: darwin, linux
(-tags nogui) and windows exit 0. The GUI suite wasn't run: no file
under internal/gui/assets changed.
87 lines
2.9 KiB
Go
87 lines
2.9 KiB
Go
package usage
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"runtime"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
// The Sessions list, each agent's manage page and a session's detail each
|
|
// ask Vias from a since of their own. One kept since was built again from
|
|
// the whole log at almost every ask, ~280 MB a time over ~720k calls, on
|
|
// every poll of an open page (#2776). One index now answers every since a
|
|
// session's calls all came after, and a session with calls before its
|
|
// since still counts only those after it.
|
|
func TestViasAnswerEverySinceFromOneIndex(t *testing.T) {
|
|
pageHome(t)
|
|
settleLogClock(t)
|
|
const sessionCount = 20000
|
|
if err := os.MkdirAll(filepath.Dir(Path()), 0o700); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
f, err := os.Create(Path())
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
enc := json.NewEncoder(f)
|
|
now := Clock()
|
|
write := func(r Record) {
|
|
if err := enc.Encode(r); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
for i := range sessionCount {
|
|
write(Record{Time: now.Add(-time.Duration(sessionCount-i) * time.Second), Agent: "pi", Session: fmt.Sprintf("s%d", i), Provider: "p", Model: "m", Input: 10, Status: 200})
|
|
}
|
|
// one session with a call on each side of a since
|
|
write(Record{Time: now.Add(-time.Hour), Agent: "pi", Session: "both", Provider: "p", Model: "m", Input: 10, Status: 200})
|
|
write(Record{Time: now, Agent: "pi", Session: "both", Provider: "p", Model: "m", Input: 10, Status: 200})
|
|
if err := f.Close(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if got := Vias(now.Add(-time.Minute)).Of("pi|both"); len(got) != 1 || got[0].Calls != 1 {
|
|
t.Fatalf("since a minute ago: %+v, want the one call after it", got)
|
|
}
|
|
if got := Vias(time.Time{}).Of("pi|both"); len(got) != 1 || got[0].Calls != 2 {
|
|
t.Fatalf("since ever: %+v, want both calls", got)
|
|
}
|
|
// the last 60 sessions, a second apart up to a second ago, and "both"
|
|
if n := Vias(now.Add(-time.Minute)).Len(); n != 61 {
|
|
t.Fatalf("%d sessions with calls since a minute ago, want 61", n)
|
|
}
|
|
if n := Vias(time.Time{}).Len(); n != sessionCount+1 {
|
|
t.Fatalf("%d sessions, want %d", n, sessionCount+1)
|
|
}
|
|
// the list, a manage page and a detail, asking in turn
|
|
sinces := []time.Time{
|
|
now.Add(-(sessionCount + 60) * time.Second),
|
|
now.Add(-(sessionCount/2 + 60) * time.Second),
|
|
now.Add(-(sessionCount - 10 + 60) * time.Second),
|
|
}
|
|
key := fmt.Sprintf("pi|s%d", sessionCount-10)
|
|
for _, since := range sinces {
|
|
if got := Vias(since).Of(key); len(got) != 1 || got[0].Calls != 1 {
|
|
t.Fatalf("since %v: %+v", since, got)
|
|
}
|
|
}
|
|
const asks = 30
|
|
var before, after runtime.MemStats
|
|
runtime.GC()
|
|
runtime.ReadMemStats(&before)
|
|
for i := range asks {
|
|
Vias(sinces[i%len(sinces)]).Of(key)
|
|
}
|
|
runtime.ReadMemStats(&after)
|
|
// building the lists of 20000 sessions is several MB; the log's check
|
|
// and one list are tens of KB
|
|
per := (after.TotalAlloc - before.TotalAlloc) / asks
|
|
t.Logf("an ask allocated %d bytes", per)
|
|
if per > 256<<10 {
|
|
t.Fatalf("an ask allocated %d bytes: the lists were built again", per)
|
|
}
|
|
}
|