Files
magpie/internal/usage/request_disk_test.go
T
yetone 5d2260d3e6 usage: the Requests page's chunks are kept on disk, so a start doesn't open every call shard again
A started magpie opened every session file's call shard (~15.6k on a busy
machine) and turned each call into a row again before its first Requests
page could show (#3493). requests-v1.bin (beside sessions.json) now keeps
each session file's chunk as the page packs it: an entry stands while its
file's size and mtime are the same, as a call shard does, and the whole
file while the binary that wrote it and the index's root and meta (prices,
names, providers) are the same. Each entry has a length and CRC-32; a bad
one ends the read and the entries before it stand.

- The file is read when 64 or more session files want reading (a start, a
  change of prices), and written behind the lock once 64 or more chunks
  were made since the last write; fewer are a few shard reads next start,
  not worth writing the whole file again. The idle trim does not write.
- A chunk of another computer's day is not kept; times keep their zone
  (Local, UTC, a tz-database name or a fixed offset).

Real data (sandbox HOME with the live ~/.codex/sessions and
~/.claude/projects symlinked read only, 15,603 sources, 420k rows, load
~90 to 130): a cold All page 3.6s; written in 76 to 168ms behind the lock,
44MB. A start from the file: fromDisk 50ms for 15,602 chunks, then the
blocks 100ms, against the shard reads before; the whole page 1.5s, most of
it now readRequestState (0.65 to 1.1s), which is next. Same total, cost
and input as the cold page.

Run at 500aa7096 (origin/main, fetched): go test -timeout 45m of usage
(14.7s), usageapi (6.5s) and gui (143.5s, before the idle-save removal,
which touches no GUI path) ok; TestRequestChunksFromDisk -race -count=3
ok. gofmt clean; GOOS=linux and GOOS=windows go vet -tags nogui
./internal/usage/ clean.

TestRequestChunksFromDisk (new): a start reads no session file, a file
written over to the same size is read again for its mtime, new prices read
every file, a file cut short stands for the entries before the cut, and
garbage reads every file; every page equals the ledger's. Red with the
header compare taken out, with the size/mtime check taken out, and with
the write skipped.
2026-10-25 05:54:24 +08:00

114 lines
4.0 KiB
Go

package usage
import (
"fmt"
"os"
"path/filepath"
"sync/atomic"
"testing"
"time"
"github.com/yetone/magpie/internal/catalog"
"github.com/yetone/magpie/internal/sessions"
)
// A started magpie reads the session files' chunks from the file the last
// one wrote, and reads again only the session files that changed since; a
// file of other prices, or a bad one, is read past. Every page is the one
// the ledger gives.
func TestRequestChunksFromDisk(t *testing.T) {
pageHome(t)
now := holdClock(t, time.Date(2026, 9, 30, 12, 0, 0, 0, time.Local))
least := requestDiskLeast
requestDiskLeast = 2
t.Cleanup(func() { requestDiskLeast = least })
dir := filepath.Join(sessions.CodexDir(), "sessions", "2026", "09", "30")
writeCodexIdentity(t, "main-a", "native-main", "vscode")
rollout := func(i int) string {
return filepath.Join(dir, fmt.Sprintf("rollout-2026-09-30T10-00-0%d-s%d.jsonl", i, i))
}
writeCodexCall(t, rollout(0), "main-a", "native-main", `"vscode"`, "resp-0", "m", "low", now.Add(-time.Minute), 60, 6)
for i := 1; i < 5; i++ {
at := now.Add(-time.Duration(i) * time.Hour)
if i%2 == 0 {
at = at.UTC() // a row's zone is kept as it was read
}
writeCodexCall(t, rollout(i), fmt.Sprintf("s%d", i), fmt.Sprintf("native-%d", i), `"cli"`, fmt.Sprintf("resp-%d", i), "m", "high", at, 10*i, i)
}
Append(Record{Time: now.Add(-time.Minute), Agent: "codex", Provider: "openai", Model: "m", Input: 60, Output: 6, Status: 200, ResponseID: "resp-0"})
forget := func() {
requestDiskSaves.Wait()
requestCache.Lock()
requestCache.chunks = map[string]*rowChunk{}
requestCache.pages, requestCache.parts, requestCache.pairs, requestCache.heats = nil, nil, nil, nil
requestCache.key = ""
requestCache.Unlock()
sessions.Reset()
}
var reads atomic.Int32
page := func() RequestPage {
t.Helper()
reads.Store(0)
got := queryPage(All, Filter{}, 0, 100, func(s sessions.CallSource) []sessions.Call {
reads.Add(1)
return sessions.ReadCallSource(s)
})
equalPage(t, got, pageFromLedger(All, Filter{}, 0, 100, LedgerOf(All, Filter{})))
return got
}
first := page()
// the identity's rollout is a session file too
all := reads.Load()
if first.Total != 5 || all != 6 {
t.Fatalf("first page: %d rows from %d reads, want 5 from 6", first.Total, all)
}
requestDiskSaves.Wait()
if _, err := os.Stat(requestDiskPath()); err != nil {
t.Fatalf("no file of the chunks: %v", err)
}
forget()
equalPage(t, page(), first)
if n := reads.Load(); n != 0 {
t.Fatalf("a start read %d session files, want 0", n)
}
// a file written over to the same size is read again for its mtime
writeCodexCall(t, rollout(1), "s1", "native-1", `"cli"`, "resp-1", "m", "high", now.Add(-time.Hour), 11, 1)
later := time.Now().Add(time.Minute)
if err := os.Chtimes(rollout(1), later, later); err != nil {
t.Fatal(err)
}
forget()
if p := page(); p.Sum.Input != first.Sum.Input+1 || reads.Load() != 1 {
t.Fatalf("a changed file: %d input from %d reads, want %d from 1", p.Sum.Input, reads.Load(), first.Sum.Input+1)
}
// other prices: the file is of the old ones, and every file is read
requestDiskSaves.Wait()
os.WriteFile(catalog.CachePath(), []byte(`{"openai":{"id":"openai","models":{"m":{"id":"m","cost":{"input":20,"output":80,"cache_read":5,"cache_write":25}}}}}`), 0600)
catalog.Reset()
forget()
if p := page(); reads.Load() != all || p.Sum.Cost == first.Sum.Cost {
t.Fatalf("new prices: %d reads, cost %v (was %v), want %d reads at the new prices", reads.Load(), p.Sum.Cost, first.Sum.Cost, all)
}
// a file cut short stands for the entries before the cut
requestDiskSaves.Wait()
b, err := os.ReadFile(requestDiskPath())
if err != nil {
t.Fatal(err)
}
os.WriteFile(requestDiskPath(), b[:len(b)-3], 0600)
forget()
if page(); reads.Load() == 0 || reads.Load() == all {
t.Fatalf("a cut file: %d reads, want the cut entry's and fewer than %d", reads.Load(), all)
}
os.WriteFile(requestDiskPath(), []byte("garbage"), 0600)
forget()
if page(); reads.Load() != all {
t.Fatalf("a bad file: %d reads, want %d", reads.Load(), all)
}
}