Files
magpie/internal/usage/reasoning_stream_speed_test.go
T
yetone 4d79bebfa7 usage: speed counts all output, reasoning and tool calls too, from the first output (Archivemylife on X)
Archivemylife on X: speed should be the upstream's usage over the local
time, with no split into reasoning, tools and answer, since reasoning and
tool calls are tokens too.

Speed is now every output token the vendor's usage counts (reasoning and
tool-call arguments included) over the time from the first output of any
kind to the end of the reply. The first output is the earlier of the first
content (TTFT) and the first sign of reasoning, firstToken.reasoningMs in
ttft.go, which works the same in every protocol:
- a Responses reasoning item's start;
- an Anthropic thinking or redacted_thinking block's start;
- sealed (hidden) thinking;
- reasoning deltas spread over 100 ms or more. Deltas that came at once
  are a buffered burst and tell none.

A summary is no sign (summaryPart: a Responses reasoning summary, a Gemini
thought, or a summary translated from another protocol via
firstToken.summarized), because it is written after some of the reasoning
it sums up (#1463). A reply that reasoned with no sign of when it began
counts only its answer, its output less the reasoning, from its first text,
and none with no text. Before this, a reply that reasoned always counted
only its answer, and a reply that wrote tool calls after reasoning told no
speed. Burst and held-content checks judge the answer's part.

The same definition holds everywhere:
- gateway: speed.go, trace.go, codex_backend.go;
- usage.DecodeOf, for the Usage cards, rows and `magpie usage`;
- the Usage page's ledDecode;
- the Routing page's decodeOf and its ranking (fast routing reads
  noteTrySpeed, which uses DecodeOf).

settledCountVersion goes to 8, so settled heat totals are counted again.
The rule text is in en, zh, zh-TW, ja and de. Docs: reference.md, and
gui-shell.md under "Streamed output speed".

Red on the old code (scratch worktree at 589e4ba6d):
- TestCodexPreambleThenReasoningTellsSpeed: "reasoning seen from 0 ms;
  want 3166".
- TestVisibleReasoningSpeed: 100/1000 and 0/0 where 1500/14000 was
  wanted.

