A page holds every row of its period at once, and at a cold start over
the full history the rows held were most of what the footprint's peak
held: on mote's copy of the real history (52G Codex, 13G Claude Code),
the inuse heap 20s into a polled start had 48.5 MB of decoded session
rows (unpackReusing) and 42.9 MB of priced gateway rows (pricedRows),
both packedRow.
Millis, TTFT, FirstText, FirstReasoning, Sent, Flow, Own, Retried and
Queued are ms (Record's *_ms) and go in an int32, as Masking already
did. One past 24 days, which no call takes, is kept as the longest an
int32 holds (msOf), not wrapped. The snapshot's encoding is unchanged:
each is written as the int64 it was and read back through msOf.
unsafe.Sizeof(packedRow{}) goes from 216 to 184, 15% of every row held:
~14 MB of the 91 MB above.
Run at this head (rebased on 46aa7e6b8, which touches providers and
the GUI only), on mote:
- go build ./... ok
- go test -count=1 ./internal/usage ./internal/usageapi ok (and before
the rebase, on eadeb89c4)
- GOOS=linux, windows, darwin go vet -tags nogui ./internal/usage ok;
gofmt -l clean
- TestRowsOfOneSessionShareAShape now wants 184 bytes at most.
Broken by hand: msOf as a plain int32(ms) turns the new
TestRowTimesPastWhatARowKeeps red ("read back Millis -1702967296").
Not measured: a whole-app footprint A/B. Two rounds of peakheap.sh ran
at mote load ~220, with most polls timing out (3 to 12 of 80 answered)
and each run higher than the last whatever the binary (202, 248, 270,
318 MB), so they say nothing either way.
69 lines
2.6 KiB
Go
69 lines
2.6 KiB
Go
package usage
|
|
|
|
import (
|
|
"fmt"
|
|
"math"
|
|
"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 > 184 {
|
|
t.Fatalf("a packed row is %d bytes, want 184 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, Own: int64(3 * i), Masking: int64(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)
|
|
}
|
|
}
|
|
}
|
|
|
|
// A row keeps its times in ms in 32 bits: one longer than that, which no
|
|
// call takes, reads back as the longest it can keep, not wrapped around.
|
|
func TestRowTimesPastWhatARowKeeps(t *testing.T) {
|
|
long := int64(30 * 24 * time.Hour / time.Millisecond)
|
|
c := &rowChunk{}
|
|
c.add(Row{Record: Record{Time: time.Date(2026, 10, 21, 12, 0, 0, 0, time.UTC), Millis: long, Queued: -long, TTFT: 1500}}, "", 0, false)
|
|
got := c.row(0)
|
|
if got.Millis != math.MaxInt32 || got.Queued != math.MinInt32 || got.TTFT != 1500 {
|
|
t.Fatalf("read back Millis %d, Queued %d, TTFT %d; want %d, %d, 1500", got.Millis, got.Queued, got.TTFT, int64(math.MaxInt32), int64(math.MinInt32))
|
|
}
|
|
}
|