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.
55 lines
2.1 KiB
Go
55 lines
2.1 KiB
Go
package agent
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
// The drift poll looks at each dsh entry's config several times a poll; an
|
|
// entry whose text hasn't changed is parsed once, and a writer's node is
|
|
// its own, so a change to it never shows in the next look.
|
|
func TestDshLookParsesAnEntryOnce(t *testing.T) {
|
|
lines := []string{"- id: " + dshPiRow, " name: pi", " config:", " model: \"m1\"", " providers:"}
|
|
for i := range 400 {
|
|
lines = append(lines, fmt.Sprintf(" p%d:", i), " baseURL: http://127.0.0.1:1/v1", " models: [a, b, c]")
|
|
}
|
|
it := dshItem{id: dshPiRow, lines: lines}
|
|
if got := dshConfig(it)["model"]; got != "m1" {
|
|
t.Fatalf("model: %q", got)
|
|
}
|
|
if n := testing.AllocsPerRun(20, func() { dshConfig(it); dshConfigIndex([]dshItem{it}, dshPiRow) }); n > 50 {
|
|
t.Errorf("a look at an unchanged entry: %.0f allocations, want a few", n)
|
|
}
|
|
// the lookup itself: no key built from the 1200 lines
|
|
if n := testing.AllocsPerRun(20, func() { dshNodeShared(it) }); n != 0 {
|
|
t.Errorf("finding a parsed entry: %.0f allocations, want 0", n)
|
|
}
|
|
// the cache keeps its own copy of the lines: a caller changing its
|
|
// slice in place gets the new text, not the node of the old
|
|
own := append([]string{}, lines...)
|
|
mine := dshItem{id: dshPiRow, lines: own}
|
|
if got := dshConfig(mine)["model"]; got != "m1" {
|
|
t.Fatalf("copy: %q", got)
|
|
}
|
|
own[3] = " model: m3"
|
|
if got := dshConfig(mine)["model"]; got != "m3" {
|
|
t.Errorf("lines changed in place: %q, want m3", got)
|
|
}
|
|
w, err := dshNode(it)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
yamlSetKey(yamlKey(w, "config"), "model", yamlKey(w, "name"))
|
|
if got := dshConfig(it)["model"]; got != "m1" {
|
|
t.Errorf("a writer's change showed in a look: %q", got)
|
|
}
|
|
it.lines = append(append([]string{}, it.lines[:3]...), append([]string{" model: m2"}, it.lines[4:]...)...)
|
|
if got := dshConfig(it)["model"]; got != "m2" {
|
|
t.Errorf("changed text: %q, want m2", got)
|
|
}
|
|
if _, err := dshNodeShared(dshItem{id: "bad", lines: []string{"- id: bad", " : ["}}); err == nil || !strings.Contains(err.Error(), "bad") {
|
|
t.Errorf("bad entry: %v", err)
|
|
}
|
|
}
|