appdir.Config and Cache, catalog.CachePath and LivePath, settings.Path, provider.Path and the logins path each ran filepath.Join on every call, and they are called on every read of a cached file, many times a request. Those joins were 70 MB of the 1.4 GB twenty window polls allocated. appdir.Join and JoinFile keep each (dir, name, ext) they joined, up to 4096 of them, so a path named again is the same string. A changed HOME or folder is a different key, so a test's sandbox still gets its own path. TestFoldersJoinedOnce and TestLivePathNamedOnce: 0 allocations a call. Broken by hand on mote (the files before this change) they are red at 1 allocation a call, and 3 for LivePath. Run on mote at this tree (ond714c4216): gofmt clean; vet ok on darwin, windows and linux (appdir, catalog, settings, provider); go build ok; appdir, catalog, settings (49s), provider (187s), agent (324s) and gateway (1244s) ok. At the pushed head (on019bfc951, which adds provider.Held): vet of appdir, catalog, settings, provider and gui ok, go build ok, appdir, catalog and gui's TestAccountLabelsBuildTheAgentsOnce ok.
42 lines
1.4 KiB
Go
42 lines
1.4 KiB
Go
package appdir
|
|
|
|
import (
|
|
"path/filepath"
|
|
"testing"
|
|
)
|
|
|
|
// magpie's folders and the files named from them are joined once, not on
|
|
// every read of a file, and a folder changed since is seen at once.
|
|
func TestFoldersJoinedOnce(t *testing.T) {
|
|
t.Setenv("APPIMAGE", "")
|
|
r := root(t)
|
|
t.Setenv("XDG_CONFIG_HOME", filepath.Join(r, "cfg"))
|
|
t.Setenv("XDG_CACHE_HOME", filepath.Join(r, "cache"))
|
|
t.Cleanup(func() { UseExecutable("") })
|
|
UseExecutable("")
|
|
|
|
for name, f := range map[string]func(){
|
|
"Config": func() { Config() },
|
|
"Cache": func() { Cache() },
|
|
"JoinFile": func() { JoinFile(Cache(), "openai", ".json") },
|
|
} {
|
|
f()
|
|
if n := testing.AllocsPerRun(20, f); n > 0 {
|
|
t.Errorf("%s with the folders unchanged: %.0f allocations", name, n)
|
|
}
|
|
}
|
|
if got, want := JoinFile(Join(Cache(), "models"), "openai", ".json"), filepath.Join(r, "cache", "magpie", "models", "openai.json"); got != want {
|
|
t.Errorf("JoinFile = %q, want %q", got, want)
|
|
}
|
|
|
|
t.Setenv("XDG_CACHE_HOME", filepath.Join(r, "other"))
|
|
if got, want := Cache(), filepath.Join(r, "other", "magpie"); got != want {
|
|
t.Errorf("a cache folder changed since: %q, want %q", got, want)
|
|
}
|
|
t.Setenv("XDG_CONFIG_HOME", "")
|
|
t.Setenv(homeVar(), filepath.Join(r, "home"))
|
|
if got, want := Config(), filepath.Join(r, "home", ".config", "magpie"); got != want {
|
|
t.Errorf("under the home folder: %q, want %q", got, want)
|
|
}
|
|
}
|