using System.Diagnostics; using Chronos.Core.Imaging; using Chronos.Core.Model; using Chronos.Testing; namespace Chronos.Data.Tests.Perf; /// /// 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. /// 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}", }; /// Writes the library into and returns its archive. public static async Task BuildAsync(ChronosDatabase 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(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 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, }; } } }