Files
magpie/internal/provider/usage_renames_memo_test.go
T
yetone e364d27fce agent, provider, profile: a state poll builds no agents per profile, keys dsh entries without joining them, and works out renames once a change
What /api/state still allocated with real accounts, forty polls on mote
(t29, 76 and 83 MB):

- profile.Details built every agent again for each profile listed
  (7 MB). DetailsIn takes the agents the page already holds for its
  request; Details keeps its signature for other callers.
- dshNodeShared joined an entry's lines into a string to key its parsed
  node (5.7 MB). It now hashes the lines with maphash and checks them
  against its own copy, so a hit allocates nothing; a caller changing
  its slice in place still gets the new text.
- dsh's Joined check read and parsed the first profile's list each poll
  (5.2 MB). It uses dshLook, the parse shared until the file changes;
  dshOver only changes a copy of the items.
- provider.UsageRenames parsed the whole providers.json for each
  profile's in-use check (5.1 MB). It is worked out through filememo
  once per change; a file that doesn't decode goes through read's own
  last-good path as before.

Measured on mote, sandbox HOME with the owner's real account data, 5
warm-up then 40 calls, paired runs:
  /api/state  t29 82.9 / 76.0 MB  ->  t30 38.1 / 47.4 MB  (-46%)
  /api/drift  t29 7.3 / 5.5 MB    ->  t30 9.7 / 7.1 MB (512 kB sampling
              noise: no dsh or renames frame in either top list)

Tests:
- TestUsageRenamesParsedOnceAChange: 60 providers, a look at unchanged
  renames allocates at most 10 times, a rename since shows at once.
  Red on the old rename.go (424 allocations).
- TestDshLookParsesAnEntryOnce now asks dshNodeShared alone for 0
  allocations and checks a slice changed in place. Red on the old
  dsh.go (1 allocation).

Run on mote at this tree: gofmt -l clean; go vet of agent, provider,
profile, gui; go test -count=1 of internal/agent (390s), provider
(256s), profile, gui (174s) and usage: all ok.
Not run: the full ./... suite and the GUI (JS) suite; no JS changed.
2026-11-10 07:36:19 +08:00

39 lines
1.2 KiB
Go

package provider
import (
"fmt"
"testing"
"time"
)
// Each profile's in-use check on a state poll asks for the renames; with
// providers.json unchanged they are worked out once, not parsed from the
// whole file again, and a rename after still shows at once.
func TestUsageRenamesParsedOnceAChange(t *testing.T) {
isolate(t)
t.Setenv("HOME", t.TempDir())
t.Setenv("USERPROFILE", t.TempDir())
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
t.Setenv("XDG_CACHE_HOME", t.TempDir())
for i := range 60 {
if err := Save(Provider{ID: fmt.Sprintf("p%d", i), Name: fmt.Sprintf("P %d", i), Key: "k", Chat: "http://127.0.0.1:1/v1", Models: []string{"a", "b", "c"}}); err != nil {
t.Fatal(err)
}
}
if err := Rename("p1", "one"); err != nil {
t.Fatal(err)
}
if got := UsageRenames().Of("p1", time.Now()); got != "one" {
t.Fatalf("p1 went to %q", got)
}
if n := testing.AllocsPerRun(20, func() { UsageRenames() }); n > 10 {
t.Errorf("renames of an unchanged file: %.0f allocations, want a few", n)
}
if err := Rename("p2", "two"); err != nil {
t.Fatal(err)
}
if got := UsageRenames().Of("p2", time.Now()); got != "two" {
t.Errorf("a rename since: p2 went to %q", got)
}
}