Files
Chronos/tests/Titano.Data.Tests/Perf/SyntheticLibrary.cs
T

119 lines
5.7 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). Titles, cameras, dates, tags and flags follow the
/// seed; ids and hashes are random.
/// </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 => Frames(s, seed)), 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)
{
var random = new Random(HashCode.Combine(seed, sequence.Id));
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,
};
}
}
}