Files
magpie/internal/gui/speeds_test.go
T
yetone 1198cff601 gui: a model picker's row says how fast its provider answered this week, fastest first under a search
axuhere on X: with several providers serving one model, the list and the
model picker should show each one's speed and sort by it. Each row of an
agent's model picker now carries a quiet tag from the usage ledger's last
seven days: tokens a second, or the first token's wait when only that was
timed (title: "first token in Xs, N tokens a second"). Under a search,
rows that match equally go fastest first by about how long a 200-token
reply takes, the untimed last. Without a search the rows keep catalog
order, grouped by provider. "Fastest first" for a routing group already
existed as Speed first (d9a8148d7); nothing added there.

- GET /api/model-speeds (internal/gui/speeds.go): usage.SummarizeTop(Week)
  models by provider/model, the catalog ref an option carries; a model
  whose calls went to more than one host is left out.
- app.js: readSpeeds (once a minute, on a model picker opening, re-filters
  keeping the cursor), speedRank, speedTag. Existing i18n keys only.
  app.js 1,304,233 under its 1,311,735 budget, no raise.
- menu-scroll's strict fixture answers /api/model-speeds.

Run, at this head (rebased on bb3565309) unless said:
- mote: go test -run 'Budget|TestModelSpeedsByRef' ./internal/gui ok;
  go vet ./internal/gui darwin ok, GOOS=windows ok.
- picker-speeds.test.cjs: Chromium 1000/440px x en, zh, zh-TW, ja, de 10/10
  on mote; WebKit 10/10 on the Linux box (wsl-run), because WebKit launch
  hangs on mote at present (many pw_run.sh from several sessions, 0 WebKit
  processes). i18n-keys 4/4.
- Broken by hand: speeds.go without TPS -> TestModelSpeedsByRef red;
  filter() without the speedRank tie-break -> picker-speeds red
  ("fastest first, the untimed last").
- 57 other GUI files that open a model picker, Chromium, at the pre-rebase
  tree, concurrency 4 on mote at load ~30-40: 185 pass, 180 fail. One was
  real (menu-scroll's "Unexpected API: /api/model-speeds", fixed here); the
  rest were 5000ms locator timeouts and 11 WebKit launch timeouts. A rerun
  of those files at concurrency 2 was still running at push.

Not tried: native Linux go vet (the WSL box lacks gtk4/webkitgtk dev
packages); the real app with a real ledger; the full GUI or Go suite.
2026-10-29 17:09:13 +08:00

56 lines
2.0 KiB
Go

package gui
import (
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
"time"
"github.com/yetone/magpie/internal/usage"
)
// /api/model-speeds says how fast each provider answered a model this
// week, keyed by the ref an agent's model picker gives the row (axuhere on
// X: one model from several providers, at very different speeds). A model
// with no timed reply, and one older than the week, say nothing.
func TestModelSpeedsByRef(t *testing.T) {
sandboxHome(t)
now := time.Now()
for _, r := range []usage.Record{
// 200 tokens written in 2s after a 400ms first token: 100 tok/s
{Time: now.Add(-time.Hour), Provider: "quick", Model: "glm-5", Output: 200, Millis: 2400, TTFT: 400, Status: 200},
{Time: now.Add(-time.Hour), Provider: "quick", Model: "glm-5", Output: 200, Millis: 2600, TTFT: 600, Status: 200},
// 200 tokens in 10s after 3s: 20 tok/s
{Time: now.Add(-time.Hour), Provider: "slow", Model: "glm-5", Output: 200, Millis: 13000, TTFT: 3000, Status: 200},
// not streamed: no first token, no speed
{Time: now.Add(-time.Hour), Provider: "plain", Model: "glm-5", Output: 200, Millis: 5000, Status: 200},
// before the week
{Time: now.AddDate(0, 0, -9), Provider: "old", Model: "glm-5", Output: 200, Millis: 2400, TTFT: 400, Status: 200},
} {
usage.Append(r)
}
mux := http.NewServeMux()
speedsAPI(mux)
w := httptest.NewRecorder()
mux.ServeHTTP(w, httptest.NewRequest("GET", "/api/model-speeds", nil))
if w.Code != 200 {
t.Fatalf("%d %s", w.Code, w.Body)
}
var got map[string]speedJSON
if err := json.Unmarshal(w.Body.Bytes(), &got); err != nil {
t.Fatal(err)
}
if q := got["quick/glm-5"]; q.TTFT != 500 || q.TPS < 99 || q.TPS > 101 || q.N != 2 {
t.Errorf("quick/glm-5 = %+v; want 500ms, 100 tok/s of 2", q)
}
if s := got["slow/glm-5"]; s.TTFT != 3000 || s.TPS < 19 || s.TPS > 21 {
t.Errorf("slow/glm-5 = %+v; want 3000ms, 20 tok/s", s)
}
for _, k := range []string{"plain/glm-5", "old/glm-5"} {
if s, ok := got[k]; ok {
t.Errorf("%s says %+v; want nothing", k, s)
}
}
}