Files
magpie/internal/agent/loopback_fast_test.go
T
yetone 079da3a785 perf: a state poll lists dsh profiles once, and skips work for Codex's models and gateway base
Each /api/state poll redid work for each agent row. That work is now done
once, or skipped:
- dsh's profile list is held for the request (dshProfilesHeld). dshModern,
  Joined and dshLead each globbed profiles/*/cordis.patch.yml again.
  Writers still call dshProfiles.
- CodexSignInGone reads the record first. With nothing on record for the
  home, the usual case, it no longer parses auth.json and hashes the
  refresh token on each look. A record that can't be read answers as
  before.
- Codex's effort field reads catalog.CodexShared(), which doesn't copy.
  With both lists, they are joined into one presized slice.
- codexViaMagpieFor builds one presized slice instead of two that grow
  and are then joined.
- onAnotherMachine answers a plain loopback base (localhost, 127.0.0.1,
  [::1], digits-only port, no user info) without url.Parse. Anything
  else still goes through url.Parse.

Measured on mote with the real-account sandbox, allocations over 40
/api/state polls, two alternating pairs:
- before (216ac6f85): 34.6 and 36.2 MB
- after: 26.8 and 23.7 MB

Run on mote:
- go vet on agent, provider and catalog.
- go test, all of internal/catalog.
- go test, internal/provider -run "Gone|SignIn|Codex".
- go test, internal/agent -run "OnAnother|Codex|Dsh|DSH|ZCode|Zcode|Loopback".
All ok.

New test: TestOnAnotherMachineQuickLookAgrees compares the quick look with
the url.Parse logic over 21 bases (user info, a port with letters,
uppercase, [::1], %2f) and requires 0 allocations for the gateway base.

Not tried: the full internal/agent suite (about 50 min).
2026-11-10 08:12:56 +08:00

41 lines
1.4 KiB
Go

package agent
import (
"net"
"net/url"
"testing"
)
// onAnotherMachine's quick look at a loopback base answers as url.Parse
// does, user info and odd ports included, and parses nothing for the
// gateway's own base, which a state poll asks of each agent.
func TestOnAnotherMachineQuickLookAgrees(t *testing.T) {
slow := func(base string) bool {
u, err := url.Parse(base)
if err != nil || (u.Scheme != "http" && u.Scheme != "https") || u.Hostname() == "" {
return false
}
h := u.Hostname()
if h == "localhost" {
return false
}
ip := net.ParseIP(h)
return ip == nil || !ip.IsLoopback() && !ip.IsUnspecified()
}
for _, b := range []string{
"http://127.0.0.1:3425/v1", "https://127.0.0.1:3425", "http://localhost:3425/v1?x=1",
"http://[::1]:3425/v1", "http://[::1]/v1", "http://127.0.0.1/v1#a", "http://127.0.0.1:/v1",
"http://127.0.0.1:3425@evil.example/v1", "http://u:p@127.0.0.1:3425/v1",
"http://127.0.0.1:34x5/v1", "http://LOCALHOST:3425", "http://127.0.0.2:1/v1",
"http://0.0.0.0:1", "http://192.168.1.5:3425/v1", "https://api.example.com/v1",
"ftp://127.0.0.1/", "127.0.0.1:3425", "http://", "", "http://[::1", "http://127.0.0.1%2f:1/",
} {
if got, want := onAnotherMachine(b), slow(b); got != want {
t.Errorf("%q: %v, url.Parse says %v", b, got, want)
}
}
if n := testing.AllocsPerRun(50, func() { onAnotherMachine("http://127.0.0.1:3425/v1") }); n != 0 {
t.Errorf("the gateway's base: %.0f allocations, want none", n)
}
}