Files
Chronos/tests/Titano.Data.Tests/Perf/RebuildPerfTests.cs
T
Claude 7170943c68 test(perf): opt-in gates for the 500k-photo library and the 100k-frame rebuild
Category perf, skipped unless TITANO_PERF=1. A synthetic library of 2,000
sequences and 500,000 frames (SQLite, and PostgreSQL when TITANO_TEST_PG is
set) must open the Library in under 1 s cold and answer every filter, search,
sort and next page in under 200 ms; a 100,000-frame archive adopted from real
files must rebuild from its sidecars, with and without the index snapshot,
into the identical index within the scaled 2-minute budget. perf.yml runs
them weekly and on demand against PostgreSQL 17 and writes the timings to the
job summary.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
2026-10-02 22:35:30 +00:00

86 lines
4.0 KiB
C#

using System.Diagnostics;
using System.Text;
using Titano.Data.Archiving;
using Titano.Data.Tests.Archiving;
using Titano.Testing;
namespace Titano.Data.Tests.Perf;
/// <summary>
/// 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: <c>TITANO_PERF=1</c>.
/// </summary>
[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");
}
}
/// <summary>Every sequence row and every frame row (paged), in folder and index order.</summary>
private static async Task<string> 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();
}
}