Files
magpie/internal/gateway/info_count_test.go
T
yetone 318697dcea gateway: GET / counts the catalog once a change, not on every ask
Why: in the live heap profile (yg6888 on X, ~1 GB held), 5.4 GB of a
day's 36 GB of allocations came from info. It built the whole
provider.Catalog() only to report "models": len(...). Every magpie on
this computer asks the gateway which magpie it is (gateway.ServedBy):
- a window that doesn't serve, every 15s (watchGateway);
- each providersState while it doesn't serve;
- the CLI's daemon and web commands;
- the TUI.
These asks are seconds apart, so the catalog a request holds
(provider.Hold, 1s) had always gone by the next one.

What changed: info's count is kept in the same stamped memo as
AutoVision's lists. It is keyed by provider.StampNow, and built
again after a minute at the latest. The answer keeps its shape.

Measured: BenchmarkScratchInfo (not committed) sends GET / through
s.Handler() in a sandbox HOME with the live models.json and 12 API-key
providers, with 4f044c24c applied on both sides:

| | Time/op | Bytes/op | Allocs/op |
| --- | --- | --- | --- |
| Before | 4,069,096 ns | 1,287,667 B | 219 |
| After | 323,045 ns | 15,919 B | 90 |

Test: TestInfoCountsModelsOnceAChange asks info outside a held
catalog, as a probe 15s later does, with 4 and with 400 models
enabled. It got 64 allocations against 65. With the memo bypassed by
hand it went red at 1275 against 14673. It also checks that the
count follows a provider added through provider.Save.

Run:
- gofmt -l internal/: clean.
- GOOS=linux and GOOS=windows go vet -tags nogui ./...: clean.
- set -o pipefail; go test -count=1 -timeout 45m ./internal/gateway/
  ./internal/provider/ ./internal/catalog/ ./internal/gui/, at load
  ~400 (other sessions' suites), with this and the AutoVision commit
  and 4f044c24c applied:
  - gateway ok 2259.7s, provider ok 821.8s, catalog ok 16.5s.
  - gui FAIL in two tests that fail the same way without these
    changes, each run alone on 28bcd4f7e:
    - TestMainWindowCorners: killed at its 40s deadline (#1936, #2645).
    - TestSetAnswersWhileDevinAndProvidersHang: setting slow/slow-1
      took 10.9s against its 10s bound. Alone it took 4.6 to 5.9s
      without the changes, and passed (#1375).
2026-10-20 20:32:05 +08:00

50 lines
1.6 KiB
Go

package gateway
import (
"net/http"
"net/http/httptest"
"testing"
"github.com/yetone/magpie/internal/provider"
)
// info's count of the models is kept, and counted again once a provider
// is added. Another magpie asks every 15s or so, after the catalog a
// request held (provider.Hold) has gone: info is asked here outside one.
func TestInfoCountsModelsOnceAChange(t *testing.T) {
var s *Server
count := func() float64 {
r := httptest.NewRequest(http.MethodGet, "/", nil)
r.RemoteAddr = "127.0.0.1:5555"
w := httptest.NewRecorder()
s.info(w, r)
var out struct{ Models float64 }
if err := decodeJSON(w.Body.Bytes(), &out); err != nil {
t.Fatal(err)
}
return out.Models
}
// asked again with nothing changed, it costs the same whatever the
// lists' length
allocs := func(n int) float64 {
manyEyes(t, n, true)
s = New()
if m := count(); m != float64(len(provider.Catalog())) {
t.Fatalf("info says %v models of %d", m, len(provider.Catalog()))
}
return testing.AllocsPerRun(20, func() { count() })
}
few, many := allocs(4), allocs(400)
t.Logf("allocations a GET /: %.0f with 4 models listed, %.0f with 400", few, many)
if many > few+50 {
t.Fatalf("GET / made %.0f allocations with 400 models listed, %.0f with 4: the catalog is built again", many, few)
}
n := count()
if err := provider.Save(provider.Provider{ID: "more", Name: "More", Chat: "http://more.invalid/v1", Key: "k", Models: []string{"a", "b"}}); err != nil {
t.Fatal(err)
}
if m := count(); m != float64(len(provider.Catalog())) || m == n {
t.Fatalf("info says %v models after a provider was added, of %d", m, len(provider.Catalog()))
}
}