Files
magpie/internal/proc/listing_test.go
T
yetone 8a4cfe0e4e proc: a command that isn't installed is answered from each PATH folder's listing, not a stat per extension, so a Library change after a start no longer looks for ~100 agents' commands one file at a time on Windows (#1624, Hipple)
On every Library create, switch or delete, Targets() asks agent.Detected for
about a hundred agents. Each one's command is looked up with exec.LookPath,
and on Windows with FindTool's findDirs as well. A command that isn't
installed costs a stat of name+each PATHEXT extension in every folder. The
answers are cached (looked), but that cache is empty after a start and is
dropped whenever any PATH folder's mtime changes. With an antivirus slowing
every stat, that is the slow first change Hipple sees.

Each folder's listing is now read once (lower-cased names, also each name up
to every dot) and read again only when the folder's mtime changes. A
command that no folder has a name for is not found without a stat. One a
folder may hold, and any folder that can't be read, goes to exec.LookPath
and toolIn as before. On Windows the current folder is included unless
NoDefaultCurrentDirectoryInExePath is set, as exec.LookPath does.

Measured on the Windows test box (47 PATH folders, WSL agents off,
library ops through a throwaway test in a sandbox HOME):
- the first create after a start went from 486ms to 285ms; the old CPU
  profile was mostly exec.LookPath/GetFileAttributesEx;
- warm ops ~10ms either way.
With WSL agents on, the first op costs ~3.0s on both builds, about 2.7s of
it in wsl.exe list and probe spawns. This commit doesn't change that. On the
box, wsl -l -q, wsl -l --running -q and a docker-desktop probe each take
40-75ms, so a failed-probe retry every 10 min doesn't explain the report
there. The 5-8s after 10 min idle wasn't reproduced: ops after 4, 8 and 12
min idle took ~120ms.

Run: go test ./internal/proc ./internal/agent ./internal/library (ok);
gofmt -l; GOOS=linux and GOOS=windows go vet on the three packages.
I broke the fix by hand (look.go/find.go back to exec.LookPath, mayBeIn
check off), and TestMissingCommandsReadEachFolderOnce failed: "100 missing
commands were looked for on PATH 200 times, want 0".
2026-10-14 08:08:50 +08:00

112 lines
3.2 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package proc
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"runtime"
"testing"
)
// countLooks counts the commands looked for on PATH for real, from here on.
func countLooks(t *testing.T) *int {
n := 0
old := execLookPath
execLookPath = func(name string) (string, error) { n++; return old(name) }
t.Cleanup(func() { execLookPath = old })
return &n
}
// A command that isn't installed is answered from each PATH folder's
// listing, read once: before, each was a stat of every name it could have
// in every folder, asked again for about a hundred agents whenever any
// folder changed, and the Library's first change after a start or an idle
// spell took 5–8s on Windows (#1624, Hipple).
func TestMissingCommandsReadEachFolderOnce(t *testing.T) {
var dirs []string
for i := 0; i < 40; i++ {
d := t.TempDir()
os.WriteFile(filepath.Join(d, fmt.Sprintf("tool%d.exe", i)), []byte("x"), 0o755)
dirs = append(dirs, d)
}
t.Setenv("PATH", joinList(dirs))
t.Setenv("NoDefaultCurrentDirectoryInExePath", "1")
lookWindowFor(t, lookFresh)
looks := countLooks(t)
read := listingRead
for i := 0; i < 100; i++ {
name := fmt.Sprintf("magpie-missing-%d", i)
if p, err := Look(name); err == nil {
t.Fatalf("%s found at %s", name, p)
} else if !isNotFound(err) {
t.Fatalf("%s: %v, want exec.ErrNotFound", name, err)
}
if p := FindTool(name); p != "" {
t.Fatalf("FindTool %s = %s", name, p)
}
}
if *looks != 0 {
t.Errorf("100 missing commands were looked for on PATH %d times, want 0", *looks)
}
if got := listingRead - read; got > len(dirs)+len(findDirs())+1 {
t.Errorf("read %d folders for 100 commands, want each of the %d once", got, len(dirs))
}
}
// One that is there, under any of its extensions or another case, is
// looked for as before.
func TestCommandListedIsLookedFor(t *testing.T) {
d := t.TempDir()
t.Setenv("PATH", d)
lookWindowFor(t, 0)
want := installTool(t, d, "magpie-fake-agent")
looks := countLooks(t)
if p, err := Look("magpie-fake-agent"); err != nil || p != want {
t.Fatalf("Look = %q, %v; want %q", p, err, want)
}
if *looks != 1 {
t.Fatalf("looked %d times, want 1", *looks)
}
for _, n := range []string{"magpie-fake-agent.cmd", "Magpie-Fake-Agent.EXE"} {
os.WriteFile(filepath.Join(d, n), nil, 0o755)
if !mayBeIn("magpie-fake-agent", []string{d}) {
t.Errorf("with %s there, magpie-fake-agent isn't thought to be", n)
}
}
}
// A folder that can't be read may hold the command: it is looked for.
func TestUnreadableFolderMayHoldIt(t *testing.T) {
if runtime.GOOS == "windows" || os.Getuid() == 0 {
t.Skip("needs a folder this user can't list")
}
d := t.TempDir()
installTool(t, d, "magpie-fake-agent")
if err := os.Chmod(d, 0o311); err != nil { // searchable, not listable
t.Fatal(err)
}
t.Cleanup(func() { os.Chmod(d, 0o755) })
t.Setenv("PATH", d)
lookWindowFor(t, 0)
if _, err := Look("magpie-fake-agent"); err != nil {
t.Fatalf("a command in a folder that can't be listed wasn't found: %v", err)
}
}
func joinList(dirs []string) string {
s := ""
for i, d := range dirs {
if i > 0 {
s += string(os.PathListSeparator)
}
s += d
}
return s
}
func isNotFound(err error) bool {
e, ok := err.(*exec.Error)
return ok && e.Err == exec.ErrNotFound
}