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.
This commit is contained in:
@@ -6,6 +6,8 @@ import (
|
||||
"net/http"
|
||||
"slices"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/yetone/magpie/internal/agent"
|
||||
"github.com/yetone/magpie/internal/provider"
|
||||
@@ -106,7 +108,49 @@ func agentModelCount(a *agent.Agent, fields []fieldJSON) *modelCountJSON {
|
||||
return nil
|
||||
}
|
||||
|
||||
// modelCounts keeps each agent's count while provider.StampNow stays the
|
||||
// same, for a minute at most, as the agents' model lists are kept
|
||||
// (agent's magpieModelsFor): every poll of the Agents page counted each
|
||||
// agent's list anew, ListedFor built whole for a number (0.27 GB of the
|
||||
// 2.6 GB a real config allocated over 20 polls).
|
||||
var modelCounts struct {
|
||||
sync.Mutex
|
||||
m map[string]keptCount
|
||||
}
|
||||
|
||||
type keptCount struct {
|
||||
c modelCountJSON
|
||||
stamp provider.Stamp
|
||||
at time.Time
|
||||
}
|
||||
|
||||
// modelCountFor is how long a count is kept while the stamp says nothing
|
||||
// changed: what it can't see (another app rewriting an account's file, a
|
||||
// model retired by its date) reaches the count within it.
|
||||
const modelCountFor = time.Minute
|
||||
|
||||
func modelCount(id string) *modelCountJSON {
|
||||
modelCounts.Lock()
|
||||
k, ok := modelCounts.m[id]
|
||||
modelCounts.Unlock()
|
||||
// the stamp is read before the count is made: a change while it is
|
||||
// moves it past the one kept, and the next call counts again
|
||||
stamp := provider.StampNow()
|
||||
if !ok || k.stamp != stamp || time.Since(k.at) >= modelCountFor {
|
||||
k = keptCount{countModels(id), stamp, time.Now()}
|
||||
modelCounts.Lock()
|
||||
if modelCounts.m == nil {
|
||||
modelCounts.m = map[string]keptCount{}
|
||||
}
|
||||
modelCounts.m[id] = k
|
||||
modelCounts.Unlock()
|
||||
}
|
||||
c := k.c
|
||||
c.By, c.Names = slices.Clone(c.By), slices.Clone(c.Names)
|
||||
return &c
|
||||
}
|
||||
|
||||
func countModels(id string) modelCountJSON {
|
||||
listed, _ := provider.ListedFor(id)
|
||||
off := provider.ModelOff(id)
|
||||
c := &modelCountJSON{Listed: len(listed)}
|
||||
@@ -131,7 +175,7 @@ func modelCount(id string) *modelCountJSON {
|
||||
}
|
||||
c.By[i].N++
|
||||
}
|
||||
return c
|
||||
return *c
|
||||
}
|
||||
|
||||
// usedBy reports whether one of the agent's values is the entry: its id,
|
||||
|
||||
@@ -0,0 +1,75 @@
|
||||
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)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user