agent: applied.json is parsed once a change for drift
appliedOf is asked for every agent on every drift poll (the GUI asks /api/drift every 15s while visible), and each ask read and decoded the whole applied.json. It now goes through filememo.Read: an unchanged file is parsed once and its map shared (every appliedOf caller only reads it). appliedSave calls filememo.Look so magpie's own record is read at once, even inside a held request. Writers (record, appliedLoad's other callers) still decode a fresh map. Run (mote, at this tree): - go test -count=1 ./internal/agent ./internal/filememo: ok (227s) - go vet ./internal/agent for darwin, linux, windows: clean - TestAppliedOfParsesAnUnchangedFileOnce: green; with appliedOf put back on appliedLoad by hand it is red (25 allocations), restored - Real accounts (sandbox HOME copy), 40 /api/drift calls, paired runs: appliedLoad 2.0/3.1 MB before, gone after; totals 26.1/20.6 MB before, 18.9/18.2 MB after. Not tried: Windows and Linux runs of the package (vet only).
This commit is contained in:
@@ -62,12 +62,21 @@ func appliedSave(m map[string]applied) {
|
||||
b, _ := json.MarshalIndent(m, "", " ")
|
||||
os.MkdirAll(filepath.Dir(appliedPath()), 0o755)
|
||||
os.WriteFile(appliedPath(), b, 0o600)
|
||||
filememo.Look(appliedPath())
|
||||
}
|
||||
|
||||
// appliedOf is what magpie set on agent id. Every look at an agent's
|
||||
// drift asks it, for every agent a poll lists, so the file is parsed once
|
||||
// a change and the map is shared: callers only read it.
|
||||
func appliedOf(id string) applied {
|
||||
appliedMu.Lock()
|
||||
defer appliedMu.Unlock()
|
||||
return appliedLoad()[id]
|
||||
m, _ := filememo.Read("applied", appliedPath(), func(b []byte) (map[string]applied, error) {
|
||||
out := map[string]applied{}
|
||||
json.Unmarshal(b, &out)
|
||||
return out, nil
|
||||
})
|
||||
return m[id]
|
||||
}
|
||||
|
||||
// record keeps what a field reads after magpie set it; a field put back to
|
||||
|
||||
@@ -0,0 +1,41 @@
|
||||
package agent
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/yetone/magpie/internal/filememo"
|
||||
)
|
||||
|
||||
// appliedOf is asked for every agent on every drift poll: an unchanged
|
||||
// applied.json is parsed once, and magpie's own record is read at once.
|
||||
func TestAppliedOfParsesAnUnchangedFileOnce(t *testing.T) {
|
||||
stashHome(t)
|
||||
p := appliedPath()
|
||||
os.MkdirAll(filepath.Dir(p), 0o755)
|
||||
if err := os.WriteFile(p, []byte(`{"codex":{"fields":{"model":"magpie/a"}}}`), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
old := time.Now().Add(-time.Minute)
|
||||
os.Chtimes(p, old, old)
|
||||
if got := appliedOf("codex").Fields["model"]; got != "magpie/a" {
|
||||
t.Fatalf("model = %q", got)
|
||||
}
|
||||
release := filememo.Hold()
|
||||
defer release()
|
||||
appliedOf("codex")
|
||||
if n := testing.AllocsPerRun(20, func() { appliedOf("codex") }); n > 2 {
|
||||
t.Fatalf("%v allocations for an unchanged file", n)
|
||||
}
|
||||
record("codex", "model", "magpie/b")
|
||||
if got := appliedOf("codex").Fields["model"]; got != "magpie/b" {
|
||||
t.Fatalf("after record, model = %q", got)
|
||||
}
|
||||
os.Remove(p)
|
||||
filememo.Look(p)
|
||||
if got := appliedOf("codex"); got.Fields != nil {
|
||||
t.Fatalf("gone file: %+v", got)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user