using Titano.Core.Model;
using Titano.Data.Repositories;
using Titano.Data.Tests.Infrastructure;
using Titano.Testing;
namespace Titano.Data.Tests.Perf;
///
/// ยง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 a cold index, and every filter, search and
/// sort answers its first page plus the total in under 200 ms. Opt-in: TITANO_PERF=1.
///
[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 page cache, as at application start.
var db = Own(await TitanoDatabase.OpenAsync(options, Ct));
var cold = await PerfGate.TimeAsync(() => OpenLibraryAsync(db));
var warm = await PerfGate.MedianAsync(() => OpenLibraryAsync(db));
Context.Log($"perf: {Dialect} library open {cold:F0} ms cold, {warm:F0} ms warm (budget {OpenBudgetMs:F0} ms)");
var failures = new List();
if (cold > OpenBudgetMs) failures.Add($"open {cold: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));
}
/// What the Library screen loads when it opens: totals and filter lists, then the first page.
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;
}