Files
magpie/internal/usage/row_shape_test.go
T
yetone 4042aa4d1d usage: a row keeps the strings its chunk shares once, in Shapes
The usage page holds every row of the period at once, and a packed row
was 328 bytes, 140 of them 35 string ids. On the owner's real history
(364,013 calls in 6,445 chunks) only five of those vary call by call:
the error, the request id, the archive, the response id and the message
id. The other 30 (agent, provider, model, session, endpoint, client...)
repeat for most rows of a chunk: 364k rows have 7,300 distinct sets.

So a row keeps those five ids and a Shape, an index into its chunk's
Shapes table, which holds every other set once. packedRow is 216 bytes.
A snapshot (process-private, nothing on disk) writes the table once
after the strings and per row the shape and the five ids, so archives
shrink too. block() renames and re-prices per shape, not per row.

Same output: f5b015742 and this build served byte-identical bodies on
the real history for 14 endpoints, signed in: requests.csv of every row
(94,292,195 bytes); the requests page for all, offset 20000, month,
week, day, agent=codex, agent=claude, failed=1, q=opus and
provider=anthropic; the heatmap; usage for all and for month.

Footprint (phys_footprint, what Activity Monitor shows), cold start on a
copy of the real history, two runs each, alternating:
  f5b015742  peak 483 / 541 MB  end RSS 530 / 600 MB  CPU 1:44.7 / 1:47.7
  this       peak 465 / 463 MB  end RSS 511 / 509 MB  CPU 1:45.7 / 1:47.1
At rest both settle at 188-197 MB.

Tests: TestRowsOfOneSessionShareAShape fails when packedRow grows past
216 bytes (red at 328 with 112 bytes of padding put in by hand: "a
packed row is 328 bytes"), and checks that 200 rows of two models keep
2 shapes and read back equal after pack and unpack.
go test ./... -timeout 45m at this head: 65 packages ok; agent, gateway
and library failed only with "no space left on device" (the disk filled
mid-run) and, run again once there was room, are ok (agent 208s,
gateway 342s, library 115s).
GOOS=linux and GOOS=windows go vet -tags nogui ./... clean; gofmt clean.
2026-10-21 23:21:41 +08:00

56 lines
1.9 KiB
Go

package usage
import (
"fmt"
"reflect"
"testing"
"time"
"unsafe"
)
// A chunk's rows keep the strings they share once, in Shapes, and only
// their own ids in the row: the page holds every row of the period at once,
// and 140 of a row's 328 bytes were string ids most rows of a chunk repeat.
// Packed and read back, every row is the row it was.
func TestRowsOfOneSessionShareAShape(t *testing.T) {
if n := unsafe.Sizeof(packedRow{}); n > 216 {
t.Fatalf("a packed row is %d bytes, want 216 at most", n)
}
at := time.Date(2026, 10, 21, 12, 0, 0, 0, time.Local)
c := &rowChunk{}
var want []Row
for i := range 200 {
r := Row{Record: Record{Time: at.Add(time.Duration(i) * time.Second), Agent: "claude", Provider: "anthropic", Model: "claude-opus-5-5",
Session: "s1", Endpoint: "/v1/messages", Input: i, Output: 2 * i, Status: 200,
RequestID: fmt.Sprint("req", i), ResponseID: fmt.Sprint("resp", i), Archive: fmt.Sprint("a", i)}, Source: "log"}
if i%7 == 0 {
r.Error, r.Status = fmt.Sprint("overloaded ", i), 529
}
if i == 150 {
r.Model = "claude-sonnet-5-5"
}
c.add(r, fmt.Sprint("msg", i), int64(i), r.Error != "")
want = append(want, c.row(i))
}
if len(c.Shapes) != 2 {
t.Fatalf("200 rows of two models kept %d shapes, want 2", len(c.Shapes))
}
c.dict, c.shapeIDs = nil, nil
packed := c.pack()
if len(packed.Archive) == 0 || packed.Shapes != nil {
t.Fatalf("not packed: %d archive bytes, %d shapes", len(packed.Archive), len(packed.Shapes))
}
back := packed.unpack()
if back == nil || len(back.Rows) != len(want) || len(back.Shapes) != 2 {
t.Fatalf("read back: %+v", back)
}
for i, w := range want {
if got := back.row(i); !reflect.DeepEqual(got, w) {
t.Fatalf("row %d read back as\n%+v\nwant\n%+v", i, got, w)
}
if msg := back.Strings[back.text(&back.Rows[i], rowMsg)]; msg != fmt.Sprint("msg", i) {
t.Fatalf("row %d's message id read back as %q", i, msg)
}
}
}