| 1 | package sessioncatalog |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "fmt" |
| 6 | "os" |
| 7 | "path/filepath" |
| 8 | "testing" |
| 9 | "time" |
| 10 | ) |
| 11 | |
| 12 | // BenchmarkReconcileDirectory10K measures the full incremental reconcile, not |
| 13 | // the unchanged-signature fast path. File creation and the initial snapshot are |
| 14 | // excluded so the result can be compared directly with the pre-atomic code. |
| 15 | func BenchmarkReconcileDirectory10K(b *testing.B) { |
| 16 | ctx := context.Background() |
| 17 | dir := b.TempDir() |
| 18 | paths := make([]string, 10_000) |
| 19 | for i := range paths { |
| 20 | paths[i] = filepath.Join(dir, fmt.Sprintf("session-%05d.jsonl", i)) |
| 21 | if err := os.WriteFile(paths[i], []byte("{}\n"), 0o600); err != nil { |
| 22 | b.Fatal(err) |
| 23 | } |
| 24 | } |
| 25 | catalog, err := Open(ctx, Options{Path: filepath.Join(b.TempDir(), "catalog.sqlite"), DisableRepair: true}) |
| 26 | if err != nil { |
| 27 | b.Fatal(err) |
| 28 | } |
| 29 | b.Cleanup(func() { _ = catalog.Close(context.Background()) }) |
| 30 | target := DirectoryTarget{Path: dir, Scope: "project", WorkspaceRoot: "/workspace"} |
| 31 | if err := catalog.ReconcileDirectory(ctx, target); err != nil { |
| 32 | b.Fatal(err) |
| 33 | } |
| 34 | |
| 35 | b.ResetTimer() |
| 36 | for i := range b.N { |
| 37 | b.StopTimer() |
| 38 | stamp := time.Unix(1_800_000_000+int64(i), 0) |
| 39 | if err := os.Chtimes(paths[i%len(paths)], stamp, stamp); err != nil { |
| 40 | b.Fatal(err) |
| 41 | } |
| 42 | b.StartTimer() |
| 43 | if err := catalog.ReconcileDirectory(ctx, target); err != nil { |
| 44 | b.Fatal(err) |
| 45 | } |
| 46 | } |
| 47 | } |
| 48 |