Files
magpie/internal/gateway/unserved_kept_test.go
T
yetone 5daea73c6e gateway: whether a model is served is answered from catalog names kept while the catalog stays the same
unserved() built provider.Catalog() on every request to see whether the
asked id is a catalog name. In a 10-minute profile of a sandbox instance
(fake upstream, ~1,000 gateway requests plus the GUI's polls) that was
2.26GB of 9.8GB allocated. The id and model names are now kept in a
stamped set (provider.StampNow, at most a minute), so a saved provider is
seen at once.

Measured, gateway-only load for 300s on the same sandbox data (fixed
build also carries 0afeebd01's pooled zstd body decoders):
  before: 3.9GB allocated for 434 requests (unserved 1.22GB, Catalog
          1.26GB, zstd 863MB), numGC +52
  after:  2.96GB for 443 requests (unserved 25MB, Catalog 57MB, zstd
          body decode gone from the top), numGC +29

TestUnservedKeptWhileCatalogStays (a provider of 1,000 models) counts
two answers already given against one catalog build in the same run,
since the package's other tests leave background work allocating beside
it (an absolute bar of 2000 read 2733 in the full suite, 682 alone):
67985 allocations against a build of 33262 before (fails, needs under a
third), 1486 after; a provider saved afterwards is served.

Run: gofmt -l; GOOS=linux and GOOS=windows go vet -tags nogui
./internal/gateway/; go test -race -run
'Unserved|Desktop|StandIn|Science|ClaudeTier|Zstd' ./internal/gateway/ ok.
Full suite (go test -tags nogui -timeout 45m ./..., sandbox HOME, on
this commit's first test): 62 packages ok; gateway and agent hit the
45m timeout under machine load (no hung test: TestCachePartialUpdateMatrix
and TestSyncKeepsAnAddressOnAnotherMachine were at 0s), provider red in
TestPluginPicksComeBackWithTheirList (#3051, passes alone 3/3), and this
test's absolute bar read 2733, fixed above. Rerun at this head:
gateway ok 885s, agent ok 899s, provider ok 226s.
2026-10-21 04:16:28 +08:00

50 lines
1.9 KiB
Go

package gateway
import (
"fmt"
"testing"
"github.com/yetone/magpie/internal/provider"
)
// Whether a model an agent asks for is served here is answered from the
// catalog's names kept by provider.StampNow: every request an agent sends
// asks it (standIn), and each one built the whole catalog to look, 2.26 GB
// of 9.8 GB in 10 minutes of ~1,000 requests. A provider saved since is
// seen at once.
func TestUnservedKeptWhileCatalogStays(t *testing.T) {
setup(t, provider.Chat, &fake{})
models := make([]string, 1000)
for i := range models {
models[i] = fmt.Sprintf("m%d", i)
}
if err := provider.Save(provider.Provider{ID: "big", Name: "Big", Key: "k", Chat: "http://127.0.0.1:1/v1", Models: models}); err != nil {
t.Fatal(err)
}
if unserved("m1") || unserved("big/m150") || !unserved("claude-sonnet-4-6") || !unserved("nope/claude-sonnet-4-6") {
t.Fatal("served wrong before anything is kept")
}
// ids with a provider in them, as magpie writes into agents' configs:
// a bare one asks the routing groups first, which the request's
// routing builds too
allocs := testing.AllocsPerRun(20, func() {
unserved("big/m150")
unserved("nope/claude-sonnet-4-6")
})
// measured against one catalog build in the same run, since the
// package's other tests leave background work allocating beside it:
// two answers built it twice (~2x), kept they are StampNow's look at
// three files and Find's of the providers for the id no entry has
built := testing.AllocsPerRun(5, func() { provider.Catalog() })
if allocs*3 > built {
t.Fatalf("%.0f allocations for two answers already given; one catalog build is %.0f", allocs, built)
}
if err := provider.Save(provider.Provider{ID: "anth", Name: "Anth", Key: "k", Anthropic: "http://127.0.0.1:1", Models: []string{"claude-sonnet-4-6"}}); err != nil {
t.Fatal(err)
}
if unserved("claude-sonnet-4-6") {
t.Fatal("a model of a provider saved since is still unserved")
}
}