Files
magpie/internal/usage/read_ahead_test.go
T
yetone 1f589e3f4f usage: read today's usage ahead at startup
CyberCaius (Discord, Windows): "今天 N 次调用" is slow on the first
panel open after a start. The first usage?period=today parses
usage.jsonl: 0.33s for 2MB, 0.43s for a 40MB sandbox log here, more
on a slower disk. startBackend now runs usage.ReadAhead() in the
background, which works out today's summary into the summary cache,
so the panel's first ask finds it.

Parallel parsing was not done: with the read ahead, a request 1s
after start answers in 1-2ms, so there is nothing left to win there.

Test: TestReadAheadLeavesTodayNothingToRead. After ReadAhead, a
SummarizeTopIn(Today) parses nothing and works nothing out. Red with
ReadAhead's body emptied ("parsed the log 1 more times and was
worked out 1 more").

Run at tree 53ae48f72 (on origin/main 3b5a2f612): go test -tags nogui
-timeout 45m ./internal/usage ./internal/gui ok (TestCoreScriptBudget
included, no budget raised; app.js 1,317,465 of 1,317,486). GUI files
panel*, tray*, i18n-keys, routing-sessions (27 files, chromium and
webkit): 336 pass, 0 fail. gofmt clean. Earlier, at 7571ebc93: full
GUI suite 1095 pass; the 4 reds are two tests red on main too (#3497,
removed by hand gives "changed height 630 !== 561".
2026-10-24 23:49:08 +08:00

35 lines
1.0 KiB
Go

package usage
import (
"testing"
"time"
)
// What magpie reads ahead as it starts leaves the tray panel's first ask,
// today's summary in this computer's zone as /api/usage?period=today asks
// for it, nothing to parse or work out: the cold parse of the whole log
// was what its numbers waited for on the first open (CyberCaius).
func TestReadAheadLeavesTodayNothingToRead(t *testing.T) {
pageHome(t)
holdClock(t, time.Date(2026, 9, 30, 12, 0, 0, 0, time.Local))
settleLogClock(t)
historyLog(t, 2000)
counts := func() (int, int) {
logIndex.Lock()
reads := logIndex.reads
logIndex.Unlock()
summaries.Lock()
defer summaries.Unlock()
return reads, summaries.worked
}
ReadAhead()
reads, worked := counts()
got := SummarizeTopIn(Today, 100, nil)
if r, w := counts(); r != reads || w != worked {
t.Fatalf("today's summary after the read ahead parsed the log %d more times and was worked out %d more", r-reads, w-worked)
}
if got.Totals.Calls != 1 {
t.Fatalf("today's calls: %+v", got.Totals)
}
}