Files
magpie/internal/filememo/filememo_test.go
T
yetone 2a4042a94d perf: magpie opens and shows the Agents page in a fraction of the time it took (Junjie Zhou on Discord: 8 个 agent 30 个 provider … 每次我打开的时候都会很慢,要好几秒。setting 页面秒进后 我切换到 agent 就有问题) — at start, writing the agents' model lists (agent.SyncCatalog) built every provider's catalog thousands of times with nothing held, each build reading migrations.json, settings and groups again: 6000+ file reads and ~8.5s of CPU in the first 8s with the owner's 30-odd providers, all competing with the first /api/state and the Agents page. Now SyncCatalog, magpieModels and main's start-up moves run under provider.Hold, migrations, groups, settings and a provider's fetched models are held for a second like the rest, and filememo.Hold lets a GET /api/* not stat a file it confirmed unchanged within the last second (any magpie write, Changed() or a recent mtime still reads it again). A big ~/.codex/config.toml parsed in quadratic time (the TOML parser's Shape counts newlines from the start for every key): a 3 MB one took 5.4s, now 0.04s (tomlShaper). theInstalled reads through edit.Read's memo. Measured with a copy of the owner's magpie config and agent configs under a sandbox HOME, the real server in WebKit: CPU in the first 8s 8.59s → 0.39s, page up 475 → 75ms, Agents rows drawn 775 → 325ms after start; the agents' written model files are unchanged (pi byte for byte, opencode the same models). TestTOMLShaperMatchesParser, TestTOMLBigFileParsesInLinearTime (fails on the old code), TestHold (filememo), TestHoldReadsMigrationsOnce. go vet, builds (darwin, GOOS=linux nogui, GOOS=windows) and go test -tags nogui ./... under a temp HOME pass.
2026-10-05 03:02:40 +08:00

99 lines
2.8 KiB
Go

package filememo
import (
"fmt"
"os"
"path/filepath"
"testing"
"time"
)
func TestRead(t *testing.T) {
p := filepath.Join(t.TempDir(), "f")
os.WriteFile(p, []byte("a"), 0o600)
n := 0
parse := func(b []byte) (string, error) { n++; return string(b), nil }
for range 3 {
if v, err := Read("t", p, parse); err != nil || v != "a" {
t.Fatal(v, err)
}
}
if n != 1 {
t.Fatalf("parsed %d times", n)
}
os.WriteFile(p, []byte("bb"), 0o600)
os.Chtimes(p, time.Now(), time.Now().Add(time.Second))
if v, _ := Read("t", p, parse); v != "bb" || n != 2 {
t.Fatalf("after a change: %q, parsed %d times", v, n)
}
os.Remove(p)
if _, err := Read("t", p, parse); err == nil {
t.Fatal("a file gone is an error")
}
}
// two writes of one size inside one tick of the file system's clock (Linux
// stamps a file's time from a clock a few milliseconds coarse) leave the
// same time and size: the second is still read, not the first's parse
func TestReadSameSizeSameTick(t *testing.T) {
p := filepath.Join(t.TempDir(), "f")
parse := func(b []byte) (string, error) { return string(b), nil }
for i := range 200 {
a, b := fmt.Sprintf("a%03d,b", i), fmt.Sprintf("b%03d,a", i)
os.WriteFile(p, []byte(a), 0o600)
if v, _ := Read("tick", p, parse); v != a {
t.Fatalf("read %q, want %q", v, a)
}
os.WriteFile(p, []byte(b), 0o600)
if v, _ := Read("tick", p, parse); v != b {
t.Fatalf("read %q after writing %q: the first write's parse", v, b)
}
}
}
// While a request holds what was read, a file found unchanged is not looked
// at again (the GUI's state looked a few thousand times, seconds on a slow
// disk); a write magpie makes (Forget), a second gone by or the release has
// it looked at, and a file just written is always read again.
func TestHold(t *testing.T) {
p := filepath.Join(t.TempDir(), "f")
os.WriteFile(p, []byte("a"), 0o600)
old := time.Now().Add(-time.Minute)
os.Chtimes(p, old, old)
parse := func(b []byte) (string, error) { return string(b), nil }
write := func(s string) {
os.WriteFile(p, []byte(s), 0o600)
at := old.Add(time.Duration(len(s)) * time.Second)
os.Chtimes(p, at, at)
}
release := Hold()
if v, _ := Read("hold", p, parse); v != "a" {
t.Fatal(v)
}
write("bb")
if v, _ := Read("hold", p, parse); v != "a" {
t.Fatalf("looked at again while held: %q", v)
}
Forget()
if v, _ := Read("hold", p, parse); v != "bb" {
t.Fatalf("after a write magpie made: %q", v)
}
write("ccc")
release()
release()
if v, _ := Read("hold", p, parse); v != "ccc" {
t.Fatalf("released: %q", v)
}
if holds != 0 {
t.Fatalf("holds left: %d", holds)
}
// a file written just now is read each time, held or not
defer Hold()()
os.WriteFile(p, []byte("d"), 0o600)
Read("hold", p, parse)
os.WriteFile(p, []byte("e"), 0o600)
if v, _ := Read("hold", p, parse); v != "e" {
t.Fatalf("a file written just now: %q", v)
}
}