Files
magpie/internal/usage/lent_parts_test.go
T
yetone e94fb18b8f usage: a page's cells and the heatmap's days read strings where they were decoded (#2278)
partsOf, cellsBefore and heatDaysOf read each chunk through a chunkRows
that copied the chunk's whole string section first. What they keep is
unique's handles (a cell), a date made by Format and numbers (a day), so
the copy was thrown away chunk by chunk. They now read through
lentRows(), as newest and the Codex readers already do.

Run on mote:
- go test ./internal/usage/ -count=1 -timeout 45m: ok (40s).
- -race -count=3 on TestPartsReadStringsWhereDecoded,
  TestRowBufferKeptPastCollections, TestRequestCells*, *Heat*, *PageSplit*: ok.
- TestPartsReadStringsWhereDecoded (new): 8 packed chunks, 1.1MB of
  strings. With the three sites put back to `var rows chunkRows` by sed
  it is red: partsOf 1,188,672 bytes, cellsBefore 1,197,568, heatDaysOf
  1,187,616. Green with the change; the cells' names and the days' counts
  are right after the buffer was decoded over again.
- Real history copy (66GB, ~12k session files), magpie web in a sandbox
  HOME, heap profile 40s after start, two rounds each:
  unpackReusing 183MB / 174MB before, 106MB / 113MB after;
  partsOf 76MB / 78MB before, 55MB / 56MB after.
  A warm poll of every page reads cached parts and is unchanged.

Not tried: the Windows and Linux boxes (no OS-specific code).
2026-10-31 20:48:05 +08:00

92 lines
2.9 KiB
Go

package usage
import (
"fmt"
"runtime"
"strings"
"testing"
"time"
)
// The usage page's cells and the heatmap's days keep unique's handles and
// numbers, never a row's strings, so they read a chunk's strings where it
// was decoded: a copy of each chunk's strings was ~56MB a poll on a real
// history, every chunk read again for the 7- and 30-day pages (#2278).
func TestPartsReadStringsWhereDecoded(t *testing.T) {
now := time.Date(2026, 10, 1, 13, 0, 0, 0, time.UTC)
var chunks []*rowChunk
section := 0
for k := range 8 {
c := &rowChunk{}
for i := range 2000 {
id := fmt.Sprintf("req-%d-%d-%s", k, i, strings.Repeat("x", 60))
section += len(id)
c.add(Row{Record: Record{Time: now.Add(-time.Duration(k*2000+i) * time.Second), Agent: "claude", Provider: "p", Model: fmt.Sprint("model-", k), Session: "s", Input: 1, Status: 200, RequestID: id}}, "", int64(i), false)
}
packed := c.pack()
if len(packed.Archive) == 0 {
t.Fatal("a chunk wasn't packed")
}
chunks = append(chunks, packed)
}
t.Cleanup(ReleaseScratch)
ps := pairRows(All, now, chunks, nil, nil, nil)
pass := newCellPass(All, Filter{}, now, true)
since := now.AddDate(0, 0, -1)
allocs := func(name string, fn func()) {
t.Helper()
fn() // the decode buffer, kept from here on
least := ^uint64(0)
for range 3 {
var a, b runtime.MemStats
runtime.ReadMemStats(&a)
fn()
runtime.ReadMemStats(&b)
least = min(least, b.TotalAlloc-a.TotalAlloc)
}
if least > uint64(section/4) {
t.Errorf("%s allocated %d bytes for %d bytes of strings", name, least, section)
}
}
allocs("partsOf", func() { ps.partsOf(pass, nil, nil) })
allocs("cellsBefore", func() { ps.cellsBefore(pass, now.Add(time.Second)) })
allocs("heatDaysOf", func() { ps.heatDaysOf(since, now, Filter{}, nil, nil, nil) })
// what they keep is right after every buffer was read over again
parts := ps.partsOf(pass, nil, nil)
before := ps.cellsBefore(pass, now.Add(time.Second))
days := ps.heatDaysOf(since, now, Filter{}, nil, nil, nil)
ps.partsOf(newCellPass(All, Filter{Model: "model-3"}, now, true), nil, nil)
models := map[string]int{}
for _, p := range parts {
for _, c := range p.cells {
models[c.model.Value()] += c.n
}
}
for k := range 8 {
if n := models[fmt.Sprint("model-", k)]; n != 2000 {
t.Fatalf("model-%d has %d calls in the parts: %v", k, n, models)
}
}
n := 0
for _, c := range before.cells {
if !strings.HasPrefix(c.model.Value(), "model-") || c.provider.Value() != "p" || c.agent.Value() != "claude" {
t.Fatalf("a cell before the cut reads %q %q %q", c.provider.Value(), c.agent.Value(), c.model.Value())
}
n += c.n
}
if n != 16000 {
t.Fatalf("%d calls before the cut", n)
}
calls := 0
for _, d := range days {
if d.Date != "2026-10-01" && d.Date != "2026-09-30" {
t.Fatalf("a day reads %q", d.Date)
}
calls += d.Calls
}
if calls != 16000 {
t.Fatalf("%d calls in the days", calls)
}
}