Commit Graph
1 Commits
Author SHA1 Message Date
yetone 28c89afcef usage: a period's summary counts only the log's blocks since the last
Each call the gateway logged changed the log's version, and every
/api/usage ask for Week or Month counted the whole period again: on
the #2278 bench (a busy gateway, 4 calls a second, the GUI's polls every
5 s) topSummary was 11% of CPU, beside GC at ~30%.

A period's summary now keeps what it counted over the log's full blocks
(512 rows each, never written again once full) and counts only the
blocks since, then the last block. It counts afresh when anything a
count depends on changed: a block it counted was replaced, a rewrite,
prices or names (meta), cleared calls, the period's bounds or series, or
the keying (hosts, accounts, keyed providers) changed. In the last case
an account that newly answers for a provider splits that provider's
earlier calls too. Rows are counted in the same order as before, so
every float comes out bit-identical. The group maps are copy-on-write
over the kept prefix, and only what was touched is copied.

Run at this tree, 213a31b4d before this message (the parent is origin/main 7795f5573, no rebase):
- New TestSummaryCountedOnIsCountedAfresh. Over 3*512+100 rows, for
  Today, Week, Month and All, it DeepEquals the folded summary against
  summarizeFrom: after appends across a block edge, a new host, an
  account newly answering for openai, another block, and a log written
  over. It also checks that at least one summary per period was counted
  on from a prefix.
- Broken by hand twice. With the keying check skipped, red at "an
  account". With one block left uncounted, red at "first". Restored
  after each.
- On mote: go test ./internal/usage ./internal/usageapi ./internal/tui
  all ok (usage 17.2s); GOOS=linux/windows/darwin go vet -tags nogui
  ok; gofmt clean. Before the last comment-only edit: go test
  ./internal/gui is red only on TestMainWindowCorners/macOS_26_SDK,
  the known #3949 on mote's macOS 27.
- A/B on mote, v0.1.1963 against this, 240 s each, under the same
  polls and feed, with another session's GUI suite also running (load
  ~35):
  - topSummary: 3.24 s to 0.93 s of a 60 s CPU profile.
  - Polls, mean seconds:
    - /api/usage?period=7d: 0.16 to 0.13
    - /api/usage?period=30d: 0.27 to 0.14
    - all polls: 0.65 to 0.63
  - Steady RSS after 130 s: 892 to 920 MiB.
  - The cold-start peak at 30 s, before any calls were fed: 1000 to
    1219 MiB (ps); footprint peak 755 to 781 MB. The cold-start peak
    swings between runs; the RSS peak is the next work.

Not tried: rolling windows ("last:") and picked ranges are still
counted afresh on every ask, as before.

Refs #2278
2026-10-29 06:04:22 +08:00