Files
magpie/internal/sessions/stats_native_test.go
T
yetone 536a98316d sessions: a poll over unchanged files gets the stats the last one read (#2278)
The Usage page asks for its range every 10 s and the Sessions page for
7, 30 and every day every 15 s. Each read put every session of every
range together again, though between two polls the files mostly
haven't changed: statsReadAll allocated ~1.1 GB per 80 s of polling,
a fifth of all magpie allocated.

statsKept keeps each range's Stats (at most 8 ranges) and the native
keys, with what they were read from: every listed file's path, key,
agent, size, mod time, main/read-only, WSL distro and its parse pointer
(a published parse is never written; a changed file gets a new one),
today, and the cache generation. It also keeps the answers the read
got from the prices, Codex's thread names and Claude Desktop's names,
and asks them again before it hands anything out. When any of these
differ, everything is read again. The listing and the refresh of
changed files still run on every poll.

MergeExternalStats wrote into the Stats it was given (a day's usage
rows, an append into spare room in Sessions, sorts in place). It now
copies whatever it changes, so a kept range is never written.

What this doesn't help: while an agent is writing a session, every
poll sees a changed file and reads everything again. Reading only the
changed sessions is the next change.

Run at the pushed head on mote:
- GOOS=linux/windows/darwin go vet -tags nogui ./internal/sessions/
  ./internal/gui/: ok.
- go test -count=1 -timeout 45m ./internal/sessions/ ./internal/gui/
  ./internal/usage/: sessions ok, usage ok, gui red only
  TestMainWindowCorners/macOS_26_SDK (#3949, open, same as main).
- New tests: TestStatsKeptBetweenPolls (the same backing array on an
  unchanged poll; a grown file and a price change are read in),
  TestStatsKeptNewDay, TestStatsKeptRangesBounded,
  TestMergeExternalStatsWritesNoInput. TestStatsAllRangesOnePass now
  forgets the kept ranges before each single read.
- Broken by hand, each red: keptFor always missing
  (TestStatsKeptBetweenPolls, "put together again"); the price check
  dropped (repriced cost unchanged); MergeExternalStats without its
  copies (TestMergeExternalStatsWritesNoInput).
- Real data (a copy of the owner's history, ~34k sessions), v0.1.1978
  against this: /api/sessions/stats?days=0/7/30 and both overviews
  asked 4 times 12 s apart, each pair byte-identical (1.97 MB for
  days=0).
- A/B, 80 s of GUI polling, v0.1.1978 against this: allocated 5512 to
  4480 MB, statsReadAll's share 1108 to 261 MB (the listing);
  GC 3.90 to 3.42 s, madvise 1.05 to 0.61 s, CPU 28.6 to 25.3%, avg
  RSS 1061 to 1019 MiB. mote's load was 20-40, so latencies aren't
  compared. The A/B binary was built before the 8-range bound was added.
- Rebased onto b9f1fcddd (redact only), then again: vet on the three
  systems over sessions, gui and redact, and go test of sessions, usage
  and redact: ok.
- Rebased again onto a4e929212 (GUI context map only), then at this
  head: the same vet and tests ok. TestCoreScriptBudget is red at
  a4e929212 itself (#4133); this commit touches no GUI file.
2026-10-29 08:52:32 +08:00

64 lines
1.6 KiB
Go

package sessions
import (
"reflect"
"testing"
"time"
)
// The overview's stats and the sessions that tell its gateway sessions
// apart come of one pass over every agent's files (#2776): it took a second
// pass, through List(All), on every refresh. The keys are List's.
func TestStatsAndNativeOnePass(t *testing.T) {
setup(t)
want := map[string]bool{}
for _, s := range List(All) {
want[s.Agent+"|"+s.ID] = true
}
passes := 0
filesHook = func() { passes++ }
t.Cleanup(func() { filesHook = nil })
st, native := StatsAndNative(0)
if passes != 1 {
t.Fatalf("%d passes over the files", passes)
}
if len(st.Sessions) == 0 {
t.Fatalf("no stats: %+v", st)
}
if len(native) != len(want) || len(want) < 2 {
t.Fatalf("native %v, List's %v", native, want)
}
for k := range want {
if !native[k] {
t.Fatalf("native %v, List's %v", native, want)
}
}
}
// TestStatsAllRangesOnePass: the ranges statsFor keeps are read again in
// one pass, and each comes out as its own read would (#2278).
func TestStatsAllRangesOnePass(t *testing.T) {
setup(t)
now := time.Now()
ranges := []int{0, 1, 7, 30}
passes := 0
filesHook = func() { passes++ }
t.Cleanup(func() { filesHook = nil })
all := statsReadAll(ranges, now, map[string]bool{})
if passes != 1 {
t.Fatalf("%d passes over the files", passes)
}
if len(all[0].Sessions) == 0 {
t.Fatalf("no stats: %+v", all[0])
}
for i, days := range ranges {
// read again, not the ranges kept
mu.Lock()
forgetKeptStats()
mu.Unlock()
if one := statsRead(days, now, nil); !reflect.DeepEqual(all[i], one) {
t.Fatalf("days=%d: one pass %+v\nalone %+v", days, all[i], one)
}
}
}