songlairui on X asked for the gateway's bandwidth: many LLM calls, base64
images above all, weigh on the network. Each request's row already kept
its bytes up (UpBytes) and down (DownBytes), shown in its details and as
the period's "↑ · ↓" under the Requests card. What was missing was
bandwidth over time and right now.
- The Requests chart's metrics take Traffic: each hour's/day's bytes up and
down, stacked by model, provider or agent. Its axis is in bytes ("4 MB"),
its tooltip splits a column into "↑ up · ↓ down", and the ranking's
values are bytes with the same split under each. A period with none
says "No traffic counted in this period". A chart point's Part now
carries up_bytes/down_bytes, from rows (Part.add) and from the request
page's cells (Part.plus). The cells already kept Totals, so no cache
version bump is needed.
- While requests are in flight, the Requests card says how fast traffic
flows now: "↑ 1.0 MB/s · ↓ 40 KB/s", over the last ~3s, read from the
new GET /api/usage/traffic a second apart. That route reports the
gateway's running totals (Server.Traffic) and its requests in flight.
The totals are atomics added where a row's bytes are added (sentUp,
downCount.Read). So a stream counts as it arrives, nothing is buffered
or scanned, and the totals and rows count the same bytes once. Polling
runs only while Usage → Requests is shown and the window is visible.
The line goes when nothing is in flight and nothing flowed. When another
magpie serves the gateway, the route answers {} and the page shows no
rate.
A Codex turn on the Responses WebSocket (7c1dc198d) is in the totals as
its row is: what the socket was written. The HTTP body sentUp counted is
taken back when the transport chooses the socket (toSocket) and given back
if the turn falls to HTTP after all (toHTTP), so the totals equal the sum
of the rows either way (TestTrafficCountsWhatTheSocketSent, kept and
refused).
Not counted, as before: Claude bridge turns (no HTTP to the vendor). A
plugin's row counts the local hop to the plugin host. An upload is counted
when its try is sent, so a large body shows as a burst in the live rate.
docs/subsystems/gateway-sessions.md has a Bandwidth section.
Run (mote; first at this change on da8480326, then again on 16a2dfac9):
- go test -count=1 -run 'Traffic|Socket|UpBytes|Ledger|Series'
./internal/gateway ./internal/usage ./internal/gui: ok.
- Broken by hand, each red, then restored: the down tally in downCount,
the up tally in addUp, Part.plus's bytes, Part.add's bytes, the
toSocket call (kept socket: traffic 151020, rows 50469) and the toHTTP
call (refused: traffic 50360, rows 100551).
- gofmt -l clean; go vet -tags production ./..., GOOS=linux and
GOOS=windows go vet ./...: ok; darwin, linux and windows builds: ok.
- GUI (mote, this tree): i18n-keys, i18n-en-no-cjk, usage-traffic and
request-archive-usage, 54/54 in Chromium and WebKit (usage-traffic in
en, zh, zh-TW, ja and de); usage-traffic red with the rate line broken
by hand. "Traffic" is in i18n.js, not the Usage plugin's: Go says it
too, and i18n-keys was red with it in the plugin's file.
- internal/cli TestEveryRouteHasACommandOrIsNamed: ok, with GET
/api/usage/traffic named in notYet (the full run found it red).
- On 16a2dfac9 (the pushed base): gofmt -l internal clean; go test
-count=1 -run 'Traffic|Socket|UpBytes|Ledger|Series|
TestEveryRouteHasACommandOrIsNamed|Budget' ./internal/gateway
./internal/usage ./internal/gui ./internal/cli ok; i18n-keys and
usage-traffic 40/40.
- Not finished at the tag, still running on mote: go test ./... -timeout
45m and the whole GUI suite (under suite.lock), and the whole
internal/gui, internal/usage and internal/gateway packages with every
usage* GUI file. What they find is fixed in follow-ups.
53 lines
1.7 KiB
Go
53 lines
1.7 KiB
Go
package usage
|
|
|
|
import (
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
// A point of the chart tells each part's bytes up and down apart, as it
|
|
// does tokens, for Requests' Traffic chart to stack them (songlairui on
|
|
// X): from the rows (LedgerSeries, Part.add) and from the request page's
|
|
// cells (Part.plus) alike, and the point's own sums have them all.
|
|
func TestSeriesPartsKeepBytes(t *testing.T) {
|
|
pageHome(t)
|
|
now := holdClock(t, time.Date(2026, 9, 30, 12, 0, 0, 0, time.Local))
|
|
records := []Record{
|
|
{Time: now, Provider: "a", Agent: "codex", Model: "m", Input: 10, UpBytes: 1000, DownBytes: 300, Status: 200},
|
|
{Time: now, Provider: "a", Agent: "codex", Model: "m", Input: 10, UpBytes: 2000, DownBytes: 400, Status: 200},
|
|
{Time: now, Provider: "b", Agent: "claude", Model: "n", Input: 10, UpBytes: 50, DownBytes: 7, Status: 500},
|
|
}
|
|
gateway := &rowChunk{}
|
|
all := Ledgered{}
|
|
for i, r := range records {
|
|
row := Row{Record: r}
|
|
gateway.add(row, "", int64(i), false)
|
|
all.Rows = append(all.Rows, row)
|
|
}
|
|
for name, page := range map[string]RequestPage{
|
|
"compact": buildRequestPage(Today, Filter{}, 0, 100, gateway, nil),
|
|
"ledger": pageFromLedger(Today, Filter{}, 0, 100, all),
|
|
} {
|
|
t.Run(name, func(t *testing.T) {
|
|
var up, down int64
|
|
parts := map[string]Part{}
|
|
for _, pt := range page.Series {
|
|
up += pt.UpBytes
|
|
down += pt.DownBytes
|
|
for id, p := range pt.By["provider"] {
|
|
q := parts[id]
|
|
q.UpBytes += p.UpBytes
|
|
q.DownBytes += p.DownBytes
|
|
parts[id] = q
|
|
}
|
|
}
|
|
if up != 3050 || down != 707 {
|
|
t.Fatalf("the points' own sums: up %d, down %d", up, down)
|
|
}
|
|
if a, b := parts["a"], parts["b"]; a.UpBytes != 3000 || a.DownBytes != 700 || b.UpBytes != 50 || b.DownBytes != 7 {
|
|
t.Fatalf("the providers' parts: %+v", parts)
|
|
}
|
|
})
|
|
}
|
|
}
|