From f8bd074dfceb884794b4e040a08e108a849d8133 Mon Sep 17 00:00:00 2001 From: Anton Firszov Date: Mon, 9 Jul 2018 01:29:33 +0200 Subject: [PATCH] LoadResizeSave benchmark + add EnableParallelExecution parameter to the Resize benchmark --- .../Codecs/Jpeg/LoadResizeSave.cs | 88 +++++++++++++++++++ .../ImageSharp.Benchmarks/Samplers/Resize.cs | 36 ++++++-- 2 files changed, 115 insertions(+), 9 deletions(-) create mode 100644 tests/ImageSharp.Benchmarks/Codecs/Jpeg/LoadResizeSave.cs diff --git a/tests/ImageSharp.Benchmarks/Codecs/Jpeg/LoadResizeSave.cs b/tests/ImageSharp.Benchmarks/Codecs/Jpeg/LoadResizeSave.cs new file mode 100644 index 000000000..296b3fb7a --- /dev/null +++ b/tests/ImageSharp.Benchmarks/Codecs/Jpeg/LoadResizeSave.cs @@ -0,0 +1,88 @@ +// Copyright (c) Six Labors and contributors. +// Licensed under the Apache License, Version 2.0. + +using BenchmarkDotNet.Attributes; +using System; +using System.IO; +using SixLabors.ImageSharp.Tests; +using System.Drawing; +using System.Drawing.Drawing2D; +using System.Drawing.Imaging; +using SixLabors.ImageSharp.Processing; +using SDImage = System.Drawing.Image; +using SixLabors.ImageSharp.Formats.Jpeg; + +namespace SixLabors.ImageSharp.Benchmarks.Codecs.Jpeg +{ + [Config(typeof(Config.ShortClr))] + public class LoadResizeSave : BenchmarkBase + { + private readonly Configuration configuration = new Configuration(new JpegConfigurationModule()); + + private byte[] sourceBytes; + + private byte[] destBytes; + + private string TestImageFullPath => Path.Combine(TestEnvironment.InputImagesDirectoryFullPath, this.TestImage); + + [Params( + TestImages.Jpeg.Baseline.Jpeg420Exif + //, TestImages.Jpeg.Baseline.Calliphora + )] + public string TestImage { get; set; } + + [Params(false, true)] + public bool EnableParallelExecution { get; set; } + + [GlobalSetup] + public void Setup() + { + this.configuration.ParallelOptions.MaxDegreeOfParallelism = + this.EnableParallelExecution ? Environment.ProcessorCount : 1; + + if (this.sourceBytes == null) + { + this.sourceBytes = File.ReadAllBytes(this.TestImageFullPath); + } + + if (this.destBytes == null) + { + this.destBytes = new byte[this.sourceBytes.Length]; + } + } + + [Benchmark(Baseline = true)] + public void SystemDrawing() + { + using (var sourceStream = new MemoryStream(this.sourceBytes)) + using (var destStream = new MemoryStream(this.destBytes)) + using (var source = SDImage.FromStream(sourceStream)) + using (var destination = new Bitmap(source.Width / 4, source.Height / 4)) + { + using (var graphics = Graphics.FromImage(destination)) + { + graphics.InterpolationMode = InterpolationMode.HighQualityBicubic; + graphics.PixelOffsetMode = PixelOffsetMode.HighQuality; + graphics.CompositingQuality = CompositingQuality.HighQuality; + graphics.DrawImage(source, 0, 0, 400, 400); + } + + destination.Save(destStream, ImageFormat.Jpeg); + } + } + + [Benchmark] + public void ImageSharp() + { + using (var source = Image.Load( + this.configuration, + this.sourceBytes, + new JpegDecoder { IgnoreMetadata = true })) + using (var destStream = new MemoryStream(this.destBytes)) + { + source.Mutate(c => c.Resize(source.Width / 4, source.Height / 4)); + source.SaveAsJpeg(destStream); + } + } + } +} \ No newline at end of file diff --git a/tests/ImageSharp.Benchmarks/Samplers/Resize.cs b/tests/ImageSharp.Benchmarks/Samplers/Resize.cs index 8bba227c5..d4506fc6a 100644 --- a/tests/ImageSharp.Benchmarks/Samplers/Resize.cs +++ b/tests/ImageSharp.Benchmarks/Samplers/Resize.cs @@ -3,20 +3,38 @@ // Licensed under the Apache License, Version 2.0. // -namespace SixLabors.ImageSharp.Benchmarks -{ - using System.Drawing; - using System.Drawing.Drawing2D; +using System; +using System.Drawing; +using System.Drawing.Drawing2D; + +using BenchmarkDotNet.Attributes; - using BenchmarkDotNet.Attributes; +using SixLabors.ImageSharp.PixelFormats; +using SixLabors.ImageSharp.Processing; - using SixLabors.ImageSharp.PixelFormats; - using SixLabors.ImageSharp.Processing; +using CoreSize = SixLabors.Primitives.Size; + +namespace SixLabors.ImageSharp.Benchmarks +{ + using System.Threading.Tasks; - using CoreSize = SixLabors.Primitives.Size; + using SixLabors.ImageSharp.Formats.Jpeg; + [Config(typeof(Config.ShortClr))] public class Resize : BenchmarkBase { + private readonly Configuration configuration = new Configuration(new JpegConfigurationModule()); + + [Params(false, true)] + public bool EnableParallelExecution { get; set; } + + [GlobalSetup] + public void Setup() + { + this.configuration.ParallelOptions.MaxDegreeOfParallelism = + this.EnableParallelExecution ? Environment.ProcessorCount : 1; + } + [Benchmark(Baseline = true, Description = "System.Drawing Resize")] public Size ResizeSystemDrawing() { @@ -40,7 +58,7 @@ namespace SixLabors.ImageSharp.Benchmarks [Benchmark(Description = "ImageSharp Resize")] public CoreSize ResizeCore() { - using (Image image = new Image(2000, 2000)) + using (var image = new Image(this.configuration, 2000, 2000)) { image.Mutate(x => x.Resize(400, 400)); return new CoreSize(image.Width, image.Height);