Files
Chronos/tests/Chronos.Data.Tests/Processing/DecodeParallelismTests.cs
T
Alby96andClaude Opus 5.5 f2b58522b9
CI / Build and test (push) Successful in 8m14s
CI / Windows tests and installer (push) Skipped
CI / YAML check (push) Successful in 7s
Release / Build, test and publish (push) Successful in 13m19s
CI / PostgreSQL 16 integration (push) Successful in 4m49s
CI / PostgreSQL 17 integration (push) Successful in 5m28s
CI / Docker image (push) Successful in 53s
Release / Server image (push) Failing after 10s
refactor!: rename Titano to Chronos and move CI and releases to Gitea (#6)
The owner renamed the product and moved the repository to the self-hosted
Gitea (https://gitea.encke-hake.ts.net/BalboSoft/Chronos) for more control,
and asked for Actions and a first automatic release with the server package
and the Windows exe (docs/requests/2026-10-23-chronos-gitea.md).

- Chronos everywhere: a byte-exact rename of contents and paths, then a
  per-language review of the forms the old name's ending had dictated.
- Gitea instead of GitHub: the update check, licence, issue and support
  links read ChronosReleasesUrl; the update check takes the highest stable
  version, offers a pre-release build its final release and reports a
  repository it cannot see as a failed check. EULA, security policy and
  Gitea issue forms live at the repository root.
- Gitea Actions for the Linux act_runner (.gitea/workflows): CI, release,
  nightly image and performance gates in the .NET SDK container with git
  checkout and the system SQLite. The release cross-compiles the Windows
  programs, checks their icon, version and DPI manifest, packs installers
  and zips, publishes the Gitea release and pushes the server image
  (REGISTRY_TOKEN). Windows-only tests run in an opt-in job on a Windows
  runner (WINDOWS_RUNNER).
- New Chronos.Tools commands: zip, gitea-release, postgres-ci (replaces the
  docker-exec script, which Gitea's service containers do not allow; the
  throw-away server runs without durability) and pe-check.
- Timed tests take their budget from TestTiming (CHRONOS_TEST_TIME_FACTOR,
  3 on the shared runner; the weekly performance gates keep theirs).

BREAKING CHANGE: programs, settings, data folders, file types, environment
variables and the Docker image use the new name; nothing is migrated from
Titano, which never had a release.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: Alberto Balbo <alberto.balbo.96@gmail.com>
2026-10-23 12:45:18 +02:00

177 lines
11 KiB
C#

using Chronos.Core.Analysis.Deep;
using Chronos.Core.Archiving;
using Chronos.Core.Imaging;
using Chronos.Core.Imaging.Decoders;
using Chronos.Core.Model;
using Chronos.Data.Archiving;
using Chronos.Data.Processing;
using Chronos.Testing;
using SequenceFrames = Chronos.Data.Processing.SequenceFrames;
namespace Chronos.Data.Tests.Processing;
/// <summary>
/// Photos decoded at once (owner review 1, R2): the whole parallelism, unless developing large originals whole would
/// exceed the memory budget. Only the decodes that develop an original count: a JPEG at least twice the size asked for
/// decodes scaled down, and a frame read through its proxy or an embedded preview of that size needs no development.
/// </summary>
public sealed class DecodeParallelismTests
{
/// <summary>2 GiB: two 24 MP developments (some 768 MB each), one of 45 MP.</summary>
private const long Budget = 2L << 30;
private static readonly ImageDecoder Server = DecoderFactory.CreateServer();
[Test]
public void WhatADecodeDevelopsWholeDependsOnTheFormatTheSizeAskedForAndThePreview()
{
const int w = 6000, h = 4000;
Assert.Equal(0, DecodeBudget.Developed(ImageFileFormat.Jpeg, w, h, 0, 320, preferPreview: false));
Assert.Equal(0, DecodeBudget.Developed(ImageFileFormat.Jpeg, w, h, 0, 3000, preferPreview: false));
// A 4K render of a 24 MP JPEG decodes it nearly whole; full size (0) always does.
Assert.Equal(24_000_000, DecodeBudget.Developed(ImageFileFormat.Jpeg, w, h, 0, 3840, preferPreview: false));
Assert.Equal(24_000_000, DecodeBudget.Developed(ImageFileFormat.Jpeg, w, h, 0, 0, preferPreview: true));
// A preview reaching the size serves only decodes that prefer previews; a render develops the RAW.
Assert.Equal(0, DecodeBudget.Developed(ImageFileFormat.Cr2, w, h, 1620, 512, preferPreview: true));
Assert.Equal(24_000_000, DecodeBudget.Developed(ImageFileFormat.Cr2, w, h, 1620, 512, preferPreview: false));
Assert.Equal(24_000_000, DecodeBudget.Developed(ImageFileFormat.Cr2, w, h, 1620, 2048, preferPreview: true));
Assert.Equal(24_000_000, DecodeBudget.Developed(ImageFileFormat.Tiff, w, h, 0, 320, preferPreview: true));
// Only RAW decoders take a preview by choice: a TIFF's or a HEIF's is never a shortcut.
Assert.Equal(24_000_000, DecodeBudget.Developed(ImageFileFormat.Tiff, w, h, w, 512, preferPreview: true));
Assert.Equal(24_000_000, DecodeBudget.Developed(ImageFileFormat.Heif, w, h, 512, 512, preferPreview: true));
Assert.Equal(0, DecodeBudget.Developed(ImageFileFormat.Dng, w, h, 1024, 512, preferPreview: true));
Assert.Equal(DecodeBudget.UnknownPixels, DecodeBudget.Developed(ImageFileFormat.Png, 0, 0, 0, 320, preferPreview: true));
}
[Test]
public void ThumbnailAndProxyPassesHoldBackOnlyForTheOriginalsTheyDevelop()
{
static SidecarFrame Frame(ImageFileFormat format, int width, int height, int? preview = null) =>
new() { Id = Guid.NewGuid(), File = "f", Format = format, W = width, H = height, PreviewW = preview, PreviewH = preview * 2 / 3 };
static int Thumbnails(params SidecarFrame[] frames) => ArchiveImages.PassParallelism(8, Budget, frames, 320, preferPreview: true, Server);
static int Proxies(int edge, params SidecarFrame[] frames) => ArchiveImages.PassParallelism(8, Budget, frames, edge, preferPreview: false, Server);
Assert.Equal(8, Thumbnails(Frame(ImageFileFormat.Jpeg, 8192, 5464), Frame(ImageFileFormat.Cr2, 8192, 5464, preview: 1620)));
// A HEIF the server reads through its proxy or preview develops nothing either.
Assert.Equal(8, Thumbnails(Frame(ImageFileFormat.Heif, 8192, 5464)));
// A 45 MP TIFF is decoded whole: one at a time within 2 GiB, whatever the JPEGs beside it.
Assert.Equal(1, Thumbnails(Frame(ImageFileFormat.Jpeg, 8192, 5464), Frame(ImageFileFormat.Tiff, 8192, 5464)));
// A RAW whose preview is smaller than the thumbnail is developed; a size nobody knows counts as 24 MP.
Assert.Equal(2, Thumbnails(Frame(ImageFileFormat.Cr2, 6000, 4000, preview: 160)));
Assert.Equal(2, Thumbnails(Frame(ImageFileFormat.Png, 0, 0)));
Assert.Equal(1, ArchiveImages.PassParallelism(0, Budget, [Frame(ImageFileFormat.Jpeg, 48, 32)], 320, preferPreview: true, Server));
// Proxies of 2048 px scale JPEGs down but develop every RAW, preview or not; full-size proxies decode JPEGs whole.
Assert.Equal(8, Proxies(2048, Frame(ImageFileFormat.Jpeg, 8192, 5464)));
Assert.Equal(2, Proxies(2048, Frame(ImageFileFormat.Cr2, 6000, 4000, preview: 6000)));
Assert.Equal(2, Proxies(0, Frame(ImageFileFormat.Jpeg, 6000, 4000)));
}
[Test]
public void ADeepAnalysisHoldsBackOnlyForTheOriginalsItDevelops()
{
var analysis = DeepAnalysisOptions.Default with { MaxDegreeOfParallelism = 8 };
Assert.Equal(8, SequenceAnalyser.AnalysisParallelism(analysis, Frames((ImageFileFormat.Jpeg, 8192, 5464, 0)), Budget));
// A DNG whose embedded preview reaches the analysis size (half the long edge, at most 2048 px) develops nothing.
Assert.Equal(8, SequenceAnalyser.AnalysisParallelism(analysis, Frames((ImageFileFormat.Dng, 5568, 4872, 2784)), Budget));
Assert.Equal(1, SequenceAnalyser.AnalysisParallelism(analysis, Frames((ImageFileFormat.Dng, 5568, 4872, 1024), (ImageFileFormat.Tiff, 8192, 5464, 0)), Budget));
// Frames read through their proxies decode as JPEGs.
Assert.Equal(8, SequenceAnalyser.AnalysisParallelism(analysis, Frames((ImageFileFormat.Tiff, 8192, 5464, 0)) with { Proxied = [true] }, Budget));
}
[Test]
public async Task AWindowHoldsBackWheneverItsDecodesDevelopOriginals()
{
var window = new FrameWindowOptions { MemoryBudget = Budget, MaxConcurrentLoads = 8, Ahead = 8 };
var tiffs = Frames((ImageFileFormat.Tiff, 8192, 5464, 0), (ImageFileFormat.Tiff, 8192, 5464, 0));
// A render node renders downloaded originals without counting what they decode (Originals unset): still held back.
await using (var node = new SequenceFrameSource(tiffs, tiffs.LightFrames(), Server, window))
{
Assert.Equal(1, node.WindowOptions(1920, DecodePurpose.Render).MaxConcurrentLoads);
// A preparation pass decodes TIFFs whole too.
Assert.Equal(1, node.WindowOptions(512, DecodePurpose.Analysis).MaxConcurrentLoads);
Assert.Equal((Budget, 8), (node.WindowOptions(1920, DecodePurpose.Render).MemoryBudget, node.WindowOptions(1920, DecodePurpose.Render).Ahead));
}
// RAWs whose previews reach the preparation's size: the preparation runs them all, the render develops them.
var raws = Frames((ImageFileFormat.Cr2, 6000, 4000, 1620), (ImageFileFormat.Cr2, 6000, 4000, 1620));
await using (var raw = new SequenceFrameSource(raws, raws.LightFrames(), Server, window) { Originals = true })
{
Assert.Equal(8, raw.WindowOptions(512, DecodePurpose.Analysis).MaxConcurrentLoads);
Assert.Equal(2, raw.WindowOptions(1920, DecodePurpose.Render).MaxConcurrentLoads);
}
// Proxies decode as JPEGs, flagged or found in a proxy cache's .jpg files.
await using (var proxied = new SequenceFrameSource(tiffs with { Proxied = [true, true] }, tiffs.LightFrames(), Server, window))
Assert.Equal(8, proxied.WindowOptions(1920, DecodePurpose.Render).MaxConcurrentLoads);
var cached = raws with { Files = ["cache/0001.jpg", "cache/0002.JPG"] };
await using var cache = new SequenceFrameSource(cached, cached.LightFrames(), Server, window);
Assert.Equal(8, cache.WindowOptions(1920, DecodePurpose.Render).MaxConcurrentLoads);
}
[Test]
public async Task ADemandLoadWaitsForTheReadAheadOfADevelopingWindow()
{
// The window caps its read-ahead only: a render that skips frames (a speed-up) asks for a frame the read-ahead is
// not decoding while it develops another, and both would run. One development at a time fits this budget.
var decoder = new CountingDecoder();
var tiffs = Frames([.. Enumerable.Repeat((ImageFileFormat.Tiff, 8192, 5464, 0), 8)]);
var frames = tiffs with { Files = [.. tiffs.Files.Select(f => f + ".tif")] };
var window = new FrameWindowOptions { MemoryBudget = Budget, MaxConcurrentLoads = 8, Ahead = 8 };
await using (var source = new SequenceFrameSource(frames, frames.LightFrames(), new ImageDecoder([decoder]), window))
{
Assert.Equal(1, source.DevelopingLimit(1920, DecodePurpose.Render));
foreach (var index in new[] { 0, 2, 4, 6 })
using (await source.GetFrameAsync(index, 1920, DecodePurpose.Render, CancellationToken.None)) { }
}
Assert.Equal(1, decoder.Peak);
Assert.Greater(decoder.Decodes, 4);
}
/// <summary>Decodes anything as a tiny frame, slowly, and counts how many decodes run at once.</summary>
private sealed class CountingDecoder : IImageDecoderProvider
{
private int _running;
private int _peak;
private int _decodes;
public int Peak => Volatile.Read(ref _peak);
public int Decodes => Volatile.Read(ref _decodes);
public string Name => "counting";
public DecodeTier? TierFor(ImageFileFormat format) => DecodeTier.Native;
public DecodedImage? Decode(DecodeRequest request, CancellationToken cancellationToken = default)
{
var now = Interlocked.Increment(ref _running);
int seen;
while ((seen = Volatile.Read(ref _peak)) < now && Interlocked.CompareExchange(ref _peak, now, seen) != seen)
{
}
Interlocked.Increment(ref _decodes);
Thread.Sleep(40);
Interlocked.Decrement(ref _running);
return new DecodedImage(new ImageBuffer(8, 6), DecodeTier.Native, Name, 8192, 5464);
}
}
private static SequenceFrames Frames(params (ImageFileFormat Format, int Width, int Height, int Preview)[] specs)
{
var sequence = new Sequence { Id = Guid.NewGuid(), ArchiveId = Guid.NewGuid(), Folder = "a" };
var frames = specs.Select((s, i) => new Frame
{
Id = Guid.NewGuid(),
SequenceId = sequence.Id,
Index = i,
RelativePath = $"a/IMG_{i:0000}",
FileName = $"IMG_{i:0000}",
CapturedAt = new DateTime(2026, 10, 16, 6, 0, 0).AddSeconds(10 * i),
Width = s.Width,
Height = s.Height,
Format = s.Format,
PreviewWidth = s.Preview,
PreviewHeight = s.Preview * 2 / 3,
}).ToList();
return new SequenceFrames(sequence, frames, [.. frames.Select(f => (string?)f.RelativePath)]);
}
}