Files
magpie/internal/usage/totals_plus_test.go
T
yetone 66b6d6ade9 usage: the Requests page sums each row once, not once per group
Every Requests page build (each 5s refresh of the newest page, each page
turn, each filter) walks the whole period. Over all of it each row went
into about 17 sums: the page's own, a source cell, five chart series
groups, five dimension groups, the caller key and account groups, the
computers' share, and the chart point with its five parts. Each one
copied the 896-byte Row and worked out Decode and Floor again, and
each dimension rebuilt f.Without(d) and ran keepsRow on another copy.

Now each row's sums are made once (Totals.addRow into d) and added to
each group (Totals.plus, Part.plus). The filters with a dimension, or
the computer and day, cleared are built once. A filter that clearing
leaves unchanged reuses the row's own keep, and with no pick that is
every dimension. keepsRow, keepsRecord, computer, Row.key and keyer.key
take pointers.

Measured on the real sandbox data (sbmem, 416,665 rows), with zzbench:
one QueryPage of period=all, then six rebuilds at new offsets. Process
CPU at load ~110-127, three runs each, alternated with the old binary:
  before (7d67910f7): user 20.84 / 20.39 / 20.61 s, sys 2.69-2.93 s
  after:              user 15.33 / 15.74 / 15.75 s, sys 2.80-2.97 s
In the profile, buildRequestBlocksOf's row closure went from 5.56 s to
1.59 s cumulative. Wall time per page is too noisy at this load to quote
(1.9-3.1 s both ways). What remains per rebuild is mostly evicted shards
being read again (prefetchSources -> loadCalls, ~1.7 s over the seven
builds) and the walk of the Codex folders.

Tests: TestTotalsPlusAddsEveryField fails when a Totals field is left
out of plus (broken by hand: dropping FloorN fails it).
TestPartPlusIsAdd compares Part.plus with Part.add over measured,
reasoning, failed, handed-on and retried rows. TestCompactPageMatchesLedger
already compares the page with the per-row ledger path over 13 filters.
Making every dimension group reuse keep fails 60 of its subtests.

Run at this head: go test -count=1 ./internal/usage ./internal/gui ok
(36.5s, 150.0s); go build -tags nogui ./... ok; go vet -tags nogui for
GOOS=linux and windows on internal/usage ok; gofmt -l clean. Not run:
the full suite (other sessions' suites hold the machine) and the gateway
package (it only calls usage's exported API, which is unchanged).
2026-10-25 03:31:55 +08:00

65 lines
2.1 KiB
Go

package usage
import (
"reflect"
"testing"
"time"
)
// TestTotalsPlusAddsEveryField fails when a field added to Totals is left
// out of plus: the Requests page's groups would read it as 0.
func TestTotalsPlusAddsEveryField(t *testing.T) {
var d Totals
v := reflect.ValueOf(&d).Elem()
for i := 0; i < v.NumField(); i++ {
switch f := v.Field(i); f.Kind() {
case reflect.Int, reflect.Int64:
f.SetInt(int64(i + 1))
case reflect.Float64:
f.SetFloat(float64(i) + 0.5)
default:
t.Fatalf("Totals.%s is a %s: plus and this test need it", v.Type().Field(i).Name, f.Kind())
}
}
var got Totals
got.plus(&d)
got.plus(&d)
w := reflect.ValueOf(&got).Elem()
for i := 0; i < v.NumField(); i++ {
name := v.Type().Field(i).Name
switch a, b := w.Field(i), v.Field(i); a.Kind() {
case reflect.Float64:
if a.Float() != 2*b.Float() {
t.Errorf("plus left out %s: %v of %v", name, a.Float(), 2*b.Float())
}
default:
if a.Int() != 2*b.Int() {
t.Errorf("plus left out %s: %d of %d", name, a.Int(), 2*b.Int())
}
}
}
}
// TestPartPlusIsAdd: a part counts a row from its sums as add counts it
// from the row, for each kind of row a chart has.
func TestPartPlusIsAdd(t *testing.T) {
credits := 2.5
rows := []Row{
{Record: Record{Time: time.Now(), Agent: "claude", Input: 10, Output: 1200, CacheRead: 3, CacheWrite: 4, Status: 200, TTFT: 50, FirstText: 60, Millis: 3000}, Source: "log", Priced: true, Cost: 0.25},
{Record: Record{Agent: "codex", Input: 7, Output: 500, Reasoning: 200, Status: 200, TTFT: 900, Millis: 4000}, Source: "log"},
{Record: Record{Agent: "opencode", Input: 5, Status: 429, Error: "limited", TTFT: 10, Millis: 20}},
{Record: Record{Agent: "opencode", Input: 5, Output: 9, HandedOn: true, Status: 500, Credits: &credits}},
{Record: Record{Agent: "claude", Input: 5, Output: 9, Tries: 2, Status: 200, TTFT: 30, FirstText: 31, Millis: 400}, Priced: true, Cost: 1e-7},
}
for i, r := range rows {
var want, got Part
want.add(r)
var d Totals
d.addRow(r)
got.plus(&d)
if got != want {
t.Errorf("row %d: plus %+v, add %+v", i, got, want)
}
}
}