Files
magpie/internal/gui/sessions_warm_test.go
T
yetone b1a2eec2c1 gui: the warm-up reads Codex's effort under the tight GC too
The warm-up's last step reads Codex's per-thread effort from the whole
ledger right after the usage page's read, while that page still holds
every row (~300 MB live on the owner's history). It ran at GOGC 100, so
the heap was let grow to about twice that: a heap profile every 2 s of a
cold start showed heapSys going 342 -> 622 MB while live stayed ~300,
in the seconds after QueryPage returned. It now runs under underTightGC
like the usage read.

Footprint (phys_footprint, what Activity Monitor shows), cold start on a
clone of the real history (15,176 files), alternating, two runs each:
  4042aa4d1  peak 611 / 575 MB  end RSS 659 / 651 MB  CPU 4:20.6 / 3:54.9
  this       peak 521 / 498 MB  end RSS 595 / 568 MB  CPU 4:11.7 / 4:00.7

Tests: TestWarmUpReadsCodexEffort now also checks the read runs at
GOGC 25; with the step put back to warmCodexEffort() by hand it is red.
go test ./... -timeout 45m on 083b1c8e7: 68 packages ok; rebased onto
b3f595349 (routing CSS/JS only), go test ./internal/gui ok again.
GOOS=linux and GOOS=windows go vet -tags nogui ./... clean; gofmt clean.
2026-10-22 00:04:28 +08:00

73 lines
2.0 KiB
Go

package gui
import (
"runtime"
"runtime/debug"
"testing"
"time"
"github.com/yetone/magpie/internal/sessions"
)
// The warm-up's garbage goes back to the system when it ends, not over the
// scavenger's next minutes: an idle magpie that just started read ~470 MB.
func TestWarmUpReturnsItsGarbage(t *testing.T) {
var keep [][]byte
warmUp(func() {
// a read that allocates far more than it keeps
for range 256 {
keep = append(keep, make([]byte, 1<<20))
}
keep = [][]byte{keep[0]}
})
var m runtime.MemStats
runtime.ReadMemStats(&m)
if held := m.HeapIdle - m.HeapReleased; held > 32<<20 {
t.Fatalf("after the warm-up %d MB of freed heap is still held from the system", held>>20)
}
if m.HeapAlloc > 128<<20 {
t.Fatalf("after the warm-up %d MB of heap is still in use", m.HeapAlloc>>20)
}
runtime.KeepAlive(keep)
}
// With Codex sessions on this computer the warm-up also reads Codex's
// per-thread effort, which a first Codex page after a start otherwise read
// from the whole ledger while it waited (#2776); without any it doesn't.
// It reads under the tight GC: the usage page's rows are still held then.
func TestWarmUpReadsCodexEffort(t *testing.T) {
defer debug.SetGCPercent(debug.SetGCPercent(100))
statsMemo.Lock()
saved := statsMemo.m
statsMemo.Unlock()
old := warmCodexEffort
t.Cleanup(func() {
statsMemo.Lock()
statsMemo.m = saved
statsMemo.Unlock()
warmCodexEffort = old
})
for _, c := range []struct {
keys []string
want int
}{{[]string{"claude:a", "codex:b"}, 1}, {[]string{"claude:a"}, 0}} {
statsMemo.Lock()
statsMemo.m = map[int]*statsKept{}
for _, d := range []int{0, 30} {
statsMemo.m[d] = &statsKept{at: time.Now(), st: sessions.Stats{Keys: c.keys}}
}
statsMemo.Unlock()
read := 0
warmCodexEffort = func() {
read++
if p := gcPercent(); p != tightGCPercent {
t.Errorf("Codex's effort was read under GOGC=%d, want %d", p, tightGCPercent)
}
}
warmUp(warmSteps()...)
if read != c.want {
t.Fatalf("with sessions %v Codex's effort was read %d times, want %d", c.keys, read, c.want)
}
}
}