Hero: grote foto's in de achtergrond robuust en sneller, voortgang en opnieuw proberen; admin zonder inline script
- Een decode voor de hele set, per breedte eenmaal geschaald voor WebP en AVIF (6000x4000: 21,1 s -> 6,4 s per set) - Eigen limiet per set (Media:RenderSetTimeoutSeconds 600), harde limiet rond een foto, de renderer stopt nooit door een uitzondering, een mislukte foto wacht tot de volgende ronde en houdt de rij niet op - Logging per foto (start, per variant, einde, fout); voortgang per variant in de admin; nieuwe uitsnede na focuswijziging zichtbaar (oude afbeelding blijft, shimmer); knop Opnieuw proberen - Thema-script als /theme-init.js (CSP script-src 'self'), test en smoke-controle op inline scripts Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,84 @@
|
||||
using System.Diagnostics;
|
||||
using ImageMagick;
|
||||
using SlpModularCms.Core.Media;
|
||||
using Xunit.Abstractions;
|
||||
|
||||
namespace SlpModularCms.Modules.Hero.Tests;
|
||||
|
||||
/// <summary>
|
||||
/// A measurement, not a check: where the time goes when a large photo (about 6000x4000, a few MB) is cut into its set.
|
||||
/// Runs only when HERO_TIMING_BIG=1; otherwise it passes at once. Set MAGICK_THREAD_LIMIT=1 to look at a single core.
|
||||
/// </summary>
|
||||
public class HeroBigPhotoTimingTests(ITestOutputHelper output)
|
||||
{
|
||||
private static byte[] BigPhoto(int width, int height)
|
||||
{
|
||||
// Smooth gradients with a little noise: compresses like a photo (a few MB), unlike pure noise.
|
||||
using var image = new MagickImage(MagickColors.SteelBlue, (uint)width, (uint)height);
|
||||
image.AddNoise(NoiseType.Gaussian, 0.4);
|
||||
using var clouds = new MagickImage("plasma:fractal", (uint)(width / 4), (uint)(height / 4));
|
||||
clouds.Resize((uint)width, (uint)height);
|
||||
image.Composite(clouds, CompositeOperator.Blend, "40");
|
||||
image.Quality = 88;
|
||||
using var stream = new MemoryStream();
|
||||
image.Write(stream, MagickFormat.Jpeg);
|
||||
return stream.ToArray();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task MeasureASetOfABigPhoto()
|
||||
{
|
||||
if (Environment.GetEnvironmentVariable("HERO_TIMING_BIG") != "1")
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var width = int.Parse(Environment.GetEnvironmentVariable("HERO_TIMING_W") ?? "6000");
|
||||
var height = int.Parse(Environment.GetEnvironmentVariable("HERO_TIMING_H") ?? "4000");
|
||||
// Made before the processor exists: its resource limits (20 s) would stop the making of the fixture itself.
|
||||
var bytes = BigPhoto(width, height);
|
||||
using var rig = new HeroTestRig();
|
||||
if (int.TryParse(Environment.GetEnvironmentVariable("HERO_TIMING_THREADS"), out var threads))
|
||||
{
|
||||
ResourceLimits.Thread = (ulong)threads;
|
||||
output.WriteLine($"image library threads: {ResourceLimits.Thread}");
|
||||
}
|
||||
output.WriteLine($"photo {width}x{height}, {bytes.Length / 1024.0 / 1024.0:0.0} MB");
|
||||
|
||||
var watch = Stopwatch.StartNew();
|
||||
var info = await rig.Processor.InspectAsync(bytes, ImageFormat.Jpeg, default);
|
||||
output.WriteLine($"inspect: {watch.ElapsedMilliseconds} ms ({info.Width}x{info.Height})");
|
||||
|
||||
var ratios = new[] { Ratio.Wide, Ratio.Tall }
|
||||
.Select(r => new RatioPlan(r, new FocusSetting(0.5, 0.5, 1).Normalize(), VariantPlanner.DefaultWidths(r)))
|
||||
.ToList();
|
||||
var specs = VariantPlanner.Plan(info.Width, info.Height, ratios, rig.Processor.SupportedOutput);
|
||||
var byFormat = new Dictionary<ImageFormat, List<long>>();
|
||||
var total = Stopwatch.StartNew();
|
||||
foreach (var spec in specs)
|
||||
{
|
||||
watch.Restart();
|
||||
var rendered = await rig.Processor.RenderAsync(bytes, ImageFormat.Jpeg, spec, default);
|
||||
var ms = watch.ElapsedMilliseconds;
|
||||
if (!byFormat.TryGetValue(spec.Format, out var list))
|
||||
{
|
||||
byFormat[spec.Format] = list = [];
|
||||
}
|
||||
|
||||
list.Add(ms);
|
||||
output.WriteLine($" {spec.Ratio.Name,-4} {spec.Width,5} {spec.Format,-5}: {ms,6} ms, {rendered.Bytes.Length / 1024} KB");
|
||||
}
|
||||
|
||||
output.WriteLine($"per variant, one decode each: {specs.Count} variants in {total.ElapsedMilliseconds} ms");
|
||||
foreach (var (format, list) in byFormat)
|
||||
{
|
||||
output.WriteLine($" {format}: {list.Count} variants, mean {list.Average():0} ms, total {list.Sum()} ms");
|
||||
}
|
||||
|
||||
// The whole set from one decode.
|
||||
watch.Restart();
|
||||
var steps = 0;
|
||||
var set = await rig.Processor.RenderSetAsync(bytes, ImageFormat.Jpeg, specs, (_, _) => steps++, default);
|
||||
output.WriteLine($"whole set from one decode: {specs.Count} variants in {watch.ElapsedMilliseconds} ms ({steps} progress steps, {set.Sum(r => r.Bytes.Length) / 1024} KB)");
|
||||
}
|
||||
}
|
||||
@@ -202,4 +202,66 @@ public class HeroFocusSaveTests
|
||||
limits.MaxUploadBytes.Should().Be(rig.Media.MaxUploadBytes);
|
||||
limits.MaxMegapixels.Should().Be((int)(rig.Media.MaxPixels / 1_000_000));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task AnImageThatCannotBeCut_FailsWithItsId_AndTheOthersAreStillCut()
|
||||
{
|
||||
using var rig = new HeroTestRig();
|
||||
var broken = await rig.UploadWithoutRendering(HeroTestRig.Photo(seed: 1));
|
||||
var fine = await rig.UploadWithoutRendering(HeroTestRig.Photo(seed: 2));
|
||||
// The original of the first photo is gone (a lost file): cutting it cannot work.
|
||||
await rig.Store.DeleteAsync(rig.Db.HeroImages.Single(i => i.Id == Guid.Parse(broken.Id)).OriginalKey, default);
|
||||
|
||||
var failure = await Assert.ThrowsAsync<HeroImageRenderException>(() => rig.Images.RenderNextPendingAsync(new HashSet<Guid>(), default));
|
||||
failure.ImageId.Should().Be(Guid.Parse(broken.Id));
|
||||
|
||||
var handled = await rig.Images.RenderNextPendingAsync(new HashSet<Guid> { failure.ImageId }, default);
|
||||
|
||||
handled.Should().Be(Guid.Parse(fine.Id));
|
||||
HeroMapping.NeedsRender(rig.Db.HeroImages.Single(i => i.Id == Guid.Parse(fine.Id))).Should().BeFalse();
|
||||
HeroMapping.NeedsRender(rig.Db.HeroImages.Single(i => i.Id == Guid.Parse(broken.Id))).Should().BeTrue();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ASetFromOneDecode_GivesTheSameSizesAndFormats_AsCuttingEveryVariantOnItsOwn_AndReportsEveryStep()
|
||||
{
|
||||
using var rig = new HeroTestRig();
|
||||
var photo = HeroTestRig.Photo(2400, 1600);
|
||||
var info = await rig.Processor.InspectAsync(photo, SlpModularCms.Core.Media.ImageFormat.Jpeg, default);
|
||||
var ratios = new[] { SlpModularCms.Core.Media.Ratio.Wide, SlpModularCms.Core.Media.Ratio.Tall }
|
||||
.Select(r => new SlpModularCms.Core.Media.RatioPlan(r, new SlpModularCms.Core.Media.FocusSetting(0.3, 0.6, 1.5).Normalize(), SlpModularCms.Core.Media.VariantPlanner.DefaultWidths(r)))
|
||||
.ToList();
|
||||
var specs = SlpModularCms.Core.Media.VariantPlanner.Plan(info.Width, info.Height, ratios, rig.Processor.SupportedOutput);
|
||||
var steps = new List<(int Done, int Total)>();
|
||||
|
||||
var set = await rig.Processor.RenderSetAsync(photo, SlpModularCms.Core.Media.ImageFormat.Jpeg, specs, (done, total) => steps.Add((done, total)), default);
|
||||
|
||||
set.Should().HaveCount(specs.Count);
|
||||
for (var i = 0; i < specs.Count; i++)
|
||||
{
|
||||
var single = await rig.Processor.RenderAsync(photo, SlpModularCms.Core.Media.ImageFormat.Jpeg, specs[i], default);
|
||||
(set[i].Width, set[i].Height, set[i].Format).Should().Be((single.Width, single.Height, single.Format), $"variant {i}");
|
||||
}
|
||||
|
||||
steps.Should().HaveCount(specs.Count);
|
||||
steps.Select(x => x.Done).Should().BeInAscendingOrder();
|
||||
steps.Last().Should().Be((specs.Count, specs.Count));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ACancelledSet_StopsBetweenVariants()
|
||||
{
|
||||
using var rig = new HeroTestRig();
|
||||
var photo = HeroTestRig.Photo(2400, 1600);
|
||||
var info = await rig.Processor.InspectAsync(photo, SlpModularCms.Core.Media.ImageFormat.Jpeg, default);
|
||||
var ratios = new[] { new SlpModularCms.Core.Media.RatioPlan(SlpModularCms.Core.Media.Ratio.Wide, new SlpModularCms.Core.Media.FocusSetting(0.5, 0.5, 1).Normalize(), SlpModularCms.Core.Media.VariantPlanner.DefaultWidths(SlpModularCms.Core.Media.Ratio.Wide)) };
|
||||
var specs = SlpModularCms.Core.Media.VariantPlanner.Plan(info.Width, info.Height, ratios, rig.Processor.SupportedOutput);
|
||||
using var cts = new CancellationTokenSource();
|
||||
var done = 0;
|
||||
|
||||
var act = async () => await rig.Processor.RenderSetAsync(photo, SlpModularCms.Core.Media.ImageFormat.Jpeg, specs, (_, _) => { done++; cts.Cancel(); }, cts.Token);
|
||||
|
||||
await act.Should().ThrowAsync<OperationCanceledException>();
|
||||
done.Should().BeLessThan(specs.Count);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user