Files
magpie/internal/gui/page_memory_test.go
T
yetone 94a70f1a0d gui: what a Sessions or Usage poll threw away goes back once the page rests
An open window's Sessions and Usage polls allocate ~330 MB every 15 s to
keep ~105 MB, and Go's scavenger hands the freed pages back only slowly,
never while the next collection's goal covers them: between polls the
footprint sat at 336-392 MB. The gateway already hands its requests'
pages back once it rests (#3472), but the window's reads go through the
GUI's handler, not the gateway's, so nothing ever did that for them.

held now counts the /api/sessions and /api/usage reads (readingPage),
and once none has been in flight for 3 s (a poll's reads come within a
second of one another) it calls debug.FreeOSMemory, as the warm-up does
after its reads.

Measured on a copy of the owner's real history (rss-home, 3581/3582,
8 GUI-like poll cycles 15 s apart, footprint 10 s into each rest), at
c2c08850f vs this change:
- footprint at rest: 336-392 MB -> 164-193 MB
- footprint right after a poll: 336-392 MB -> 181-276 MB (358 on the
  first)
- process CPU over the 8 cycles: 6.55 s -> 6.27 s
- /api/sessions mean 256 -> 305 ms, /api/usage/requests 232 -> 154 ms
  (one run each, the live history changing under them)

Tests: TestPageReadsReleasedAtRest (no release while a read is in
flight, one when the last ends, a cut-short rest waited out again) and
TestHeldCountsPageReads (sessions and usage counted, /api/state not).
Broken by hand: without the in-flight check the first fails ("released
while a read was in flight"); without readingPage in held the second
fails for both paths.

Run at this head (rebased on 8fba65d0b): go test ./internal/gui/ ok (94s); the two new tests
-count=20 ok; gofmt clean; GOOS=linux and GOOS=windows go vet -tags
nogui ./internal/gui/ ok.
Not run: the GUI JS suite (no script changed), the full ./... suite.
2026-10-27 08:29:11 +08:00

104 lines
2.4 KiB
Go

package gui
import (
"net/http"
"net/http/httptest"
"testing"
"time"
)
// The pages a poll's reads left go back once no read is in flight: not
// while one is, once when the last ends, and a rest a new read cut short
// is waited out again.
func TestPageReadsReleasedAtRest(t *testing.T) {
released := make(chan struct{}, 4)
pageReads.Lock()
pageReads.rest, pageReads.release = 20*time.Millisecond, func() { released <- struct{}{} }
pageReads.Unlock()
t.Cleanup(func() {
pageReads.Lock()
if pageReads.due != nil {
pageReads.due.Stop()
pageReads.due = nil
}
pageReads.rest, pageReads.release = 0, nil
pageReads.Unlock()
})
a, b := readingPage(), readingPage()
a()
// one is still in flight
releaseIfPagesRest()
select {
case <-released:
t.Fatal("released while a read was in flight")
default:
}
b()
b() // done twice ends it once
select {
case <-released:
case <-time.After(10 * time.Second):
t.Fatal("not released once every read ended")
}
// a rest not long enough yet: the release waits out the rest of it
stopDue := func() {
if pageReads.due != nil {
pageReads.due.Stop()
pageReads.due = nil
}
}
pageReads.Lock()
pageReads.rest = time.Hour
pageReads.Unlock()
readingPage()()
pageReads.Lock()
stopDue()
pageReads.Unlock()
releaseIfPagesRest()
pageReads.Lock()
waiting := pageReads.due != nil
stopDue()
pageReads.last = time.Time{}
pageReads.Unlock()
if !waiting {
t.Error("a rest cut short isn't waited out again")
}
select {
case <-released:
t.Fatal("released before the reads rested")
default:
}
releaseIfPagesRest()
select {
case <-released:
default:
t.Fatal("not released once the reads rested")
}
}
// held counts the Sessions and Usage pages' reads, and no other request.
func TestHeldCountsPageReads(t *testing.T) {
in := -1
h := held(http.HandlerFunc(func(http.ResponseWriter, *http.Request) {
pageReads.Lock()
in = pageReads.n
pageReads.Unlock()
}))
t.Cleanup(func() {
pageReads.Lock()
if pageReads.due != nil {
pageReads.due.Stop()
pageReads.due = nil
}
pageReads.Unlock()
})
for path, want := range map[string]int{"/api/sessions": 1, "/api/usage/requests": 1, "/api/state": 0} {
h.ServeHTTP(httptest.NewRecorder(), httptest.NewRequest(http.MethodGet, path, nil))
if in != want {
t.Errorf("%s: %d page reads in flight while it was answered, want %d", path, in, want)
}
}
}