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".
This commit is contained in:
yetone
2026-10-24 23:49:08 +08:00
parent 2a8a177466
commit 1f589e3f4f
3 changed files with 47 additions and 0 deletions
+4
View File
@@ -12,6 +12,7 @@ import (
"github.com/yetone/magpie/internal/gateway"
"github.com/yetone/magpie/internal/provider"
"github.com/yetone/magpie/internal/update"
"github.com/yetone/magpie/internal/usage"
)
// served is the gateway this process serves, nil while another magpie
@@ -40,6 +41,9 @@ func startBackend() (gw *gateway.Server) {
gateway.Window = true // the routing this process serves is shown on its page
gw = serveGateway()
go watchGateway(backendCtx, gatewayWatch)
// the usage log parsed now, not when the tray panel first asks for
// today's calls (CyberCaius)
go usage.ReadAhead()
// Codex's background app-server, restarted when it has the list from
// before a change and no codex session is on it (luci)
go agent.KeepCodexDaemonCurrent(backendCtx)
+34
View File
@@ -0,0 +1,34 @@
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)
}
}
+9
View File
@@ -38,6 +38,15 @@ var summaries struct {
func indexedSummary(p Period, now time.Time) Summary { return topSummary(p, now, -1) }
// ReadAhead parses the log into its index and works out today's summary in
// this computer's zone, for magpie to run as it starts: the first reader,
// the tray panel's line of today's calls, finds both done. Parsing the log
// cold took 0.33 s at 2 MB and 0.77 s at 40 MB on one core, and the
// panel's numbers waited for it on the first open after a start
// (CyberCaius on Discord). A reader that comes while it runs waits for the
// same parse, never a second.
func ReadAhead() { SummarizeTopIn(Today, summaryCacheSessions, nil) }
// SummarizeTop is Summarize with only the top n sessions by tokens (all of
// them for n < 0); SessionCount says how many the period has. It is what a page that
// shows a few of them or none asks for: a summary of more than