Files
Chronos/tests/Titano.Data.Tests/Perf/SyntheticLibrary.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

120 lines
5.8 KiB
C#

using System.Diagnostics;
using Titano.Core.Imaging;
using Titano.Core.Model;
using Titano.Testing;
namespace Titano.Data.Tests.Perf;
/// <summary>
/// The §1.1 library: 2,000 sequences of 250 photos (500,000 frames) over twelve years, a dozen cameras, sixty tags and
/// mixed status flags, written straight into the index (no files). Everything but ids and hashes follows the seed, so
/// every run measures the same library.
/// </summary>
internal static class SyntheticLibrary
{
public const int Sequences = 2_000;
public const int FramesPerSequence = 250;
public const int Photos = Sequences * FramesPerSequence;
public static readonly (string Make, string Model)[] Cameras =
[
("Canon", "Canon EOS R5"), ("Canon", "Canon EOS 5D Mark IV"), ("Canon", "Canon EOS R6"), ("NIKON CORPORATION", "NIKON Z 7_2"),
("NIKON CORPORATION", "NIKON D850"), ("SONY", "ILCE-7M4"), ("SONY", "ILCE-7RM5"), ("FUJIFILM", "X-T5"), ("FUJIFILM", "GFX100S"),
("Panasonic", "DC-S5M2"), ("OM Digital Solutions", "OM-1"), ("GoPro", "HERO12 Black"),
];
private static readonly string[] Subjects =
["Harbour", "Milky Way", "Storm front", "Construction site", "City lights", "Glacier", "Sunrise", "Aurora", "Harvest", "Tide", "Fog", "Market"];
private static readonly string[] Places =
["Dolomites", "Trieste", "Lofoten", "Atacama", "Iceland", "Rome", "Garda", "Etna", "Patagonia", "Namib", "Kyoto", "Sardinia"];
private static readonly ImageFileFormat[] Formats = [ImageFileFormat.Cr3, ImageFileFormat.Nef, ImageFileFormat.Arw, ImageFileFormat.Jpeg, ImageFileFormat.Raf, ImageFileFormat.Dng];
public static string Tag(int i) => i switch
{
0 => "milky way",
1 => "harbour",
2 => "client",
_ => $"tag {i:00}",
};
/// <summary>Writes the library into <paramref name="db"/> and returns its archive.</summary>
public static async Task<Archive> BuildAsync(TitanoDatabase db, TestContext context, int seed = 7)
{
var watch = Stopwatch.StartNew();
var archive = new Archive { Id = Guid.CreateVersion7(), Name = "Perf", RootPath = "/archive/perf", Created = DateTimeOffset.UtcNow };
await db.Archives.UpsertAsync(archive, context.Cancellation);
var random = new Random(seed);
var sequences = new List<Sequence>(Sequences);
for (var i = 0; i < Sequences; i++)
{
var (make, model) = Cameras[random.Next(Cameras.Length)];
var first = new DateTime(2015, 1, 1).AddDays(random.Next(12 * 365)).AddMinutes(random.Next(24 * 60));
var interval = 2 + random.Next(30);
var status = (random.Next(4) > 0 ? SequenceStatus.AnalyzedLight : 0) | (random.Next(3) == 0 ? SequenceStatus.ProxiesBuilt : 0)
| (random.Next(2) == 0 ? SequenceStatus.Verified : 0) | (random.Next(10) == 0 ? SequenceStatus.HasWarnings : 0);
var tags = Enumerable.Range(0, random.Next(5)).Select(_ => Tag(random.Next(60))).Distinct().ToArray();
sequences.Add(new Sequence
{
Id = Guid.CreateVersion7(),
ArchiveId = archive.Id,
Title = random.Next(8) == 0 ? null : $"{Subjects[random.Next(Subjects.Length)]} {Places[random.Next(Places.Length)]} {i}",
Folder = $"{first.Year}/{first:yyyy-MM-dd}_{i:0000}",
CameraMake = make,
CameraModel = model,
FirstCapture = first,
LastCapture = first.AddSeconds(interval * (FramesPerSequence - 1)),
FrameCount = FramesPerSequence,
MedianInterval = interval,
TotalBytes = FramesPerSequence * 25_000_000L,
Status = status,
Rating = random.Next(6),
Tags = tags,
UpdatedAt = DateTimeOffset.UtcNow,
});
}
foreach (var s in sequences) await db.Sequences.UpsertAsync(s, context.Cancellation);
await db.Frames.BulkInsertAsync(sequences.SelectMany((s, i) => Frames(s, seed, i)), context.Cancellation);
context.Log($"perf: built {Sequences:N0} sequences / {Photos:N0} photos in {watch.Elapsed.TotalSeconds:F1} s");
return archive;
}
private static IEnumerable<Frame> Frames(Sequence sequence, int seed, int index)
{
// Seeded by the sequence's position, not its random id, so every run builds the same frames.
var random = new Random(unchecked((seed * 100_003) + index));
var format = Formats[random.Next(Formats.Length)];
for (var i = 0; i < FramesPerSequence; i++)
{
var flags = random.Next(2_000) switch
{
0 => FrameFlags.Missing,
1 => FrameFlags.Corrupt,
2 or 3 => FrameFlags.Duplicate,
_ => FrameFlags.None,
};
yield return new Frame
{
Id = Guid.CreateVersion7(),
SequenceId = sequence.Id,
Index = i,
RelativePath = $"{sequence.Folder}/originals/IMG_{i:0000}.{format.ToString().ToUpperInvariant()}",
FileName = $"IMG_{i:0000}.{format.ToString().ToUpperInvariant()}",
Size = 20_000_000 + random.Next(10_000_000),
Sha256 = Convert.ToHexStringLower(Guid.NewGuid().ToByteArray()) + Convert.ToHexStringLower(Guid.NewGuid().ToByteArray()),
CapturedAt = sequence.FirstCapture!.Value.AddSeconds(sequence.MedianInterval!.Value * i),
TimestampSource = TimestampSource.ExifOriginal,
ExposureTime = 1 / 250.0,
FNumber = 8,
Iso = 100,
FocalLength = 24,
Width = 6000,
Height = 4000,
Format = format,
Flags = flags,
};
}
}
}