Files
magpie/internal/usage/cut_advance_test.go
T
yetone f2eb56c6bb usage: a first start pairs the history a few weeks at a time, not at once (#2278)
With no sums on disk (a first install, or a version that counts rows
otherwise), the page's cells, the heatmap's days and Codex's effort
before the cut were built from one pairing of the whole history. Every
gateway block was priced, every session row's message id went into one
set, and every call into one match map. On the owner's history that
was ~212 MB of the warm-up's 341 MB in use and the cold start's peak.

coldSpans builds them as a day's start advances them (pageAdvance),
14 days at a time. A span pairs the chunks with a row from heatReach
before it to heatReach after. Each chunk's oldest row is kept by its
serial. A gateway block not priced yet is priced by a pricer of the
span alone, and its rows go back to the pool after the span. The
recent days are then paired as on any kept start.

The page's cells come out in another order (by span, then chunk), so
TestSettledCountedAsVersioned now sorts them before it hashes. The
sorted hash is the same for a pairing at once and in spans, so
settledCountVersion stays. Tests that count a pairing of every chunk
use pairAtOnce (coldSpan 0).

Run on mote at the pushed head (rebased on b62fa332f): gofmt -l
clean; go vet ./internal/usage for darwin, linux and windows ok;
go test -count=1 ./internal/usage ok (20.0s); go build . ok.
Broken by hand: TestFirstBuildPairsSpans goes red when a span pairs
only its own days (lo, hi := from, to): the effort differs.
Bench, real history (a year), cold first start, against v0.1.2256,
two rounds each, on mote at load ~22:
  no GUI polls:  footprint peak 451, 407 MB (2256: 493, 484);
                 live heap max 318, 289 MB (2256: 367, 362)
  GUI polls:     footprint peak 483, 410 MB (2256: 534, 532);
                 live heap max 316, 290 MB (2256: 395, 406)
The heatmap's answers are byte-equal to 2256's. The request page's
differ only in float digits from the summation order (232 floats,
largest relative difference 6.5e-14); every other value is equal.
Not tried: a start on Windows or Linux.
2026-10-31 03:13:28 +08:00

170 lines
5.5 KiB
Go

package usage
import (
"os"
"path/filepath"
"reflect"
"testing"
"time"
"github.com/yetone/magpie/internal/sessions"
)
// advanceHistory writes a Codex call, each with its gateway call, 368,
// 40, 20, 10, 5, 2 and 0 days before Sunday 2026-10-04 noon, and holds
// the clock there. The call two days back is after that day's cut and
// before the next day's; the one 368 days back is in that day's heatmap
// and not in the next day's, which begins a week later.
func advanceHistory(t *testing.T) (yesterday, today time.Time) {
pageHome(t)
pairAtOnce(t)
yesterday = holdClock(t, time.Date(2026, 10, 4, 12, 0, 0, 0, time.Local))
for i, days := range []int{368, 40, 20, 10, 5, 2, 0} {
id := string(rune('a' + i))
at := yesterday.AddDate(0, 0, -days).Add(-time.Hour)
path := filepath.Join(sessions.CodexDir(), "sessions", at.Format("2006/01/02"), "rollout-"+at.Format("2006-01-02T15-04-05")+"-"+id+".jsonl")
writeCodexCall(t, path, id, "s"+id, `"vscode"`, "resp-"+id, "m", "low", at, 100+i, 10)
Append(Record{Time: at.Add(time.Minute), Agent: "codex", Provider: "openai", Model: "m", Input: 100 + i, Output: 10, Status: 200, ResponseID: "resp-" + id, Session: "s" + id})
}
return yesterday, yesterday.AddDate(0, 0, 1)
}
// pairedBelow fails unless every pairing since from paired fewer than all.
func pairedBelow(t *testing.T, what string, paired []int, from, all int) {
t.Helper()
if len(paired) == from {
t.Fatalf("%s: nothing paired", what)
}
for _, n := range paired[from:] {
if n >= all {
t.Fatalf("%s: paired %d of %d chunks, %v", what, n, all, paired[from:])
}
}
}
// The first start of a day pairs only the last days' chunks for the
// heatmap: the days kept at the day before's cut stand, and the day
// between the two cuts is added (#2278). The days are a cold pairing's.
func TestHeatmapDaysAdvanceACut(t *testing.T) {
_, today := advanceHistory(t)
paired := []int{}
heatPairing = func(n int) { paired = append(paired, n) }
t.Cleanup(func() { heatPairing = nil })
restart := func() {
requestCache.Lock()
requestCache.heatParts, requestCache.heatOld, requestCache.heats, requestCache.pairs, requestCache.pages = nil, nil, nil, nil, map[pageKey]RequestPage{}
requestCache.Unlock()
}
restart()
t.Cleanup(restart)
HeatmapOf(Filter{})
all := paired[0]
holdClock(t, today)
restart()
from := len(paired)
warm := HeatmapOf(Filter{})
pairedBelow(t, "the next day", paired, from, all)
if e, _ := heatDiskEntryRead(); !e.Cut.Equal(time.Date(2026, 10, 3, 0, 0, 0, 0, time.Local)) {
t.Fatalf("the next day's days were written at cut %v", e.Cut)
}
restart()
from = len(paired)
if again := HeatmapOf(Filter{}); !reflect.DeepEqual(again, warm) {
t.Fatalf("started again: %+v, was %+v", again, warm)
}
pairedBelow(t, "started again", paired, from, all)
restart()
os.Remove(heatDiskPath())
cold := HeatmapOf(Filter{})
if !reflect.DeepEqual(warm, cold) {
t.Fatalf("the next day %+v, cold %+v", warm, cold)
}
if len(cold.Days) != 6 {
t.Fatalf("cold days %+v", cold.Days)
}
}
// The first start of a day pairs only the last days' chunks for a page
// (#2278), and builds the page a pairing of every chunk does.
func TestPageCellsAdvanceACut(t *testing.T) {
_, today := advanceHistory(t)
paired := []int{}
pageSplitting = func(n int) { paired = append(paired, n) }
t.Cleanup(func() { pageSplitting = nil })
restart := func() {
requestCache.Lock()
requestCache.parts, requestCache.pageOld, requestCache.pairs, requestCache.pages = nil, nil, nil, map[pageKey]RequestPage{}
requestCache.Unlock()
}
restart()
t.Cleanup(restart)
QueryPage(All, Filter{}, 0, 1)
all := paired[0]
holdClock(t, today)
restart()
from := len(paired)
warm := QueryPage(All, Filter{}, 0, 1)
pairedBelow(t, "the next day", paired, from, all)
restart()
from = len(paired)
if again := QueryPage(All, Filter{}, 0, 1); !reflect.DeepEqual(again, warm) {
t.Fatalf("started again: %+v, was %+v", again, warm)
}
pairedBelow(t, "started again", paired, from, all)
restart()
os.Remove(pageDiskPath())
cold := QueryPage(All, Filter{}, 0, 1)
if !reflect.DeepEqual(warm, cold) {
t.Fatalf("the next day %+v, cold %+v", warm, cold)
}
if cold.Total != 7 {
t.Fatalf("cold page: %d rows", cold.Total)
}
}
// The first start of a day pairs only the last days' chunks for Codex's
// effort (#2278), and tells the effort a pairing of every chunk does.
func TestCodexEffortAdvancesACut(t *testing.T) {
_, today := advanceHistory(t)
paired := []int{}
effortPairing = func(n int) { paired = append(paired, n) }
t.Cleanup(func() { effortPairing = nil })
restart := func() {
codexEffort.Lock()
codexEffort.periods = map[Period]*effortEntry{}
codexEffort.Unlock()
codexEffortBuilding.Lock()
effortOlds = map[Period]*effortOld{}
codexEffortBuilding.Unlock()
requestCache.Lock()
requestCache.pairs = nil
requestCache.Unlock()
}
restart()
t.Cleanup(restart)
CodexModelEffort(All)
all := paired[0]
holdClock(t, today)
restart()
from := len(paired)
warm := CodexModelEffort(All)
pairedBelow(t, "the next day", paired, from, all)
restart()
os.Remove(effortDiskPath())
cold := CodexModelEffort(All)
if len(cold) != 7 || len(warm) != len(cold) {
t.Fatalf("the next day %d threads, cold %d", len(warm), len(cold))
}
for id, c := range cold {
w := warm[id]
if len(w) != len(c) {
t.Fatalf("thread %s: the next day %+v, cold %+v", id, w, c)
}
for i := range c {
if w[i].Tokens != c[i].Tokens || w[i].Priced != c[i].Priced {
t.Fatalf("thread %s: the next day %+v, cold %+v", id, w, c)
}
}
}
}