Files
magpie/internal/edit/toml_each_test.go
T
yetone 216ac6f850 edit, agent: Grok Build's own model tables are read in one pass with nothing built, and its efforts read the shared list
What /api/state still allocated for Grok Build with real accounts, forty
polls on mote (t30):

- grokOwnOptions asked GetTOMLTable for each [model."<id>"] table of
  ~/.grok/config.toml: a stat and a fresh map a table on every look
  (4.6 MB). edit.EachTOMLTable walks the file's kept parse once and
  hands each ordinary table, in TOMLTables' order, with Scalar reading
  a key as GetTOMLTable's map has it (the last of a key written twice).
- the model field's efforts took magpieModels("grok"), a copy of the
  whole list per look, to read one entry (4.5 MB). It reads
  sharedModels; catalog.Efforts already returned a slice of the shared
  entry.

Measured on mote, sandbox HOME with the owner's real account data, 5
warm-up then 40 calls, paired runs:
  /api/state  t30 42.3 / 43.2 MB  ->  t31 33.2 / 38.1 MB  (-16%)
No GetTOMLTable, grokOwnOptions or magpieModels frame is left in t31's
top list.

Tests:
- TestEachTOMLTableAgreesWithGetTOMLTable: 200 model tables, an array
  table and a plain one; names match TOMLTables, every Scalar matches
  GetTOMLTable's map, a look allocates at most 5 times, a missing file
  has no tables and no error.

Run on mote at this tree: gofmt -l clean; go vet of edit and agent;
go test -count=1 of internal/edit and internal/agent (247s): ok.
Not run: the full ./... suite and the GUI suite; no JS changed.
2026-11-10 07:46:15 +08:00

61 lines
1.8 KiB
Go

package edit
import (
"fmt"
"os"
"path/filepath"
"strings"
"testing"
)
// EachTOMLTable gives what TOMLTables and GetTOMLTable give, table by
// table, without a map or a stat for each: Grok Build's model list reads
// every [model."<id>"] table of its config on each look.
func TestEachTOMLTableAgreesWithGetTOMLTable(t *testing.T) {
var b strings.Builder
b.WriteString("top = 1\n\n[[array]]\nname = \"skipped\"\n\n[plain]\nname = \"p\"\nlist = [1, 2]\n\n")
for i := range 200 {
fmt.Fprintf(&b, "[model.\"m-%d\"]\nname = \"Model %d\"\nbase_url = 'https://h%d.example/v1' # own\n\n", i, i, i)
}
path := filepath.Join(t.TempDir(), "config.toml")
if err := os.WriteFile(path, []byte(b.String()), 0o600); err != nil {
t.Fatal(err)
}
names, err := TOMLTables(path)
if err != nil {
t.Fatal(err)
}
var got []string
if err := EachTOMLTable(path, func(tb TOMLTable) {
got = append(got, tb.Name())
want, _ := GetTOMLTable(path, tb.Name())
for _, k := range []string{"name", "base_url", "list", "missing"} {
if v := tb.Scalar(k); v != want[k] {
t.Errorf("%s.%s: %q, GetTOMLTable has %q", tb.Name(), k, v, want[k])
}
}
}); err != nil {
t.Fatal(err)
}
if strings.Join(got, "|") != strings.Join(names, "|") {
t.Fatalf("tables %v, TOMLTables lists %v", got, names)
}
seen := 0
if n := testing.AllocsPerRun(10, func() {
seen = 0
EachTOMLTable(path, func(tb TOMLTable) {
if tb.Scalar("base_url") != "" {
seen++
}
})
}); n > 5 {
t.Errorf("a look at 201 tables: %.0f allocations, want a few", n)
}
if seen != 200 {
t.Errorf("tables with a base_url: %d", seen)
}
if err := EachTOMLTable(filepath.Join(t.TempDir(), "none.toml"), func(TOMLTable) { t.Error("a missing file has tables") }); err != nil {
t.Errorf("missing file: %v", err)
}
}