Run at this head (rebased on dc507aefd), on mote:
- go test -tags nogui -count=1 -timeout 45m ./...
  - gateway ok (1132s), usage ok, routes ok.
  - Red, filed, and caused by mote's own gateway on 127.0.0.1:3425:
    internal/cli TestHealthcheckFindsTheRunningGatewayWithoutMagpieAddr,
    TestUpdateRestartsTheGatewayItFindsWithoutMagpieAddr and
    TestUpdateFindsAMagpieThatWroteNoRecord (#6223, #6471); internal/gui
    TestLaneLimitRoutes (#5150).
- go vet for gateway and usage on darwin, GOOS=linux -tags nogui and
  GOOS=windows: ok. gofmt -l: clean.
- GUI, 18 files in Chromium and WebKit (reasoning-speed, usage-speed-*,
  usage-ledger, routing and the i18n files): 242 of 270 pass. The rest are
  usage-ledger "newest live route needs no return-to-live button" (#6003)
  and usage-speed-all-models "asked for 7d" (#5834). Both are red the same
  way on origin/main dc507aefd, in a run of its own.

Rebased again on 70ad7cf30, which touches gateway.go and usage/page.js.
At that head, on mote: go vet for gateway and usage; go test -run
"Reasoning|Speed|Decode|Ultra|Uncounted" for gateway and usage, ok;
reasoning-speed, usage-reads-note, usage-speed-every-row and i18n-keys in
both engines, 66 of 66.

Not tried: a real account per vendor. The timing is taken from recorded
stream shapes in the tests: Responses, Anthropic, Chat and Gemini, native
and translated.
2026-11-18 21:30:25 +08:00

190 lines
7.6 KiB
Go

package usage
import (
"testing"
"time"
"github.com/yetone/magpie/internal/catalog"
"github.com/yetone/magpie/internal/provider"
)
func TestVisibleReasoningSpeed(t *testing.T) {
visible := Record{Time: time.Now(), Output: 1500, Reasoning: 1400, Millis: 15000, TTFT: 1000, FirstReasoning: 1000, Flow: 12000, Status: 200}
for _, c := range []struct {
name string
r Record
n int
w int64
}{
{"visible tool turn", visible, 1500, 14000},
// the answer after the reasoning: all of it counts, from the first
// reasoning (Archivemylife on X: reasoning is tokens too)
{"answer after reasoning", func() Record { r := visible; r.FirstText = 14000; r.Flow = 1000; return r }(), 1500, 14000},
{"reasoning before the first token", func() Record { r := visible; r.TTFT, r.FirstText, r.Flow = 3000, 14000, 1000; return r }(), 1500, 14000},
{"hidden and old records", func() Record { r := visible; r.FirstReasoning = 0; return r }(), 0, 0},
{"unknown flow", func() Record { r := visible; r.Flow = 0; return r }(), 1500, 14000},
// sealed reasoning whose start the stream showed (a reasoning item's)
{"sealed reasoning, its start seen", func() Record { r := visible; r.ReasoningHidden = true; return r }(), 1500, 14000},
{"held reasoning at quarter boundary", func() Record { r := visible; r.Flow = 3500; return r }(), 1500, 14000},
{"held reasoning just below quarter", func() Record { r := visible; r.Flow = 3499; return r }(), 0, 0},
{"held reasoning", func() Record { r := visible; r.Flow = 1000; return r }(), 0, 0},
{"invalid count", func() Record { r := visible; r.Reasoning = 1600; return r }(), 0, 0},
{"below decode minimum", Record{Output: 10, Reasoning: 5, Millis: 119, TTFT: 20, FirstReasoning: 20, Flow: 99}, 0, 0},
{"at decode minimum", Record{Output: 10, Reasoning: 5, Millis: 120, TTFT: 20, FirstReasoning: 20, Flow: 100}, 10, 100},
{"above max speed", Record{Output: 1001, Reasoning: 5, Millis: 120, TTFT: 20, FirstReasoning: 20, Flow: 100}, 0, 0},
{"at max speed", Record{Output: 1000, Reasoning: 5, Millis: 120, TTFT: 20, FirstReasoning: 20, Flow: 100}, 1000, 100},
{"redacted with no reasoning count", Record{Output: 120, Millis: 9000, TTFT: 1000, Flow: 7000, ReasoningHidden: true}, 0, 0},
{"late hidden think", Record{Output: 1500, Reasoning: 1400, Millis: 9000, TTFT: 8000, Flow: 900}, 0, 0},
} {
t.Run(c.name, func(t *testing.T) {
if n, w := c.r.Decode(); n != c.n || w != c.w {
t.Fatalf("speed counted %d tokens/%d ms; want %d/%d", n, w, c.n, c.w)
}
})
}
var chunk rowChunk
for i := 0; i < 80; i++ {
chunk.add(Row{Record: visible}, "", int64(i), false)
}
packed := chunk.pack()
if len(packed.Archive) == 0 {
t.Fatal("snapshot not compressed")
}
for _, c := range []*rowChunk{&chunk, packed.unpack()} {
row := c.row(0)
if n, w := row.Decode(); row.FirstReasoning != 1000 || n != 1500 || w != 14000 {
t.Fatalf("cache lost streamed reasoning: %+v", row)
}
}
sealed := visible
sealed.ReasoningHidden = true
var hiddenChunk rowChunk
for i := 0; i < 80; i++ {
hiddenChunk.add(Row{Record: sealed}, "", int64(i), false)
}
for _, c := range []*rowChunk{&hiddenChunk, hiddenChunk.pack().unpack()} {
row := c.row(0)
n, w := row.Decode()
if !row.ReasoningHidden || row.FirstReasoning != 1000 || n != 1500 || w != 14000 {
t.Fatalf("cache lost sealed reasoning: %+v, %d/%d", row, n, w)
}
}
var sum Totals
sum.addRow(Row{Record: visible})
hidden := visible
hidden.FirstReasoning = 0
sum.addRow(Row{Record: hidden})
if sum.DecodeOut != 1500 || sum.DecodeMs != 14000 {
t.Fatalf("hidden reasoning lifted aggregate: %+v", sum)
}
}
// A row the Requests page reads back has been priced, and pricing sets
// swapKnown on it: that bit is not ReasoningHidden's, or every priced reply
// with no answer text would lose its speed (#1463).
func TestPricedRowKeepsReasoningEvidence(t *testing.T) {
visible := Record{Time: time.Now(), Provider: "deepseek", Model: "deepseek-chat", Output: 1500, Reasoning: 1400, Millis: 15000, TTFT: 1000, FirstReasoning: 1000, Flow: 12000, Status: 200}
toolOnly := Record{Time: time.Now(), Provider: "deepseek", Model: "deepseek-chat", Output: 120, Millis: 9000, TTFT: 1000, Flow: 7000, Status: 200}
sealed := toolOnly
sealed.ReasoningHidden = true
for _, c := range []struct {
name string
r Record
hidden bool
n int
w int64
}{
{"visible reasoning", visible, false, 1500, 14000},
{"tool call without reasoning", toolOnly, false, 120, 8000},
{"hidden reasoning", sealed, true, 0, 0},
} {
t.Run(c.name, func(t *testing.T) {
var chunk rowChunk
for i := 0; i < 80; i++ {
chunk.add(Row{Record: c.r}, "", int64(i), false)
}
b := newBlockPricer(provider.Renames{}, nil, func(Record) *catalog.Price { return nil })
defer b.release()
for _, from := range []*rowChunk{&chunk, chunk.pack()} {
priced := b.block(from)
if priced.Rows[0].Flags&swapKnown == 0 {
t.Fatal("the row wasn't priced")
}
row := priced.row(0)
if n, w := row.Decode(); row.ReasoningHidden != c.hidden || n != c.n || w != c.w {
t.Fatalf("priced row: hidden %v, %d tokens/%d ms; want %v, %d/%d", row.ReasoningHidden, n, w, c.hidden, c.n, c.w)
}
}
})
}
}
// A row's Flags bits are each its own: a reported call's own cost isn't
// read back as hidden reasoning, nor hidden reasoning as an own cost
// (rowOwnCost and rowReasoningHidden both took 256 when #1463 and #1874
// landed apart).
func TestRowFlagsApart(t *testing.T) {
cost := 0.5
own := Record{Time: time.Now(), Source: SourceReported, OwnCost: &cost, Provider: "deepseek", Model: "deepseek-chat", Output: 1500, Reasoning: 1400, Millis: 15000, TTFT: 1000, FirstReasoning: 1000, Flow: 12000, Status: 200}
hidden := own
hidden.OwnCost, hidden.ReasoningHidden = nil, true
for _, c := range []struct {
name string
r Record
hidden bool
own bool
}{
{"own cost", own, false, true},
{"hidden reasoning", hidden, true, false},
} {
t.Run(c.name, func(t *testing.T) {
made := func() *rowChunk {
var chunk rowChunk
for i := 0; i < 80; i++ {
chunk.add(Row{Record: c.r, Source: SourceReported}, "", int64(i), false)
}
return &chunk
}
b := newBlockPricer(provider.Renames{}, nil, func(Record) *catalog.Price { return nil })
defer b.release()
raw := made()
for _, row := range []Row{raw.row(0), b.block(raw).row(0), b.block(made().pack()).row(0)} {
if row.ReasoningHidden != c.hidden || (row.OwnCost != nil) != c.own {
t.Fatalf("hidden %v, own cost %v; want %v, %v", row.ReasoningHidden, row.OwnCost, c.hidden, c.own)
}
}
})
}
bits := []uint16{1, 2, 4, 8, 16, 32, swapKnown, rowHandedOn, rowReasoningHidden, rowOwnCost, rowCompactionKnown, rowCompaction, rowCredits, rowCacheUnknown, rowSocket}
var all uint16
for _, b := range bits {
if all&b != 0 {
t.Fatalf("Flags bit %d is taken twice", b)
}
all |= b
}
}
// A row that went over the Responses WebSocket says so as it was kept,
// packed and read back (matrix0487), and one that didn't doesn't.
func TestRowKeepsSocket(t *testing.T) {
for _, ws := range []bool{true, false} {
r := Record{Time: time.Now(), Provider: "codex", Model: "gpt-5.5", Output: 10, Status: 200, UpBytes: 300, Socket: ws}
made := func() *rowChunk {
var chunk rowChunk
for i := 0; i < 80; i++ {
chunk.add(Row{Record: r}, "", int64(i), false)
}
return &chunk
}
b := newBlockPricer(provider.Renames{}, nil, func(Record) *catalog.Price { return nil })
raw := made()
for _, row := range []Row{raw.row(0), b.block(raw).row(0), b.block(made().pack()).row(0)} {
if row.Socket != ws || row.UpBytes != 300 {
t.Errorf("socket %v, %d up; want %v, 300", row.Socket, row.UpBytes, ws)
}
}
b.release()
}
}