Files
magpie/internal/gui/model_count_kept_test.go
T
yetone 0fe9f97c13 gui: an agent's model count is kept while nothing it counts changed
Every poll of the Agents page (agentModelCount, per agent per state)
counted each agent's model list anew: provider.ListedFor built whole,
with prices and groups, for a number and five names. On a real config
that was 0.27 GB of the 2.6 GB twenty polls allocated.

modelCount now keeps each agent's count under provider.StampNow (the
providers, settings and catalog files, filememo's generation and the
environment) for a minute at most, as agent's magpieModelsFor keeps
the agents' lists, and hands each caller its own copy of By and Names.
The stamp is read before the count is made, so a change while it is
made is counted on the next call.

TestModelCountKeptWhileNothingChanged: a kept count takes under a
twentieth of a count's bytes, a caller's write doesn't reach the next
caller, and a provider saved and a model hidden since are counted at
once. Red with modelCount counting each time ("a count already made
took 8223892 bytes, counting 8225345", 300 models).

Run at 88a0262bf plus this change, on mote: go vet (darwin, windows)
and gofmt of internal/gui clean; go test -count=1 -timeout 45m
./internal/gui: every test ok but two filed reds that fail the same on
main, TestSettingsSaveKeepsWhatItDoesNotSend (#4514) and
TestMainWindowCorners/macOS_26_SDK on macOS 27 (#3949). Linux vet of
internal/gui doesn't build on mote (Wails' cgo). The page's JSON is the
same, so the GUI suite wasn't run.
2026-11-01 01:56:48 +08:00

76 lines
2.3 KiB
Go

package gui
import (
"fmt"
"os"
"runtime"
"slices"
"testing"
"time"
"github.com/yetone/magpie/internal/provider"
"github.com/yetone/magpie/internal/settings"
)
// An agent's model count is kept while nothing it is counted from changed:
// every poll of the Agents page counted each agent's list anew, its whole
// ListedFor built for a number. A provider or a model hidden since is
// counted at once.
func TestModelCountKeptWhileNothingChanged(t *testing.T) {
sandboxHome(t)
var models []string
for i := range 300 {
models = append(models, fmt.Sprintf("m-%d", i))
}
if err := provider.Save(provider.Provider{ID: "relay", Name: "Relay", Key: "k", Chat: "http://127.0.0.1:1/v1", Models: models}); err != nil {
t.Fatal(err)
}
// written long enough ago that its time tells a later write apart
old := time.Now().Add(-time.Hour)
if err := os.Chtimes(provider.Path(), old, old); err != nil {
t.Fatal(err)
}
c := modelCount("codex")
if c.Listed < 300 || c.Shown != c.Listed {
t.Fatalf("relay's models not counted: %+v", c)
}
bytesPer := func(f func()) uint64 {
f()
var a, b runtime.MemStats
runtime.ReadMemStats(&a)
for range 20 {
f()
}
runtime.ReadMemStats(&b)
return (b.TotalAlloc - a.TotalAlloc) / 20
}
built := bytesPer(func() { countModels("codex") })
kept := bytesPer(func() { modelCount("codex") })
if kept*20 > built {
t.Fatalf("a count already made took %d bytes, counting %d", kept, built)
}
// each caller's copy is its own
mine := modelCount("codex")
mine.Names[0] = "mine"
mine.By[0].N = -1
if c := modelCount("codex"); c.Names[0] == "mine" || c.By[0].N < 0 {
t.Fatalf("a caller's write reached the count kept: %+v", c)
}
if err := provider.Save(provider.Provider{ID: "nb", Name: "NB", Key: "k", Chat: "http://127.0.0.1:1/v1", Models: []string{"x"}}); err != nil {
t.Fatal(err)
}
if got := modelCount("codex"); got.Listed != c.Listed+1 {
t.Fatalf("a provider saved since is not counted: %d, was %d", got.Listed, c.Listed)
}
s := settings.Load()
s.HiddenModels = map[string][]string{"codex": {"relay/m-1"}}
if err := settings.Save(s); err != nil {
t.Fatal(err)
}
if got := modelCount("codex"); got.Shown != got.Listed-1 || slices.Contains(got.Names, "m-1") {
t.Fatalf("a model hidden since is still counted: %+v", got)
}
}