A first start, with no sums on disk, paired the whole history a few
days at a time (coldSpans) for the warm-up's page of All (pageCold),
then again for Codex's effort of the same period (effortCold), or the
other way round when the Sessions overview asked first. Both read the
same blocks and name the settled days by the same heatSettledKey, so
whichever runs first now counts both from its one pairing and writes
both files: pageCold counts the effort when its filter is empty and
effort-v1.json lacks it, effortCold counts the cells of the period's
unfiltered page when page-v1.json lacks them.
Run on mote (M5 Pro), at this tree (rebased on 7e0fe83d4):
- go test -count=1 ./internal/usage/: ok (25.5s).
- TestFirstBuildPairsOnceForPageAndEffort asks the page then the
effort, and the effort then the page, each after a cold restart:
the second pairs no span, and both answers equal a build alone.
Broken by hand: pageCold given no effort key, red ("page first:
effort … after N spans"); effortCold counting no cells, red
("effort first: page …").
- Real cold start on the 11,925-file fixture (8.5GB .zst + 17GB
jsonl), `magpie web`, two runs each, alternated:
before (v0.1.2368's tree) CPU 3:11.45 / 3:04.84, footprint peak
332 / 335 MB; after CPU 2:54.02 / 2:57.12, peak 330 / 325 MB. The
heatmap, requests, overview (0/7/30) and stats (0/30) answers are
byte for byte the same. Load on mote 22-28 from other sessions.
Not changed: the footprint's peak barely moves; the two builds ran one
after the other already (coldTurns), so this saves their CPU, not a
peak.
189 lines
7.3 KiB
Go
189 lines
7.3 KiB
Go
package usage
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"reflect"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/yetone/magpie/internal/sessions"
|
|
)
|
|
|
|
// pairAtOnce has a first build pair every chunk at once, for the tests
|
|
// of what a pairing of all of them is told apart from.
|
|
func pairAtOnce(t *testing.T) {
|
|
span := coldSpan
|
|
coldSpan = 0
|
|
t.Cleanup(func() { coldSpan = span })
|
|
}
|
|
|
|
// A first start pairs the history a few weeks at a time, not at once
|
|
// (#2278), and builds the page, the heatmap and Codex's effort a pairing
|
|
// of every chunk does: a call whose gateway row is after a span's end is
|
|
// paired, and a span pairs only the chunks it reaches.
|
|
func TestFirstBuildPairsSpans(t *testing.T) {
|
|
yesterday, _ := advanceHistory(t)
|
|
// a call just before midnight, its gateway row just after
|
|
at := time.Date(2026, 9, 23, 23, 59, 30, 0, time.Local)
|
|
path := filepath.Join(sessions.CodexDir(), "sessions", at.Format("2006/01/02"), "rollout-"+at.Format("2006-01-02T15-04-05")+"-z.jsonl")
|
|
writeCodexCall(t, path, "z", "sz", `"vscode"`, "resp-z", "m", "high", at, 300, 30)
|
|
Append(Record{Time: at.Add(time.Minute), Agent: "codex", Provider: "openai", Model: "m", Input: 300, Output: 30, Status: 200, ResponseID: "resp-z", Session: "sz"})
|
|
// a Codex thread written over three weeks, one file: a call 20 days
|
|
// back and one 2 days back, each through magpie too
|
|
writeLongCodexThread(t, yesterday.AddDate(0, 0, -20), yesterday.AddDate(0, 0, -1))
|
|
// a call through magpie alone, 30 days back
|
|
Append(Record{Time: yesterday.AddDate(0, 0, -30), Agent: "claude-code", Provider: "anthropic", Model: "c", Input: 50, Output: 5, Status: 200})
|
|
restart := func() {
|
|
requestCache.Lock()
|
|
requestCache.parts, requestCache.pageOld, requestCache.heatParts, requestCache.heatOld, requestCache.heats, requestCache.pairs, requestCache.pages = nil, nil, nil, nil, nil, nil, map[pageKey]RequestPage{}
|
|
requestCache.Unlock()
|
|
codexEffort.Lock()
|
|
codexEffort.periods = map[Period]*effortEntry{}
|
|
codexEffort.Unlock()
|
|
codexEffortBuilding.Lock()
|
|
effortOlds = map[Period]*effortOld{}
|
|
codexEffortBuilding.Unlock()
|
|
for _, path := range []string{pageDiskPath(), heatDiskPath(), effortDiskPath()} {
|
|
os.Remove(path)
|
|
}
|
|
}
|
|
restart()
|
|
t.Cleanup(restart)
|
|
type built struct {
|
|
page RequestPage
|
|
heat Heatmap
|
|
effort map[string][]sessions.ModelEffort
|
|
written bool
|
|
}
|
|
build := func() built {
|
|
var b built
|
|
b.page = QueryPage(All, Filter{}, 0, 50)
|
|
b.heat = HeatmapOf(Filter{})
|
|
b.effort = CodexModelEffort(All)
|
|
_, page := os.Stat(pageDiskPath())
|
|
_, heat := os.Stat(heatDiskPath())
|
|
_, effort := os.Stat(effortDiskPath())
|
|
b.written = page == nil && heat == nil && effort == nil
|
|
return b
|
|
}
|
|
paired := []int{}
|
|
pageSplitting = func(n int) { paired = append(paired, n) }
|
|
whole := build()
|
|
pageSplitting = nil
|
|
if !whole.written || len(paired) == 0 {
|
|
t.Fatal("a pairing of every chunk wrote no sums")
|
|
}
|
|
all := paired[0]
|
|
for _, days := range []int{1, 4, 14} {
|
|
coldSpan = days
|
|
restart()
|
|
spans := []int{}
|
|
coldSpanning = func(n int) { spans = append(spans, n) }
|
|
got := build()
|
|
coldSpanning = nil
|
|
if !got.written {
|
|
t.Fatalf("%d days a span: no sums written", days)
|
|
}
|
|
if len(spans) < 2 {
|
|
t.Fatalf("%d days a span: %d spans", days, len(spans))
|
|
}
|
|
pairedBelow(t, "a span", spans, 0, all)
|
|
if !reflect.DeepEqual(got.page, whole.page) {
|
|
t.Fatalf("%d days a span: page %+v, paired at once %+v", days, got.page, whole.page)
|
|
}
|
|
if !reflect.DeepEqual(got.heat, whole.heat) {
|
|
t.Fatalf("%d days a span: heatmap %+v, paired at once %+v", days, got.heat, whole.heat)
|
|
}
|
|
if !reflect.DeepEqual(got.effort, whole.effort) {
|
|
t.Fatalf("%d days a span: effort %+v, paired at once %+v", days, got.effort, whole.effort)
|
|
}
|
|
}
|
|
if whole.page.Total != 11 {
|
|
t.Fatalf("page: %d rows", whole.page.Total)
|
|
}
|
|
}
|
|
|
|
// A first start pairs the history once for the warm-up's page and
|
|
// Codex's effort, whichever is asked first, and tells what each pairing
|
|
// of its own does (#2278).
|
|
func TestFirstBuildPairsOnceForPageAndEffort(t *testing.T) {
|
|
yesterday, _ := advanceHistory(t)
|
|
writeLongCodexThread(t, yesterday.AddDate(0, 0, -20), yesterday.AddDate(0, 0, -1))
|
|
coldSpan = 4
|
|
restart := func() {
|
|
requestCache.Lock()
|
|
requestCache.parts, requestCache.pageOld, requestCache.heatParts, requestCache.heatOld, requestCache.heats, requestCache.pairs, requestCache.pages = nil, nil, nil, nil, nil, nil, map[pageKey]RequestPage{}
|
|
requestCache.Unlock()
|
|
codexEffort.Lock()
|
|
codexEffort.periods = map[Period]*effortEntry{}
|
|
codexEffort.Unlock()
|
|
codexEffortBuilding.Lock()
|
|
effortOlds = map[Period]*effortOld{}
|
|
codexEffortBuilding.Unlock()
|
|
for _, path := range []string{pageDiskPath(), heatDiskPath(), effortDiskPath()} {
|
|
os.Remove(path)
|
|
}
|
|
}
|
|
restart()
|
|
t.Cleanup(restart)
|
|
spans := 0
|
|
coldSpanning = func(int) { spans++ }
|
|
t.Cleanup(func() { coldSpanning = nil })
|
|
page := QueryPage(All, Filter{}, 0, 50)
|
|
restart()
|
|
effort := CodexModelEffort(All)
|
|
restart()
|
|
spans = 0
|
|
if got := QueryPage(All, Filter{}, 0, 50); !reflect.DeepEqual(got, page) {
|
|
t.Fatalf("page first: %+v, alone %+v", got, page)
|
|
}
|
|
first := spans
|
|
if got := CodexModelEffort(All); !reflect.DeepEqual(got, effort) || spans != first || first == 0 {
|
|
t.Fatalf("page first: effort %+v after %d spans of %d, alone %+v", got, spans, first, effort)
|
|
}
|
|
restart()
|
|
spans = 0
|
|
if got := CodexModelEffort(All); !reflect.DeepEqual(got, effort) {
|
|
t.Fatalf("effort first: %+v, alone %+v", got, effort)
|
|
}
|
|
first = spans
|
|
if got := QueryPage(All, Filter{}, 0, 50); !reflect.DeepEqual(got, page) || spans != first || first == 0 {
|
|
t.Fatalf("effort first: page %+v after %d spans of %d, alone %+v", got, spans, first, page)
|
|
}
|
|
}
|
|
|
|
// writeLongCodexThread writes one Codex rollout with a call at each of
|
|
// ats, and the gateway's row of each.
|
|
func writeLongCodexThread(t *testing.T, ats ...time.Time) {
|
|
t.Helper()
|
|
path := filepath.Join(sessions.CodexDir(), "sessions", ats[0].Format("2006/01/02"), "rollout-"+ats[0].Format("2006-01-02T15-04-05")+"-long.jsonl")
|
|
lines := []any{map[string]any{"timestamp": ats[0].Add(-time.Minute).Format(time.RFC3339Nano), "type": "session_meta", "payload": map[string]any{"id": "long", "session_id": "slong", "source": "vscode"}}}
|
|
for i, at := range ats {
|
|
response := fmt.Sprintf("resp-long-%d", i)
|
|
lines = append(lines,
|
|
map[string]any{"timestamp": at.Add(-time.Second).Format(time.RFC3339Nano), "type": "turn_context", "payload": map[string]any{"turn_id": "turn-" + response, "model": "m", "effort": "low"}},
|
|
map[string]any{"timestamp": at.Format(time.RFC3339Nano), "type": "token_usage_record", "payload": map[string]any{
|
|
"response_id": response, "session_id": "slong", "thread_id": "long", "turn_id": "turn-" + response,
|
|
"usage": map[string]int{"input_tokens": 70 + i, "cached_input_tokens": 0, "cache_write_input_tokens": 0, "output_tokens": 7, "reasoning_output_tokens": 0, "total_tokens": 77 + i},
|
|
}})
|
|
Append(Record{Time: at.Add(time.Second), Agent: "codex", Provider: "openai", Model: "m", Input: 70 + i, Output: 7, Status: 200, ResponseID: response, Session: "slong"})
|
|
}
|
|
var data []byte
|
|
for _, line := range lines {
|
|
b, err := json.Marshal(line)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
data = append(append(data, b...), '\n')
|
|
}
|
|
if err := os.MkdirAll(filepath.Dir(path), 0700); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.WriteFile(path, data, 0600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|