Files
magpie/internal/usage/pack_archive_test.go
T
yetone 16d20c0b3d usage: a packed block keeps its compressed bytes, not S2's worst-case buffer
s2.Encode makes its output MaxEncodedLen(len(src)) long, more than the
rows' own bytes, and pack kept the archive as a slice of it
(slices.Clip trims the capacity, not the allocation). Every cached
session block and every gateway log block (log_index.go) so held its
whole uncompressed size plus S2's slack, while Bytes counted only the
compressed length, so the cache budget undercounted it too. pack now
encodes into pooled scratch (the rows' bytes and S2's output) and
copies the archive out at its own length.

Measured on a sandbox HOME (a clone of a 23 GB Codex + Claude Code
history, ~380k calls), magpie serve --web, warm call caches, heap
profile from /v1/magpie/heap:
- at rest (2 min): s2.Encode's archives 17.2 MB -> 9.2 MB (slices.Clone)
- at the warm-up's peak (6 s): archives 45.3 MB -> 30.6 MB
- liveHeap at 30 s 96.1 MB -> 90.3 MB, footprint 213 MB -> 203 MB

TestPackedArchiveHoldsOnlyItsBytes packs 100 blocks of 2000 rows and
measures what stays reachable after a collection. On the old pack it
fails: "100 packed blocks hold 16035 KB for 3887 KB of archives"
(3 of 3 runs); with the change it passes, and blocks packed through
the same scratch read back their own rows.

At 8b48dc5d6 (this commit and the two beside it, on 5c557c793):
gofmt -l clean; GOOS=linux and GOOS=windows go vet -tags nogui ./...
clean; go test -race -tags nogui ./internal/usage/ ./internal/sessions/
ok; go test -tags nogui -timeout 45m ./... with pipefail: 68 packages
ok, no FAIL.
Warm start in a sandbox HOME, magpie serve --web (ps RSS / footprint /
heapSys at 30s): origin/main 650 / 213 / 649 MB, all three 606 / 208 /
598 MB. Allocated in the first 30s: 2557 MB before, 1878 MB after.
2026-10-21 09:41:29 +08:00

63 lines
2.2 KiB
Go

package usage
import (
"fmt"
"runtime"
"testing"
"time"
)
// A packed block keeps its compressed bytes and no more. s2.Encode's
// output is as large as the worst case, more than the rows' own bytes,
// and an archive cut from it (even clipped) held that whole allocation:
// several times the compressed bytes Bytes counts, for as long as the
// block is cached.
func TestPackedArchiveHoldsOnlyItsBytes(t *testing.T) {
now := time.Now()
block := func(k, n int) *rowChunk {
c := &rowChunk{}
for i := range n {
r := Row{Record: Record{Time: now.Add(time.Duration(i) * time.Second), Agent: "claude", Provider: "anthropic", Model: "claude-opus-5-5", Input: 1200 + i%7, Output: 300, Session: fmt.Sprint("s", k, "-", i/50)}}
c.add(r, fmt.Sprint("msg", i), int64(i), false)
}
return c
}
var base, after runtime.MemStats
runtime.GC()
runtime.ReadMemStats(&base)
packed := make([]*rowChunk, 100)
archives := 0
for k := range packed {
packed[k] = block(k, 2000).pack()
if len(packed[k].Archive) == 0 {
t.Fatalf("block %d: not packed", k)
}
archives += len(packed[k].Archive)
}
runtime.GC()
runtime.ReadMemStats(&after)
// the unpacked rows went with the collection: what stays is the
// archives, their chunks' headers and the scratch the pool keeps
if held := int64(after.HeapAlloc) - int64(base.HeapAlloc); held > int64(archives)*3/2+1<<20 {
t.Fatalf("100 packed blocks hold %d KB for %d KB of archives", held>>10, archives>>10)
}
for k, c := range packed {
back := c.unpack()
if back == nil || len(back.Rows) != 2000 || back.row(1999).Input != 1200+1999%7 || back.row(1999).Session != fmt.Sprint("s", k, "-", 1999/50) {
t.Fatalf("block %d: not read back", k)
}
}
// blocks packed one after another through the same scratch keep their
// own bytes
a, b := &rowChunk{}, &rowChunk{}
for i := range 100 {
a.add(Row{Record: Record{Time: now, Agent: "a", Model: fmt.Sprint("ma", i)}}, "", int64(i), false)
b.add(Row{Record: Record{Time: now, Agent: "b", Model: fmt.Sprint("mb", i)}}, "", int64(i), false)
}
pa := a.pack()
pb := b.pack()
if ua, ub := pa.unpack(), pb.unpack(); ua.row(99).Model != "ma99" || ub.row(99).Model != "mb99" {
t.Fatalf("read back %q and %q", ua.row(99).Model, ub.row(99).Model)
}
}