Red on ubuntu CI at4a9cfb52e(run 41865226839) and on the Linux box alone: "listing 3000 unchanged files took 12159 allocations", bar 3n = 9000. Cause: dirent.Stats reads a folder's stats in one call on macOS (getattrlistbulk) and has no such call elsewhere, so on Linux statFiles stats each file alone through statOne (os.Stat), two allocations a file: 6000 for the test's 3000 files. The names are kept the same on both systems. A probe of the second listing (not committed) gave Linux 12115 allocations, 6110 with statOne answered without allocating, and macOS 6128 and 6124; one os.Stat is 2 allocations on each. The 3n bar was set on macOS and charged Linux's stats to the names. Change (test only): the measured listing's statOne is answered from a table of os.Stat read before it, so what is counted is the listing's own work on every system. The bar is unchanged. What was run, at this tree on origin/mainfd85c0a1e: - Linux (wsl-run, magpie-desktop, go1.26.8, -tags nogui): the old test red, 12159 allocations. The new test ok. The new test with ccNames.of never taking the last listing's names (broken by hand): red, 12159. - macOS (mote): the new test ok; with the names broken by hand red, 12198. - go test -count=3 ./internal/sessions: ok on Linux (-tags nogui) and macOS. gofmt -l clean; go vet, GOOS=linux go vet -tags nogui, GOOS=windows go vet of internal/sessions: clean. Not tried: Windows (no bulk stat there either, so it would have been red the same way; Windows CI only compiles tests). Not changed: Linux still pays os.Stat's two allocations a session file each poll; that cost is the system's stat, not this test's guarantee.
110 lines
3.6 KiB
Go
110 lines
3.6 KiB
Go
package sessions
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"reflect"
|
|
"runtime"
|
|
"testing"
|
|
)
|
|
|
|
// TestCCListingKeepsItsNames: each Sessions poll made every Claude Code
|
|
// file's path and key again, ~10 MB of a poll over ~15k sessions, though
|
|
// the listing is the same but for a file or two. A listing like the last
|
|
// makes none of them; a file new, gone or moved since is listed as a
|
|
// fresh listing lists it.
|
|
func TestCCListingKeepsItsNames(t *testing.T) {
|
|
dir := t.TempDir()
|
|
write := func(rel string) {
|
|
t.Helper()
|
|
p := filepath.Join(dir, "projects", rel)
|
|
if err := os.MkdirAll(filepath.Dir(p), 0o755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.WriteFile(p, []byte("{}\n"), 0o644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
const n = 3000
|
|
for i := range n {
|
|
write(fmt.Sprintf("-Users-someone-work-a-project-p%d/%08d-1111-2222-3333-444444444444.jsonl", i%3, i))
|
|
}
|
|
// a session's folder, sorted between files, with its subagents
|
|
write("-Users-someone-work-a-project-p1/00000500-aaaa/subagents/agent-1.jsonl")
|
|
write("-Users-someone-work-a-project-p1/00000500-aaaa/subagents/workflows/run-1/agent-2.jsonl")
|
|
write("-Users-someone-work-a-project-p1/00000500-aaaa/subagents/workflows/run-1/note.jsonl")
|
|
ccListed.Clear()
|
|
t.Cleanup(func() { ccListed.Clear() })
|
|
fresh := func() []file {
|
|
ccListed.Clear()
|
|
return listCCFiles("claude", dir)
|
|
}
|
|
same := func(what string, got []file) {
|
|
t.Helper()
|
|
want := fresh()
|
|
if !reflect.DeepEqual(got, want) {
|
|
t.Fatalf("%s: %d files, a fresh listing %d", what, len(got), len(want))
|
|
}
|
|
}
|
|
|
|
first := listCCFiles("claude", dir)
|
|
if len(first) != n+2 {
|
|
t.Fatalf("listed %d files, want %d", len(first), n+2)
|
|
}
|
|
// What a file's stat costs is the system's, not the names': macOS
|
|
// reads a folder's in one call (dirent.Stats), elsewhere each file is
|
|
// stat'ed alone, os.Stat's two allocations a file, which counted
|
|
// against this bar on Linux (#4925). Those asked alone are answered
|
|
// here from what was read before.
|
|
stated := map[string]os.FileInfo{}
|
|
for _, f := range first {
|
|
fi, err := os.Stat(f.path)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
stated[f.path] = fi
|
|
}
|
|
was := statOne
|
|
t.Cleanup(func() { statOne = was })
|
|
statOne = func(path string, follow bool) (os.FileInfo, error) {
|
|
if fi, ok := stated[path]; ok {
|
|
return fi, nil
|
|
}
|
|
return was(path, follow)
|
|
}
|
|
runtime.GC()
|
|
var before, after runtime.MemStats
|
|
runtime.ReadMemStats(&before)
|
|
again := listCCFiles("claude", dir)
|
|
runtime.ReadMemStats(&after)
|
|
statOne = was
|
|
if !reflect.DeepEqual(again, first) {
|
|
t.Fatal("an unchanged folder listed otherwise")
|
|
}
|
|
// a path and a key a file made again are 2n more
|
|
if m := after.Mallocs - before.Mallocs; m > 3*n {
|
|
t.Fatalf("listing %d unchanged files took %d allocations", n, m)
|
|
}
|
|
|
|
write("-p0/00000001-new.jsonl")
|
|
same("a file new", listCCFiles("claude", dir))
|
|
listCCFiles("claude", dir)
|
|
os.Remove(filepath.Join(dir, "projects", "-Users-someone-work-a-project-p0", "00000003-1111-2222-3333-444444444444.jsonl"))
|
|
os.Remove(filepath.Join(dir, "projects", "-Users-someone-work-a-project-p2", "00000005-1111-2222-3333-444444444444.jsonl"))
|
|
os.Remove(filepath.Join(dir, "projects", "-Users-someone-work-a-project-p2", "00000008-1111-2222-3333-444444444444.jsonl"))
|
|
same("files gone", listCCFiles("claude", dir))
|
|
listCCFiles("claude", dir)
|
|
os.RemoveAll(filepath.Join(dir, "projects", "-Users-someone-work-a-project-p1"))
|
|
write("-p9/00000500-aaaa/subagents/agent-1.jsonl")
|
|
same("a folder gone and one new", listCCFiles("claude", dir))
|
|
listCCFiles("claude", dir)
|
|
// the same names under another agent's key
|
|
got := listCCFiles("qoder", dir)
|
|
for _, f := range got {
|
|
if f.key[:6] != "qoder:" {
|
|
t.Fatalf("key %q under qoder", f.key)
|
|
}
|
|
}
|
|
}
|