Files
magpie/internal/gui/sessions_stats_idle_test.go
T
yetone 53280aa08e sessions: the stats caches are let go two minutes after the page stops asking
The Sessions page polls /api/sessions/stats every 15 s. Each range it asked
for kept its Stats, the overview and tag sums, about 15 MB on a 14k-session
history, for as long as magpie ran, with the window closed too. Now a timer
armed on every ask drops them two minutes after the last one (not while a
read is busy), with the sessions package's kept ranges, then returns the
memory to the OS. The parsed files stay, so asking again sums them up
again without reading a file.

Measured on mote with a copy of the real accounts (14k sessions), the
statsIdle delay set to 20 s in a measuring build only:
- footprint after six stats asks: t47 (origin/main) 140 MB, t48 151 MB;
- 45 s later: t47 130 MB, t48 116 MB;
- asking again after the release: days=0 0.34 s (was 0.06 s while kept),
  days=30 0.17 s.

Run on mote: go vet ./internal/gui ./internal/sessions; go test
./internal/gui ./internal/sessions ok. TestIdleStatsLetGo is red with
`statsMemo.m = nil` taken out ("2 ranges ... kept; want none").
The timer is off under go test, so other gui tests keep their memos.
Not tried: the GUI page itself; the full suite.
2026-11-10 13:58:41 +08:00

91 lines
2.2 KiB
Go

package gui
import (
"net/http/httptest"
"testing"
)
// Once the Sessions page has gone unasked for statsIdle, every range's
// Stats, the overviews and the answers' tags are let go: a long history's
// were ~15 MB held for good by a window that showed another page, or none.
// The next ask is answered as before, from a new read.
func TestIdleStatsLetGo(t *testing.T) {
forgetStats()
t.Cleanup(forgetStats)
statsIdleTimer.Lock()
statsIdleTimer.off = false
statsIdleTimer.Unlock()
t.Cleanup(func() {
statsIdleTimer.Lock()
if statsIdleTimer.t != nil {
statsIdleTimer.t.Stop()
statsIdleTimer.t = nil
}
statsIdleTimer.off = true
statsIdleTimer.Unlock()
})
before, gen := statsForGen(0)
statsFor(30)
writeOverview(httptest.NewRecorder(), 0, "", "", "")
overviews.Lock()
had := len(overviews.m)
overviews.Unlock()
if had == 0 {
t.Fatal("the overview asked for was not kept")
}
statsIdleTimer.Lock()
armed := statsIdleTimer.t != nil
statsIdleTimer.Unlock()
if !armed {
t.Fatal("an ask for stats armed no idle timer")
}
forgetIdleStats()
statsMemo.Lock()
n := len(statsMemo.m)
statsMemo.Unlock()
overviews.Lock()
o := len(overviews.m)
overviews.Unlock()
statsTags.Lock()
tags := len(statsTags.m)
statsTags.Unlock()
if n != 0 || o != 0 || tags != 0 {
t.Fatalf("after the idle timer: %d ranges, %d overviews, %d tags kept; want none", n, o, tags)
}
after, gen2 := statsForGen(0)
if gen2 == gen {
t.Fatal("the range asked after the idle timer was answered from what it let go")
}
if !sameStats(before, after) {
t.Fatal("the range read again differs from the one let go")
}
}
// A range being read again behind the page is not let go under it: the
// read would put back a range the memo no longer has.
func TestIdleStatsWaitForABusyRead(t *testing.T) {
forgetStats()
t.Cleanup(forgetStats)
statsFor(0)
statsMemo.Lock()
statsMemo.m[0].busy = true
statsMemo.Unlock()
t.Cleanup(func() {
statsMemo.Lock()
if k := statsMemo.m[0]; k != nil {
k.busy = false
}
statsMemo.Unlock()
})
forgetIdleStats()
statsMemo.Lock()
kept := statsMemo.m[0] != nil
statsMemo.Unlock()
if !kept {
t.Fatal("a range being read again was let go")
}
}