Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Drawing.Imaging;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using ImageMagick;
using PhotoSauce.MagicScaler;
using SixLabors.ImageSharp.Formats;
Expand All @@ -27,6 +28,7 @@ public enum JpegKind
Any = Baseline | Progressive
}

[SupportedOSPlatform("windows")]
public class LoadResizeSaveStressRunner
{
private const int Quality = 75;
Expand Down Expand Up @@ -158,7 +160,7 @@ private string OutputPath(string inputPath, [CallerMemberName] string postfix =
this.outputDirectory,
Path.GetFileNameWithoutExtension(inputPath) + "-" + postfix + Path.GetExtension(inputPath));

private (int Width, int Height) ScaledSize(int inWidth, int inHeight, int outSize)
private static (int Width, int Height) ScaledSize(int inWidth, int inHeight, int outSize)
{
int width, height;
if (inWidth > inHeight)
Expand All @@ -180,7 +182,7 @@ public void SystemDrawingResize(string input)
using SystemDrawingImage image = SystemDrawingImage.FromFile(input, true);
this.LogImageProcessed(image.Width, image.Height);

(int width, int height) = this.ScaledSize(image.Width, image.Height, this.ThumbnailSize);
(int width, int height) = ScaledSize(image.Width, image.Height, this.ThumbnailSize);
Bitmap resized = new(width, height);
using Graphics graphics = Graphics.FromImage(resized);
using ImageAttributes attributes = new();
Expand Down Expand Up @@ -282,7 +284,7 @@ public void SkiaCanvasResize(string input)
{
using SKBitmap original = SKBitmap.Decode(input);
this.LogImageProcessed(original.Width, original.Height);
(int width, int height) = this.ScaledSize(original.Width, original.Height, this.ThumbnailSize);
(int width, int height) = ScaledSize(original.Width, original.Height, this.ThumbnailSize);
using SKSurface surface = SKSurface.Create(new SKImageInfo(width, height, original.ColorType, original.AlphaType));
using SKPaint paint = new() { FilterQuality = SKFilterQuality.High };
SKCanvas canvas = surface.Canvas;
Expand All @@ -300,7 +302,7 @@ public void SkiaBitmapResize(string input)
{
using SKBitmap original = SKBitmap.Decode(input);
this.LogImageProcessed(original.Width, original.Height);
(int width, int height) = this.ScaledSize(original.Width, original.Height, this.ThumbnailSize);
(int width, int height) = ScaledSize(original.Width, original.Height, this.ThumbnailSize);
using SKBitmap resized = original.Resize(new SKImageInfo(width, height), SKFilterQuality.High);
if (resized == null)
{
Expand All @@ -319,7 +321,7 @@ public void SkiaBitmapDecodeToTargetSize(string input)

SKImageInfo info = codec.Info;
this.LogImageProcessed(info.Width, info.Height);
(int width, int height) = this.ScaledSize(info.Width, info.Height, this.ThumbnailSize);
(int width, int height) = ScaledSize(info.Width, info.Height, this.ThumbnailSize);
SKSizeI supportedScale = codec.GetScaledDimensions((float)width / info.Width);

using SKBitmap original = SKBitmap.Decode(codec, new SKImageInfo(supportedScale.Width, supportedScale.Height));
Expand Down
65 changes: 65 additions & 0 deletions tests/ImageSharp.Benchmarks/Processing/ParallelProcessing.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// Copyright (c) Six Labors.
// Licensed under the Six Labors Split License.

using BenchmarkDotNet.Attributes;
using SixLabors.ImageSharp.PixelFormats;
using SixLabors.ImageSharp.Processing;
using SixLabors.ImageSharp.Tests;

namespace SixLabors.ImageSharp.Benchmarks;

public class ParallelProcessing
{
private Image<Rgba32> image;
private Image<Rgba32> foreground;
private Configuration configuration;

public static IEnumerable<int> MaxDegreeOfParallelismValues()
{
int processorCount = Environment.ProcessorCount;
for (int p = 1; p <= processorCount; p *= 2)
{
yield return p;
}

if ((processorCount & (processorCount - 1)) != 0)
{
yield return processorCount;
}
}

[ParamsSource(nameof(MaxDegreeOfParallelismValues))]
public int MaxDegreeOfParallelism { get; set; }

[GlobalSetup]
public void Setup()
{
this.image = new Image<Rgba32>(2048, 2048);
this.foreground = new Image<Rgba32>(2048, 2048);
this.configuration = Configuration.Default.Clone();
this.configuration.MaxDegreeOfParallelism = this.MaxDegreeOfParallelism;
}

[Benchmark]
public void DetectEdges() => this.image.Mutate(this.configuration, x => x.DetectEdges());

[Benchmark]
public void DrawImage() => this.image.Mutate(this.configuration, x => x.DrawImage(this.foreground, 0.5f));

[Benchmark]
public void Crop()
{
Rectangle bounds = this.image.Bounds;
bounds = new Rectangle(1, 1, bounds.Width - 2, bounds.Height - 2);
this.image
.Clone(this.configuration, x => x.Crop(bounds))
.Dispose();
}

[GlobalCleanup]
public void Cleanup()
{
this.image.Dispose();
this.foreground.Dispose();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
<Prefer32Bit>false</Prefer32Bit>
<RootNamespace>SixLabors.ImageSharp.Tests.ProfilingSandbox</RootNamespace>
<RuntimeIdentifier>win-x64</RuntimeIdentifier>
<StartupObject>SixLabors.ImageSharp.Tests.ProfilingSandbox.Program</StartupObject>
<!--Used to hide test project from dotnet test-->
<IsTestProject>false</IsTestProject>
<EnsureNETCoreAppRuntime>false</EnsureNETCoreAppRuntime>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System.Diagnostics;
using System.Globalization;
using System.Runtime.Versioning;
using System.Text;
using CommandLine;
using CommandLine.Text;
Expand All @@ -13,7 +14,8 @@
namespace SixLabors.ImageSharp.Tests.ProfilingSandbox;

// See ImageSharp.Benchmarks/LoadResizeSave/README.md
internal class LoadResizeSaveParallelMemoryStress
[SupportedOSPlatform("windows")]
internal sealed class LoadResizeSaveParallelMemoryStress
{
private LoadResizeSaveParallelMemoryStress()
{
Expand Down Expand Up @@ -206,14 +208,15 @@ public string GetMarkdown()
StringBuilder bld = new();
bld.AppendLine($"| {nameof(this.TotalSeconds)} | {nameof(this.MegapixelsPerSec)} | {nameof(this.MegapixelsPerSecPerCpu)} |");
bld.AppendLine(
CultureInfo.InvariantCulture,
$"| {L(nameof(this.TotalSeconds))} | {L(nameof(this.MegapixelsPerSec))} | {L(nameof(this.MegapixelsPerSecPerCpu))} |");

bld.Append("| ");
bld.AppendFormat(F(nameof(this.TotalSeconds)), this.TotalSeconds);
bld.AppendFormat(CultureInfo.InvariantCulture, F(nameof(this.TotalSeconds)), this.TotalSeconds);
bld.Append(" | ");
bld.AppendFormat(F(nameof(this.MegapixelsPerSec)), this.MegapixelsPerSec);
bld.AppendFormat(CultureInfo.InvariantCulture, F(nameof(this.MegapixelsPerSec)), this.MegapixelsPerSec);
bld.Append(" | ");
bld.AppendFormat(F(nameof(this.MegapixelsPerSecPerCpu)), this.MegapixelsPerSecPerCpu);
bld.AppendFormat(CultureInfo.InvariantCulture, F(nameof(this.MegapixelsPerSecPerCpu)), this.MegapixelsPerSecPerCpu);
bld.AppendLine(" |");

return bld.ToString();
Expand All @@ -223,7 +226,7 @@ public string GetMarkdown()
}
}

private class CommandLineOptions
private sealed class CommandLineOptions
{
[Option('a', "async-imagesharp", Required = false, Default = false, HelpText = "Async ImageSharp without benchmark switching")]
public bool AsyncImageSharp { get; set; }
Expand Down
Loading
Loading