Files
magpie/internal/sessions/overview_alloc_test.go
T
yetone 3ec9babe7d sessions: the overview points to the sessions it sums up, not copies (#2278)
Stats.Overview copied every Summary (~400 bytes) into its working slice,
then cloned that slice once per Top ranking. Over 34k sessions the
Sessions page's poll allocated ~1.5 GB in two minutes this way while an
agent worked (alloc profile of v0.1.1928 on mote: `in = append` 958 MB,
slices.Clone 557 MB). It now ranks pointers into st.Sessions and copies
only the k sessions each ranking keeps. shapes, toolUse and skillUse take
the pointers.

TestOverviewCopiesOnlyItsTop: a second Overview over 5000 sessions
allocates at most half of their Summaries' size, and its count and
rankings are what they were. Red on the old code (14,923,144 bytes over a
1,020,000 bar), green on the new.

A/B on mote (34,040 session files, 240k-row usage.jsonl, the feed run
of run2278c.sh: 230s, feed from 60s, Usage and Sessions pages polled),
v0.1.1928 code vs this change. mote's load was 47 to 66 from other runs
throughout, so the numbers are noisy:
  avg CPU after the feed starts  122.8% -> 69.4%
  heatmap poll mean               6.97s -> 4.84s
  all polls mean                  3.11s -> 2.16s
  footprint peak                 685.6M -> 626.1M
  footprint at the end           402.8M -> 337.5M

Run on mote, at base b5d2858f4:
  go test -count=1 -timeout 45m ./internal/sessions/ ./internal/usage/
    ok (30.9s, 18.0s)
  go test ./internal/gui/: FAIL only in TestMainWindowCorners, the open
    macOS 27 red #3949
Rebased onto origin/main 7a7931833 (no change to internal/sessions), and
run again at the pushed head on mote:
  go test -count=1 -timeout 45m ./internal/sessions/ ./internal/usage/
    ok (16.8s, 16.1s)
  GOOS=linux/windows/darwin go vet -tags nogui ./internal/sessions/
    ./internal/usage/ ./internal/gui/ ok
2026-10-29 00:48:47 +08:00

31 lines
1.2 KiB
Go

package sessions
import (
"fmt"
"runtime"
"testing"
"unsafe"
)
// The overview points to the sessions it sums up and copies only the few
// it shows: the Sessions page's poll copied every Summary four times, ~1.5
// GB in two minutes over 34k sessions while an agent worked (#2278).
func TestOverviewCopiesOnlyItsTop(t *testing.T) {
st := Stats{From: "2026-10-01", To: "2026-10-28"}
for i := range 5000 {
st.Sessions = append(st.Sessions, Summary{Key: fmt.Sprint("claude:", i), Agent: "claude", ID: fmt.Sprint(i), Tokens: Tokens{Input: i, Output: 1}, Cost: float64(i), Active: int64(i), Days: []int{i % 28}})
}
st.Overview("", "", "", 8) // the maps' first growth
var before, after runtime.MemStats
runtime.ReadMemStats(&before)
o := st.Overview("", "", "", 8)
runtime.ReadMemStats(&after)
if o.Count != 5000 || len(o.Top["tokens"]) != 8 || o.Top["tokens"][0].ID != "4999" || o.Top["cost"][7].ID != "4992" {
t.Fatalf("count %d, top %d, first %q", o.Count, len(o.Top["tokens"]), o.Top["tokens"][0].ID)
}
all := uint64(len(st.Sessions)) * uint64(unsafe.Sizeof(Summary{}))
if got := after.TotalAlloc - before.TotalAlloc; got > all/2 {
t.Fatalf("the overview allocated %d bytes; its sessions are %d", got, all)
}
}