Files
magpie/internal/usage/heat_before_test.go
T
yetone 8974836679 usage: a chunk's rows before the heatmap's cut are counted once, not on every read (#2278)
heatBefore summed the recent chunks' rows before the cut for heatSplit,
pageSplit and effortSplit, unpacking every recent chunk whole each time.
A long-lived session file is recent for weeks (one of the owner's Claude
sessions holds 57k calls), so each recompute of the Usage page while an
agent works unpacked ~200k rows three times. A named chunk's sum is now
kept by its serial and the cut, and dropped after hintsKept reads that
didn't ask for it, as hintsOf keeps a chunk's hints.

Run on mote at this tree:
- go test ./internal/usage ./internal/usageapi: ok
- GOOS=linux|windows|darwin go vet -tags nogui ./internal/usage: ok
- TestHeatBeforeKeptBySerial: sums equal a count of every chunk, a named
  chunk is unpacked once per cut, an unnamed one every time.
  Broken by hand (a kept sum used whatever its cut): red; restored, green.
- A debug build on a copy of the real history logged each call: the
  first reads of a start (concurrent) unpacked 766 non-empty chunks
  (207k rows) each; the next read unpacked none of them.
- Cold start bench, 120s of polls: fp_peak 321MB vs 309MB before, 103 vs
  96 GCs: no change outside noise, since polls answered 304 recompute
  nothing. A bench appending a call every 3s was too noisy on mote
  (load ~20 from other work) to show a difference either way.

Not tried: the GUI suite (no GUI file changed).
2026-10-30 17:42:08 +08:00

58 lines
1.5 KiB
Go

package usage
import (
"testing"
"time"
)
// A named chunk's rows before the cut are counted once for a cut, not
// unpacked again for each read of the heatmap, the page and Codex's
// effort (#2278), and sum as counting every chunk does.
func TestHeatBeforeKeptBySerial(t *testing.T) {
now := time.Now().Truncate(time.Second)
cut := now.Add(-48 * time.Hour)
var set []*rowChunk
for k := range 5 {
c := &rowChunk{}
for i := range 40 {
r := Record{Time: now.Add(-time.Duration(k*20+i) * 2 * time.Hour), Agent: "claude", Provider: "relay", Input: 1 + i%5, Output: k}
c.add(Row{Record: r}, "", int64(i), false)
}
if k < 4 {
c.serial = nextChunkSerial()
}
set = append(set, c)
}
want := func(cut time.Time) heatSum {
var s heatSum
for _, c := range set {
for i := range c.Rows {
if p := &c.Rows[i]; p.Time.Before(cut) {
s.n++
s.at += p.Time.UnixNano()
for _, v := range p.Tokens {
s.tokens += v
}
}
}
}
return s
}
var opened int
heatBeforeOpening = func(*rowChunk) { opened++ }
t.Cleanup(func() { heatBeforeOpening = nil })
for i, step := range []struct {
cut time.Time
opened int
}{{cut, 5}, {cut, 1}, {cut, 1}, {cut.Add(-24 * time.Hour), 5}, {cut.Add(-24 * time.Hour), 1}} {
opened = 0
got := heatBefore(step.cut, [3][]*rowChunk{set[:2], set[2:4], set[4:]})
if w := want(step.cut); got != w || w.n == 0 {
t.Fatalf("read %d: %+v, want %+v", i, got, w)
}
if opened != step.opened {
t.Fatalf("read %d unpacked %d chunks, want %d", i, opened, step.opened)
}
}
}