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.
200 lines
7.0 KiB
Go
200 lines
7.0 KiB
Go
package gateway
|
|
|
|
import (
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/yetone/magpie/internal/provider"
|
|
"github.com/yetone/magpie/internal/usage"
|
|
)
|
|
|
|
// The gateway's running byte counts (Server.Traffic, which Usage's live
|
|
// rate reads) take what each request's row takes, a refused try's bytes
|
|
// too, whole or streamed, and nothing twice (songlairui on X: how much
|
|
// bandwidth the gateway is using).
|
|
func TestTrafficCountsWhatRowsCount(t *testing.T) {
|
|
for _, stream := range []bool{false, true} {
|
|
t.Run(map[bool]string{false: "whole", true: "stream"}[stream], func(t *testing.T) {
|
|
fresh(t)
|
|
overloaded := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
io.Copy(io.Discard, r.Body)
|
|
w.Header().Set("Content-Type", "application/json")
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
io.WriteString(w, `{"error":{"message":"overloaded"}}`)
|
|
}))
|
|
t.Cleanup(overloaded.Close)
|
|
ok := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
io.Copy(io.Discard, r.Body)
|
|
if stream {
|
|
w.Header().Set("Content-Type", "text/event-stream")
|
|
io.WriteString(w, sse(`data: {"id":"c1","choices":[{"index":0,"delta":{"role":"assistant","content":"hello"}}]}`, `data: {"id":"c1","choices":[{"index":0,"delta":{},"finish_reason":"stop"}],"usage":{"prompt_tokens":3,"completion_tokens":1}}`, `data: [DONE]`))
|
|
return
|
|
}
|
|
w.Header().Set("Content-Type", "application/json")
|
|
io.WriteString(w, chatOK)
|
|
}))
|
|
t.Cleanup(ok.Close)
|
|
for _, p := range []provider.Provider{
|
|
{ID: "a", Name: "A", Key: "k", Models: []string{"m"}, Chat: overloaded.URL + "/v1"},
|
|
{ID: "b", Name: "B", Key: "k", Models: []string{"m"}, Chat: ok.URL + "/v1"},
|
|
} {
|
|
if err := provider.Save(p); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
if err := provider.SaveGroup(provider.Group{Name: "G", Members: []string{"a/m", "b/m"}, Routing: provider.Ordered}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
s := New()
|
|
if got := s.Traffic(); got != (Traffic{}) {
|
|
t.Fatalf("a new gateway has counted %+v", got)
|
|
}
|
|
code, body := postAs(t, s, "", fmt.Sprintf(`{"model":"group/g","stream":%v,"messages":[{"role":"user","content":"hi"}]}`, stream))
|
|
if code != 200 || !strings.Contains(body, "hello") {
|
|
t.Fatalf("%d %s", code, body)
|
|
}
|
|
route := lastRoute(s).ID
|
|
row := rowOf(t, func(u usage.Record) bool { return u.RouteID == route })
|
|
if row.UpBytes == 0 || row.DownBytes == 0 {
|
|
t.Fatalf("row: up %d, down %d", row.UpBytes, row.DownBytes)
|
|
}
|
|
if got := s.Traffic(); got.Up != int64(row.UpBytes) || got.Down != int64(row.DownBytes) {
|
|
t.Fatalf("traffic %+v; the row: up %d, down %d", got, row.UpBytes, row.DownBytes)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// A reply is counted as it streams in, not when it ends: while the
|
|
// vendor holds its stream open after the first event, the gateway's down
|
|
// count already has that event's bytes.
|
|
func TestTrafficCountsAStreamAsItFlows(t *testing.T) {
|
|
fresh(t)
|
|
first := `data: {"id":"c1","choices":[{"index":0,"delta":{"role":"assistant","content":"hello"}}]}` + "\n\n"
|
|
rest := sse(`data: {"id":"c1","choices":[{"index":0,"delta":{},"finish_reason":"stop"}],"usage":{"prompt_tokens":3,"completion_tokens":1}}`, `data: [DONE]`)
|
|
release := make(chan struct{})
|
|
vendor := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
io.Copy(io.Discard, r.Body)
|
|
w.Header().Set("Content-Type", "text/event-stream")
|
|
io.WriteString(w, first)
|
|
w.(http.Flusher).Flush()
|
|
select {
|
|
case <-release:
|
|
case <-r.Context().Done():
|
|
return
|
|
}
|
|
io.WriteString(w, rest)
|
|
}))
|
|
t.Cleanup(vendor.Close)
|
|
var once bool
|
|
free := func() {
|
|
if !once {
|
|
once = true
|
|
close(release)
|
|
}
|
|
}
|
|
t.Cleanup(free)
|
|
if err := provider.Save(provider.Provider{ID: "b", Name: "B", Key: "k", Models: []string{"m"}, Chat: vendor.URL + "/v1"}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
s := New()
|
|
done := make(chan struct{})
|
|
go func() {
|
|
defer close(done)
|
|
postAs(t, s, "", `{"model":"b/m","stream":true,"messages":[{"role":"user","content":"hi"}]}`)
|
|
}()
|
|
deadline := time.Now().Add(10 * time.Second)
|
|
for s.Traffic().Down < int64(len(first)) {
|
|
if time.Now().After(deadline) {
|
|
free()
|
|
<-done
|
|
t.Fatalf("mid-stream traffic %+v; the vendor had sent %d bytes", s.Traffic(), len(first))
|
|
}
|
|
time.Sleep(5 * time.Millisecond)
|
|
}
|
|
mid := s.Traffic()
|
|
if mid.Up == 0 {
|
|
t.Fatalf("mid-stream traffic %+v: nothing counted up", mid)
|
|
}
|
|
free()
|
|
<-done
|
|
if end := s.Traffic(); end.Down != mid.Down+int64(len(rest)) {
|
|
t.Fatalf("traffic mid-stream %+v, at the end %+v; the rest was %d bytes", mid, end, len(rest))
|
|
}
|
|
}
|
|
|
|
// A Codex turn on the Responses WebSocket is in the gateway's count as
|
|
// what the socket was written, as its row is, not the HTTP body built
|
|
// before the transport chose the socket; one that went over HTTP after a
|
|
// refused handshake is in it as that body (#5781's 13 MB rows, live).
|
|
func TestTrafficCountsWhatTheSocketSent(t *testing.T) {
|
|
for _, tc := range []struct {
|
|
name string
|
|
f *fakeSocketBackend
|
|
}{
|
|
{"kept socket", &fakeSocketBackend{keep: true}},
|
|
{"refused, over HTTP", &fakeSocketBackend{refuse: http.StatusForbidden, whole: true}},
|
|
} {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
socketOn(t)
|
|
f := tc.f
|
|
f.frames = keptReply(f)
|
|
chatgpt(t, f.ServeHTTP)
|
|
s := New()
|
|
start := time.Now()
|
|
big := strings.Replace(userPing, "ping", strings.Repeat("x", 50_000), 1)
|
|
for _, body := range []string{
|
|
keptTurn("conv-1", "gpt-5.5-ws", big),
|
|
keptTurn("conv-1", "gpt-5.5-ws", big, asstPong, userAgain),
|
|
} {
|
|
rec := httptest.NewRecorder()
|
|
req := httptest.NewRequest("POST", CodexPath+"/responses", strings.NewReader(body))
|
|
req.Header.Set("Authorization", "Bearer chatgpt-token-acct-1")
|
|
req.Header.Set("chatgpt-account-id", "acct-1")
|
|
s.Handler().ServeHTTP(rec, req)
|
|
if rec.Code != 200 || !strings.Contains(rec.Body.String(), "pong") {
|
|
t.Fatalf("%d %s", rec.Code, rec.Body.String())
|
|
}
|
|
}
|
|
var rows []usage.Record
|
|
for deadline := time.Now().Add(10 * time.Second); ; time.Sleep(20 * time.Millisecond) {
|
|
rows = rows[:0]
|
|
for _, r := range usage.Load(start) {
|
|
if r.Time.After(start) && r.Model == "gpt-5.5-ws" {
|
|
rows = append(rows, r)
|
|
}
|
|
}
|
|
if len(rows) >= 2 || time.Now().After(deadline) {
|
|
break
|
|
}
|
|
}
|
|
if len(rows) != 2 {
|
|
t.Fatalf("%d rows, want 2", len(rows))
|
|
}
|
|
up := 0
|
|
for _, r := range rows {
|
|
up += r.UpBytes
|
|
}
|
|
posts, sockets := f.counts()
|
|
if f.refuse == 0 && (posts != 0 || sockets != 2) || f.refuse != 0 && posts != 2 {
|
|
t.Fatalf("%d posts, %d socket turns", posts, sockets)
|
|
}
|
|
if got := s.Traffic().Up; got != int64(up) || up == 0 {
|
|
t.Fatalf("traffic up %d; the rows: %d", got, up)
|
|
}
|
|
if f.refuse == 0 {
|
|
if want := len(f.turn(0).msg) + len(f.turn(1).msg); up != want {
|
|
t.Fatalf("rows and traffic up %d; the socket was written %d", up, want)
|
|
}
|
|
} else if up < 100_000 {
|
|
t.Fatalf("rows and traffic up %d; two HTTP bodies of over 50,000 went", up)
|
|
}
|
|
})
|
|
}
|
|
}
|