Files
magpie/internal/gui/account_label_agents_test.go
T
yetone 019bfc951b gui: a page of accounts builds the agent list once, not once a row
accountLabel and the excluded-agents loop called agent.Find for each row,
and Find builds All() again each time: about 3,300 allocations a call.
Over 20 window polls that was 58 MB of the 1.4 GB a magpie allocated.
provider.Held keeps All() for the request that holds it (the gui's held
middleware holds every /api/ request), and agent.FindIn looks the name up
in that list. Outside a hold Held builds it as before, so a caller with
no request in hand gets the same answer.

TestAccountLabelsBuildTheAgentsOnce (sandbox HOME): outside a hold the
label's name and icon are the same; under Hold a row allocates 3 times
against 3302 to build every agent. Broken by hand on mote (agentNamed
back to agent.Find) it is red at 3305 a row.

Run on mote at this tree (on d714c4216): agent (846s) and provider
(459s) ok; gui red only in TestSettingsSaveKeepsWhatItDoesNotSend
(#4514, red on main) and TestMainWindowCorners (#3949, macOS 27). vet ok
on darwin and windows (agent, provider, gui) and linux (agent, provider;
gui needs cgo there).
2026-11-01 03:49:03 +08:00

47 lines
1.7 KiB
Go

package gui
import (
"path/filepath"
"testing"
"github.com/yetone/magpie/internal/agent"
"github.com/yetone/magpie/internal/filememo"
"github.com/yetone/magpie/internal/provider"
)
// Each account's row on the Providers page is named for its agent, and
// agent.Find built every agent magpie knows (about a hundred, each with
// its files looked at) again for each row: with the owner's config 58 MB
// of the 1.4 GB a magpie allocated over 20 window polls. A request builds
// them once for all its rows.
func TestAccountLabelsBuildTheAgentsOnce(t *testing.T) {
home := t.TempDir()
t.Setenv("HOME", home)
t.Setenv("USERPROFILE", home)
t.Setenv("XDG_CONFIG_HOME", filepath.Join(home, ".config"))
t.Setenv("XDG_CACHE_HOME", filepath.Join(home, ".cache"))
codex, err := agent.Find("codex")
if err != nil {
t.Fatal(err)
}
p := provider.Provider{ID: "codex-a", Name: "Codex A", Account: &provider.Account{Agent: "codex", User: "a"}}
if name, icon := accountLabel(p); name != codex.Name || icon != codex.Icon {
t.Fatalf("not held: %q %q, want %q %q", name, icon, codex.Name, codex.Icon)
}
all := testing.AllocsPerRun(3, func() { agent.All() })
// as a GET /api/* is served (held)
defer provider.Hold()()
defer filememo.Hold()()
accountLabel(p) // the request's first row builds them
row := testing.AllocsPerRun(50, func() {
if name, icon := accountLabel(p); name != codex.Name || icon != codex.Icon {
t.Errorf("held: %q %q, want %q %q", name, icon, codex.Name, codex.Icon)
}
})
t.Logf("a row: %.0f allocations; building the agents: %.0f", row, all)
if row > all/20 {
t.Errorf("a row took %.0f allocations against %.0f to build every agent: the agents were built for it", row, all)
}
}