Files
magpie/internal/usage/codex_fold_ids_test.go
T
yetone c272f3799e usage: a session file's Codex ids are hashed once while it stands
A Usage page polled while one session was live paired every chunk of the
period again, and foldCodexRows decoded each session file to hash the
same response ids: ~40 MB a poll of today's page, more than any other
part of it. Each chunk's Codex log calls are now kept by its serial as
one uint64 each (the id's hash shifted over its index, 8 bytes a call),
for the four folds after the last that read them, so a poll decodes
only the chunks that changed. Calls whose hash is the same are still
told apart by their ids after, as before.

Run (worktree clip; tests and vet again at this head, rebased on b7d5a97fe):
- go test -count=1 ./internal/usage/ ./internal/usageapi/: ok
- TestFoldReadsAChunksIDsOnce: red with the cache turned off by hand
  (the second fold read both chunks again), green with it
- gofmt -l clean; GOOS=linux and GOOS=windows go vet -tags nogui
  ./internal/usage/: clean
- A/B, ab5.sh (warm start in a sandbox HOME with a copy of the real
  history, then 8 GUI-like polls 15 s apart), v0.1.1659 vs this:
  allocated over the polls 2.99 GB -> 2.39 GB; queryPage 726 -> 442 MB;
  foldCodexRows 326 MB -> under the profile's floor;
  /api/usage/requests?period=today mean 154 -> 100 ms;
  /api/sessions 305 -> 213 ms; footprint 10 s into each rest
  164-193 -> 156-187 MB.

Not done here: codexChunkResponsePairs, visibleLocal and Codex effort's
visit still decode every chunk a poll (~117, 85 and 91 MB over the run).
2026-10-27 08:46:33 +08:00

59 lines
2.0 KiB
Go

package usage
import (
"fmt"
"maps"
"testing"
"time"
)
// A chunk's Codex ids are read once while its serial stands: a poll that
// folds the same session files again reads none of them, and folds them
// the same. A chunk made again (a live session's) is read again.
func TestFoldReadsAChunksIDsOnce(t *testing.T) {
now := time.Date(2026, 10, 27, 12, 0, 0, 0, time.UTC)
chunk := func(ids ...string) *rowChunk {
c := &rowChunk{}
for i, id := range ids {
c.add(Row{Record: Record{Time: now.Add(-time.Duration(i) * time.Minute), Agent: "codex", Model: "native-model", Input: 80, Output: 8,
Session: "native-session", ResponseID: id}, Source: "log"}, "", int64(i), false)
}
for i := range 200 { // enough for the archive to be smaller than the rows
c.add(Row{Record: Record{Time: now.Add(-time.Hour), Agent: "claude", Model: "m", Session: "s", ResponseID: fmt.Sprint("other-", i)}}, "", int64(len(ids)+i), false)
}
packed := c.pack()
if len(packed.Archive) == 0 {
t.Fatal("the chunk wasn't packed")
}
packed.serial = nextChunkSerial()
return packed
}
a, b := chunk("resp-1", "resp-2"), chunk("resp-1", "resp-3")
var read []*rowChunk
foldHashing = func(c *rowChunk) { read = append(read, c) }
t.Cleanup(func() { foldHashing = nil })
first := foldCodexRows([]*rowChunk{a, b})
if len(read) != 2 || len(first) != 1 || !first[rowRef{b, 0}] {
t.Fatalf("first fold read %d chunks, skipped %v; want both read and b's resp-1 skipped", len(read), first)
}
read = nil
again := foldCodexRows([]*rowChunk{a, b})
if len(read) != 0 {
t.Fatalf("a fold of the same chunks read %d of them again", len(read))
}
if !maps.Equal(first, again) {
t.Fatalf("the same chunks folded %v, then %v", first, again)
}
read = nil
live := chunk("resp-1", "resp-4")
folded := foldCodexRows([]*rowChunk{a, live})
if len(read) != 1 || read[0] != live {
t.Fatalf("a chunk made again wasn't the one read: %d read", len(read))
}
if len(folded) != 1 || !folded[rowRef{live, 0}] {
t.Fatalf("the live chunk's copy of a's call = %v", folded)
}
}