Files
Chronos/tests/Titano.Data.Tests/Perf/LibraryPerfTests.cs
T
Claude 54fa588ecc test(perf): rebuild gate in the §1.1 shape on both dialects, every column compared
- The rebuild gate adopts 400 sequences of 250 real photos (one fifth of the
  §1.1 library, same frames per sequence) and runs on SQLite and PostgreSQL.
- The rebuilt index is compared with the original in every column of every
  sequence and frame (only UpdatedAt, the rebuild time, is left out), and the
  first differing row is named.
- Synthetic frames are seeded by the sequence's position, so every run builds
  the same library; the Library open gate says it runs on a new connection pool
  with a warm OS cache instead of calling it cold.

Measured locally (PostgreSQL 16): Library open 259 ms SQLite / 181 ms PostgreSQL,
filters <= 6.4 ms; rebuild of 100,000 frames 1.8 s SQLite / 4.7 s PostgreSQL
(budget 120 s).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
2026-10-03 10:13:06 +00:00

87 lines
4.4 KiB
C#

using Titano.Core.Model;
using Titano.Data.Repositories;
using Titano.Data.Tests.Infrastructure;
using Titano.Testing;
namespace Titano.Data.Tests.Perf;
/// <summary>
/// §1.1 / Phase 3 gate on the 500,000-photo library: the Library opens (archive totals, filter lists and the first
/// page, exactly what the Library screen asks at start) in under 1 s on its first query through a new connection pool,
/// and every filter, search and sort answers its first page plus the total in under 200 ms. The index was just written
/// by the same machine, so the OS page cache (and PostgreSQL's shared buffers) are warm: the gate covers query plans
/// and indexes, not a disk read after a reboot. Opt-in: <c>TITANO_PERF=1</c>.
/// </summary>
[Category("perf")]
[RequiresEnvironment(PerfGate.Variable)]
[Serial]
[Timeout(3_600_000)]
public abstract class LibraryPerfTests(TestContext context) : RepositoryTest(context)
{
public const double OpenBudgetMs = 1_000;
public const double FilterBudgetMs = 200;
[Test]
public async Task TheLibraryOpensAndFiltersWithinTheGates()
{
var options = await NewOptionsAsync(multiConnection: true);
await using (var build = await TitanoDatabase.OpenAsync(options, Ct)) await SyntheticLibrary.BuildAsync(build, Context);
// A new connection pool and SQLite page cache, as at application start.
var db = Own(await TitanoDatabase.OpenAsync(options, Ct));
var first = await PerfGate.TimeAsync(() => OpenLibraryAsync(db));
var again = await PerfGate.MedianAsync(() => OpenLibraryAsync(db));
Context.Log($"perf: {Dialect} library open {first:F0} ms first, {again:F0} ms again (budget {OpenBudgetMs:F0} ms)");
var failures = new List<string>();
if (first > OpenBudgetMs) failures.Add($"open {first:F0} ms");
var firstPage = await db.Sequences.ListAsync(new SequenceQuery { Limit = 200 }, Ct);
foreach (var (name, query) in Filters(firstPage.NextCursor))
{
var ms = await PerfGate.MedianAsync(async () =>
{
await db.Sequences.ListAsync(query, Ct);
if (query.Cursor is null) await db.Sequences.CountAsync(query, Ct);
});
Context.Log($"perf: {Dialect} {name} {ms:F1} ms (budget {FilterBudgetMs:F0} ms)");
if (ms > FilterBudgetMs) failures.Add($"{name} {ms:F0} ms");
}
Assert.True(failures.Count == 0, "over budget: " + string.Join(", ", failures));
}
/// <summary>What the Library screen loads when it opens: totals and filter lists, then the first page.</summary>
private async Task OpenLibraryAsync(TitanoDatabase db)
{
await db.Sequences.GetStatsAsync(null, Ct);
await db.Tags.ListAsync(Ct);
await db.Archives.ListAsync(Ct);
await db.Sequences.ListAsync(new SequenceQuery { Limit = 200 }, Ct);
}
private static IEnumerable<(string Name, SequenceQuery Query)> Filters(string? secondPage)
{
yield return ("year", new SequenceQuery { Year = 2021, Limit = 200 });
yield return ("camera", new SequenceQuery { Camera = "Canon EOS R5", Limit = 200 });
yield return ("tag", new SequenceQuery { Tag = SyntheticLibrary.Tag(0), Limit = 200 });
yield return ("status", new SequenceQuery { WithoutStatus = SequenceStatus.AnalyzedLight, Limit = 200 });
yield return ("search", new SequenceQuery { Text = "harbour", Limit = 200 });
yield return ("search-miss", new SequenceQuery { Text = "no such words", Limit = 200 });
yield return ("combined", new SequenceQuery { Year = 2024, Camera = "ILCE-7M4", WithStatus = SequenceStatus.Verified, Limit = 200 });
yield return ("sort-title", new SequenceQuery { Sort = SequenceSort.Title, Limit = 200 });
yield return ("sort-size", new SequenceQuery { Sort = SequenceSort.Size, Limit = 200 });
yield return ("sort-frames", new SequenceQuery { Sort = SequenceSort.FrameCount, Limit = 200 });
yield return ("next-page", new SequenceQuery { Limit = 200, Cursor = secondPage });
}
}
public sealed class SqlitePerfLibraryTests(TestContext context) : LibraryPerfTests(context)
{
protected override Backend Backend => Backend.SqliteFile;
}
[RequiresEnvironment(PgTestServer.Variable)]
public sealed class PostgresPerfLibraryTests(TestContext context) : LibraryPerfTests(context)
{
protected override Backend Backend => Backend.Postgres;
}