Files
magpie/internal/usageapi/speeds_test.go
T
yetone 88c417392e usage: a sortable model speed ranking on the Requests tab (@flying_crp on X)
Usage's Requests tab gets a "Model speed" card: per model, the median output tokens/s, the median wait for the first token and the request count, over the selected range, sortable by every column (aria-sort header buttons, quiet table, no gradients). A request counts when it was answered (not rejected, handed on, failed or canceled), its first token was timed and it wrote 100 tokens or more (usage.MinRankTokens; the tooltip says so). Computed locally: each chunk keeps a 2% log-bin histogram per model beside its cells (speed_rank.go, chunkPart.speeds), a page merges the chunks' histograms, so there is no per-request sort and medians are within ~1%. The settled chunks' histograms are in the page file; its key salt is now page-v2, so the first load after upgrade recounts settled history once. /api/ledger sends speeds and speed_min.

Run (mote): gofmt clean; go vet darwin, linux nogui, windows for the touched packages and ./... earlier; go test internal/usage, internal/usageapi, internal/gui ok at this head; usage-model-speed.test.cjs 12 of 12 (chromium and webkit, en/zh/zh-TW/ja/de, 400px: order, sort by every column, aria-sort, focus kept, no horizontal overflow, ellipsis, hidden when empty); with i18n-keys and i18n-en-no-cjk 20 of 20; the whole GUI suite once on the branch before the rebase: every red is in a file unrelated to this change (account-arrange #6010, account-refresh-holds #5814, account-eye #5052, agent-switch-accounts #5925, bulk visibility and disconnect previews #5982 fixed in e430e2750, accounts-own-signin #6038), each red alone on origin/main 71a68639b or load-only.
Timing: 1M requests added to a chunk in ~185ms (TestModelSpeedRankAtTheLargestLedger).
Broken by hand: dropping the Failed/Canceled exclusion (gpt: 5 requests, 59.7 tok/s) and dropping the page's merge of chunk speeds (ranking empty) each turned the new tests red; restored.
Not tried: the real Linux app (WebKitGTK), a real ledger of hundreds of thousands of rows through the page-v2 file, a Windows run (win-run).
2026-11-11 10:12:23 +08:00

46 lines
1.7 KiB
Go

package usageapi
import (
"encoding/json"
"path/filepath"
"strings"
"testing"
"time"
"github.com/yetone/magpie/internal/usage"
)
// TestLedgerSendsTheModelSpeedRanking: the Requests tab's ranking reads each
// model's median speed and first token off the ledger page, with the least
// tokens a request wrote to count; an empty ledger sends a list, not null.
func TestLedgerSendsTheModelSpeedRanking(t *testing.T) {
h := t.TempDir()
t.Setenv("HOME", h)
t.Setenv("USERPROFILE", h)
t.Setenv("XDG_CONFIG_HOME", filepath.Join(h, ".config"))
empty, _ := json.Marshal(LedgerPage(usage.All, usage.Filter{}, 0, 100))
if !strings.Contains(string(empty), `"speeds":[]`) {
t.Fatalf("an empty ledger sends %s", empty)
}
now := time.Now()
for i := range 3 {
usage.Append(usage.Record{Time: now.Add(-time.Duration(i+1) * time.Minute), Agent: "codex", Provider: "openai", Model: "gpt-5.5", Status: 200, Input: 10, Output: 500, TTFT: 2000, Millis: 12000})
}
usage.Append(usage.Record{Time: now.Add(-10 * time.Minute), Agent: "codex", Provider: "openai", Model: "gpt-5.5", Status: 500, Error: "boom", Output: 500, TTFT: 100, Millis: 200})
l := LedgerPage(usage.All, usage.Filter{}, 0, 100)
if l.SpeedMin != usage.MinRankTokens || len(l.Speeds) != 1 {
t.Fatalf("speeds %+v, min %d", l.Speeds, l.SpeedMin)
}
s := l.Speeds[0]
// 500 tokens in the 10 s after the first token: 50 tok/s, within a bin
if s.Requests != 3 || s.Speed < 49 || s.Speed > 51 || s.TTFT < 1950 || s.TTFT > 2050 || s.Name == "" {
t.Fatalf("%+v", s)
}
b, _ := json.Marshal(l)
for _, k := range []string{`"speeds":[{`, `"tps":`, `"ttft_ms":`, `"requests":3`, `"speed_min":100`} {
if !strings.Contains(string(b), k) {
t.Fatalf("the page lacks %s", k)
}
}
}