using System.Diagnostics; using System.Text; using Titano.Data.Archiving; using Titano.Data.Tests.Archiving; using Titano.Testing; namespace Titano.Data.Tests.Perf; /// /// Phase 2 gate, §1.1 scaled: a 100,000-frame archive adopted from real files, then its index thrown away and rebuilt /// from the sidecars alone. The rebuilt index must equal the original row for row, within a fifth of the 10-minute /// budget §1.1 gives 500,000 photos. Opt-in: TITANO_PERF=1. /// [Category("perf")] [RequiresEnvironment(PerfGate.Variable)] [Serial] [Timeout(3_600_000)] public sealed class RebuildPerfTests(TestContext context) { public const int Sequences = 40; public const int FramesPerSequence = 2_500; public const double RebuildBudgetSeconds = 600.0 * Sequences * FramesPerSequence / 500_000; [Test] public async Task AHundredThousandFrameArchiveRebuildsFromSidecarsAlone() { await using var fx = await ArchiveFixture.CreateAsync(context); var watch = Stopwatch.StartNew(); Parallel.For(0, Sequences, new ParallelOptions { CancellationToken = fx.Ct }, s => { var start = ArchiveFixture.Start.AddDays(3 * s); var folder = Path.Combine(fx.Paths.Root, start.Year.ToString(System.Globalization.CultureInfo.InvariantCulture), $"{start:yyyy-MM-dd} Roof {s:00}", "originals"); Photos.WriteSeries(folder, start, FramesPerSequence, firstSeed: 1 + s * FramesPerSequence); }); context.Log($"perf: wrote {Sequences * FramesPerSequence:N0} photos in {watch.Elapsed.TotalSeconds:F1} s"); watch.Restart(); var adopted = await fx.ScanAsync(ScanMode.Full); context.Log($"perf: adopted {adopted.Adopted} folders in {watch.Elapsed.TotalSeconds:F1} s"); Assert.Equal(Sequences, adopted.Adopted); Assert.Empty(adopted.Failed); var original = await DumpAsync(fx); // With the index snapshot the full scan left behind, then from the sidecar files alone (snapshot lost). foreach (var snapshot in new[] { true, false }) { if (!snapshot) File.Delete(fx.Paths.SnapshotPath); await fx.ReopenWithEmptyIndexAsync(); watch.Restart(); var rebuilt = await fx.ScanAsync(ScanMode.Rebuild); var seconds = watch.Elapsed.TotalSeconds; context.Log($"perf: rebuilt {Sequences * FramesPerSequence:N0} frames {(snapshot ? "with" : "without")} the snapshot in {seconds:F1} s (budget {RebuildBudgetSeconds:F0} s)"); Assert.Equal((Sequences, 0), (rebuilt.Indexed, rebuilt.Adopted)); Assert.Equal(original, await DumpAsync(fx)); Assert.Less(seconds, RebuildBudgetSeconds, "rebuild from sidecars"); } } /// Every sequence row and every frame row (paged), in folder and index order. private static async Task DumpAsync(ArchiveFixture fx) { var sb = new StringBuilder(); var frames = 0; foreach (var location in await fx.Db.Sequences.ListLocationsAsync(fx.Archive.Id, fx.Ct)) { var s = (await fx.Db.Sequences.GetAsync(location.Id, fx.Ct))!; sb.Append($"S {s.Id} {s.Folder} '{s.Title}' {s.CameraModel} {s.FirstCapture:O} {s.FrameCount} {s.TotalBytes} {s.Status} ") .AppendLine($"{s.VerifiedAt:O} {string.Join('|', s.Tags)} {s.SettingsVersion}/{s.FramesVersion} {s.MedianInterval}"); var after = -1; while (true) { var page = await fx.Db.Frames.ListBySequenceAsync(s.Id, after, 1000, fx.Ct); if (page.Count == 0) break; foreach (var f in page) { sb.AppendLine($" F {f.Id} {f.Index} {f.RelativePath} {f.OriginalFileName} {f.Size} {f.Sha256} {f.CapturedAt:O} {f.Width}x{f.Height} {f.Format} {f.Flags}"); frames++; } after = page[^1].Index; } } Assert.Equal(Sequences * FramesPerSequence, frames); return sb.ToString(); } }