| 1 | package sessioncatalog |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "fmt" |
| 6 | "path/filepath" |
| 7 | "testing" |
| 8 | |
| 9 | "reasonix/internal/agent" |
| 10 | ) |
| 11 | |
| 12 | // BenchmarkRepairWave1056 measures the persistent due query, 64-row result |
| 13 | // batches, aggregate publication, and per-directory reconcile coalescing. The |
| 14 | // filesystem decode is replaced by a deterministic result so this benchmark |
| 15 | // isolates catalog scheduling cost. |
| 16 | func BenchmarkRepairWave1056(b *testing.B) { |
| 17 | ctx := context.Background() |
| 18 | dir := b.TempDir() |
| 19 | catalog, err := Open(ctx, Options{InMemory: true, DisableRepair: true}) |
| 20 | if err != nil { |
| 21 | b.Fatal(err) |
| 22 | } |
| 23 | b.Cleanup(func() { _ = catalog.Close(context.Background()) }) |
| 24 | records := make([]SessionRecord, 1056) |
| 25 | for i := range records { |
| 26 | records[i] = SessionRecord{ |
| 27 | Path: filepath.Join(dir, fmt.Sprintf("%04d.jsonl", i)), Directory: dir, Scope: "global", |
| 28 | TurnsState: TurnsUnknown, Health: HealthOK, LastActivityAt: int64(1056 - i), |
| 29 | } |
| 30 | } |
| 31 | if _, err := catalog.upsertSessionsWithNotification(ctx, records, nil, "seed", false, upsertDirectoryProjection); err != nil { |
| 32 | b.Fatal(err) |
| 33 | } |
| 34 | catalog.testRepairSessionHook = func(context.Context, string) (agent.SessionListingRepairResult, error) { |
| 35 | return agent.SessionListingRepairResult{Status: agent.SessionListingRepairApplied, Preview: "ok", Turns: 1}, nil |
| 36 | } |
| 37 | lock := catalog.directoryLock(dir) |
| 38 | lock.Lock() |
| 39 | defer lock.Unlock() |
| 40 | |
| 41 | b.ReportAllocs() |
| 42 | b.ResetTimer() |
| 43 | for range b.N { |
| 44 | b.StopTimer() |
| 45 | if _, err := catalog.db.ExecContext(ctx, `UPDATE catalog_sessions SET turns_state='unknown',repair_state='pending', |
| 46 | repair_attempts=0,repair_retry_at=0,repair_error_kind=''`); err != nil { |
| 47 | b.Fatal(err) |
| 48 | } |
| 49 | b.StartTimer() |
| 50 | catalog.runRepairWave(ctx) |
| 51 | } |
| 52 | } |
| 53 |