Files
magpie/internal/usage/speed_rank.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

168 lines
4.5 KiB
Go

package usage
import (
"math"
"slices"
"strings"
"unique"
)
// MinRankTokens is the fewest output tokens a request has to write to count
// in the model speed ranking: a short reply is nearly all wait, and its
// speed and first token say little about the model. The ranking's tooltip
// says the same number.
const MinRankTokens = 100
// ModelSpeed is one model's place in the speed ranking: the median of its
// requests' output tokens a second after the first, and of the wait for the
// first token, over the requests that count. Median, so one stalled or one
// burst reply doesn't move it. Both are read off a histogram of 2% bins, so
// a median is within 1% of the exact one.
type ModelSpeed struct {
Model string `json:"model"`
// Requests are the requests that counted: answered, streamed and timed,
// of MinRankTokens or more
Requests int `json:"requests"`
// Speed is the median tokens a second (Row.Decode's), over SpeedN
// requests whose speed was measured; 0 for none
Speed float64 `json:"tps,omitempty"`
SpeedN int `json:"tps_n,omitempty"`
// TTFT is the median ms to the first token, over Requests
TTFT int64 `json:"ttft_ms,omitempty"`
}
// speedHist counts values by the 2% bin they fall in, so a chunk of
// hundreds of thousands of requests is a few hundred numbers, and a page
// adds the chunks' up instead of sorting every request's.
type speedHist map[uint16]uint32
const (
histStep = 1.02
// histZero is the bin of 1: values from ~0.0003 to ~10^8 have one
histZero = 400
)
var histLog = math.Log(histStep)
func histBin(v float64) uint16 {
b := math.Floor(math.Log(v)/histLog) + histZero
return uint16(min(max(b, 0), math.MaxUint16))
}
// histValue is the middle of bin b.
func histValue(b uint16) float64 {
return math.Exp((float64(b) - histZero + 0.5) * histLog)
}
func (h speedHist) total() (n int) {
for _, c := range h {
n += int(c)
}
return n
}
// median is the middle of the values counted, the mean of the two middle
// ones for an even count; 0 for none.
func (h speedHist) median() float64 {
n := h.total()
if n == 0 {
return 0
}
bins := make([]uint16, 0, len(h))
for b := range h {
bins = append(bins, b)
}
slices.Sort(bins)
lo, hi := (n-1)/2, n/2
var got, seen int
var sum float64
for _, b := range bins {
next := seen + int(h[b])
if lo >= seen && lo < next {
sum += histValue(b)
got++
}
if hi >= seen && hi < next {
sum += histValue(b)
got++
}
seen = next
}
return sum / float64(got)
}
// modelHist is what one model's counted requests measured.
type modelHist struct {
Requests int `json:"n"`
Speed speedHist `json:"s,omitempty"`
TTFT speedHist `json:"t,omitempty"`
}
// speedRank collects the histograms of every model; a chunk keeps its own
// (chunkPart.speeds) and a page merges those of the chunks it reads.
type speedRank map[string]*modelHist
func (s speedRank) of(model string) *modelHist {
m := s[model]
if m == nil {
m = &modelHist{Speed: speedHist{}, TTFT: speedHist{}}
// unique's copy, so a chunk's part holds none of the chunk's strings
s[unique.Make(model).Value()] = m
}
return m
}
// add counts r when it is fair to rank by: not rejected, not a try handed on
// to another, not failed or canceled, its first token timed, and it wrote
// MinRankTokens or more.
func (s speedRank) add(r *Row) {
if r.IsRejected() || r.HandedOn || r.Failed() || r.Canceled() || r.TTFT <= 0 || r.Output < MinRankTokens {
return
}
m := s.of(r.Model)
m.Requests++
m.TTFT[histBin(float64(r.TTFT))]++
if n, w := r.Decode(); w > 0 && n >= MinRankTokens {
m.Speed[histBin(float64(n)*1000/float64(w))]++
}
}
// merge adds o's counts to s's.
func (s speedRank) merge(o speedRank) {
for model, x := range o {
m := s.of(model)
m.Requests += x.Requests
for b, c := range x.Speed {
m.Speed[b] += c
}
for b, c := range x.TTFT {
m.TTFT[b] += c
}
}
}
// result is the ranking, the fastest first; models whose speed wasn't
// measured follow, the one with more requests first.
func (s speedRank) result() []ModelSpeed {
out := make([]ModelSpeed, 0, len(s))
for model, m := range s {
out = append(out, ModelSpeed{
Model: model, Requests: m.Requests, SpeedN: m.Speed.total(),
TTFT: int64(m.TTFT.median() + 0.5), Speed: m.Speed.median(),
})
}
slices.SortFunc(out, func(a, b ModelSpeed) int {
switch {
case a.Speed != b.Speed:
if a.Speed > b.Speed {
return -1
}
return 1
case a.Requests != b.Requests:
return b.Requests - a.Requests
}
return strings.Compare(a.Model, b.Model)
})
return out
}