From 7b406537f4f06a5dce37d03e88a5a92df84c80e2 Mon Sep 17 00:00:00 2001 From: Anton Firszov Date: Thu, 3 Sep 2020 23:05:30 +0200 Subject: [PATCH 1/3] add some benchmarks --- .../ImageSharp.Drawing.csproj | 3 +- .../Drawing/DrawLongText.cs | 96 +++++++++++++++++++ .../ImageSharp.Drawing.Benchmarks.csproj | 3 +- .../ImageSharp.Drawing.Tests.csproj | 3 +- .../ProfilingBenchmarks.cs | 68 +++++++++++++ 5 files changed, 170 insertions(+), 3 deletions(-) create mode 100644 tests/ImageSharp.Drawing.Benchmarks/Drawing/DrawLongText.cs create mode 100644 tests/ImageSharp.Drawing.Tests/ProfilingBenchmarks.cs diff --git a/src/ImageSharp.Drawing/ImageSharp.Drawing.csproj b/src/ImageSharp.Drawing/ImageSharp.Drawing.csproj index c942bbd34..01a6d07d5 100644 --- a/src/ImageSharp.Drawing/ImageSharp.Drawing.csproj +++ b/src/ImageSharp.Drawing/ImageSharp.Drawing.csproj @@ -9,7 +9,8 @@ Image Draw Shape Path Font SixLabors.ImageSharp.Drawing - netcoreapp3.1;netcoreapp2.1;netstandard2.1;netstandard2.0;netstandard1.3;net472 + + netcoreapp3.1 diff --git a/tests/ImageSharp.Drawing.Benchmarks/Drawing/DrawLongText.cs b/tests/ImageSharp.Drawing.Benchmarks/Drawing/DrawLongText.cs new file mode 100644 index 000000000..afdf3ab2f --- /dev/null +++ b/tests/ImageSharp.Drawing.Benchmarks/Drawing/DrawLongText.cs @@ -0,0 +1,96 @@ +using System.Drawing; +using System.Text; +using BenchmarkDotNet.Attributes; +using SixLabors.ImageSharp.Drawing.Processing; +using SixLabors.ImageSharp.PixelFormats; +using SixLabors.ImageSharp.Processing; +using Font = SixLabors.Fonts.Font; +using SDBitmap = System.Drawing.Bitmap; +using SDGraphics = System.Drawing.Graphics; +using SDFont = System.Drawing.Font; +using SDPixelFormat = System.Drawing.Imaging.PixelFormat; +using SDRectangleF = System.Drawing.RectangleF; +using SystemFonts = SixLabors.Fonts.SystemFonts; + +namespace SixLabors.ImageSharp.Drawing.Benchmarks +{ + public class DrawLongText + { + private Image imageSharpImage; + private SDBitmap sdBitmap; + private string text; + private Font font; + private SDFont sdFont; + + public const int Width = 1300; + public const int Height = 1300; + public const int Rows = 20; + public const int Cols = 36; + + [GlobalSetup] + public void Setup() + { + this.text = CreateText(Rows, Cols); + this.imageSharpImage = new Image(Width, Height, Color.Black.ToPixel()); + this.font = SystemFonts.CreateFont("Arial", 50); + + this.sdBitmap = new SDBitmap(Width, Height, SDPixelFormat.Format32bppArgb); + this.sdFont = new SDFont("Arial", 12, System.Drawing.GraphicsUnit.Point); + } + + [GlobalCleanup] + public void Cleanup() + { + this.sdFont.Dispose(); + this.imageSharpImage.Dispose(); + this.sdBitmap.Dispose(); + } + + [Benchmark] + public void ImageSharp() + { + this.imageSharpImage.Mutate(ctx => ctx.DrawText(this.text, this.font, Color.White, default)); + } + + [Benchmark(Baseline = true)] + public void SystemDrawing() + { + using SDGraphics graphics = SDGraphics.FromImage(this.sdBitmap); + graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.Default; + graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias; + + SDRectangleF rect = new SDRectangleF(0, 0, Width, Height); + graphics.DrawString( + this.text, + this.sdFont, + System.Drawing.Brushes.HotPink, + rect); + } + + private static string CreateText(int rows, int cols) + { + StringBuilder bld = new StringBuilder(); + const char firstChar = '!'; + const char lastChar = 'z'; + + int currentChar = firstChar; + for (int i = 0; i < rows; i++) + { + for (int j = 0; j < cols; j++) + { + bld.Append((char)currentChar); + currentChar++; + if (currentChar > lastChar) + { + currentChar = firstChar; + } + } + + bld.AppendLine(); + } + + string text = bld.ToString(); + return text; + } + } +} \ No newline at end of file diff --git a/tests/ImageSharp.Drawing.Benchmarks/ImageSharp.Drawing.Benchmarks.csproj b/tests/ImageSharp.Drawing.Benchmarks/ImageSharp.Drawing.Benchmarks.csproj index fe40cdbf2..f8b18f0ce 100644 --- a/tests/ImageSharp.Drawing.Benchmarks/ImageSharp.Drawing.Benchmarks.csproj +++ b/tests/ImageSharp.Drawing.Benchmarks/ImageSharp.Drawing.Benchmarks.csproj @@ -3,7 +3,8 @@ Exe SixLabors.ImageSharp.Drawing.Benchmarks - netcoreapp3.1;netcoreapp2.1;net472 + + netcoreapp3.1 false false diff --git a/tests/ImageSharp.Drawing.Tests/ImageSharp.Drawing.Tests.csproj b/tests/ImageSharp.Drawing.Tests/ImageSharp.Drawing.Tests.csproj index 66732cca2..54199bb29 100644 --- a/tests/ImageSharp.Drawing.Tests/ImageSharp.Drawing.Tests.csproj +++ b/tests/ImageSharp.Drawing.Tests/ImageSharp.Drawing.Tests.csproj @@ -2,7 +2,8 @@ - netcoreapp3.1;netcoreapp2.1;net472 + + netcoreapp3.1 True latest True diff --git a/tests/ImageSharp.Drawing.Tests/ProfilingBenchmarks.cs b/tests/ImageSharp.Drawing.Tests/ProfilingBenchmarks.cs new file mode 100644 index 000000000..698f27da3 --- /dev/null +++ b/tests/ImageSharp.Drawing.Tests/ProfilingBenchmarks.cs @@ -0,0 +1,68 @@ +using System.Text; +using SixLabors.Fonts; +using SixLabors.ImageSharp.Drawing.Processing; +using SixLabors.ImageSharp.PixelFormats; +using SixLabors.ImageSharp.Processing; +using Xunit; + +namespace SixLabors.ImageSharp.Drawing.Tests +{ + public class ProfilingBenchmarks + { + [Theory] + [WithSolidFilledImages(1300, 1300, 0, 0, 0, PixelTypes.Rgba32, 1, 20, 36)] + public void RenderLongText(TestImageProvider provider, int times, int rows, int cols) + where TPixel : unmanaged, IPixel + { + using Image image = provider.GetImage(); + Font font = CreateFont("Arial", 50); + + var text = CreateText(rows, cols); + for (int i = 0; i < times; i++) + { + image.Mutate(ctx => ctx.DrawText(text, font, Color.White, default)); + if (i == 0) + { + image.DebugSave(provider); + } + } + } + + private static string CreateText(int rows, int cols) + { + StringBuilder bld = new StringBuilder(); + const char firstChar = '!'; + const char lastChar = 'z'; + int cnt = lastChar - firstChar; + + int currentChar = firstChar; + for (int i = 0; i < rows; i++) + { + for (int j = 0; j < cols; j++) + { + bld.Append((char)currentChar); + currentChar++; + if (currentChar > lastChar) + { + currentChar = firstChar; + } + } + + bld.AppendLine(); + } + + string text = bld.ToString(); + return text; + } + + + private static Font CreateFont(string fontName, int size) + { + return SystemFonts.CreateFont(fontName, size); + // var fontCollection = new FontCollection(); + // string fontPath = TestFontUtilities.GetPath(fontName); + // Font font = fontCollection.Install(fontPath).CreateFont(size); + // return font; + } + } +} \ No newline at end of file From 354cc8d1d1543d4201b5601c551e0552780ab2ef Mon Sep 17 00:00:00 2001 From: Anton Firszov Date: Thu, 3 Sep 2020 23:08:50 +0200 Subject: [PATCH 2/3] do not debugsave --- tests/ImageSharp.Drawing.Tests/ProfilingBenchmarks.cs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/ImageSharp.Drawing.Tests/ProfilingBenchmarks.cs b/tests/ImageSharp.Drawing.Tests/ProfilingBenchmarks.cs index 698f27da3..7d841c4fc 100644 --- a/tests/ImageSharp.Drawing.Tests/ProfilingBenchmarks.cs +++ b/tests/ImageSharp.Drawing.Tests/ProfilingBenchmarks.cs @@ -21,10 +21,10 @@ public void RenderLongText(TestImageProvider provider, int times for (int i = 0; i < times; i++) { image.Mutate(ctx => ctx.DrawText(text, font, Color.White, default)); - if (i == 0) - { - image.DebugSave(provider); - } + // if (i == 0) + // { + // image.DebugSave(provider); + // } } } From 5f562180c3a55d11009735207293e193997f4c21 Mon Sep 17 00:00:00 2001 From: Anton Firszov Date: Thu, 3 Sep 2020 23:14:01 +0200 Subject: [PATCH 3/3] run RenderLongText x10 --- tests/ImageSharp.Drawing.Tests/ProfilingBenchmarks.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/ImageSharp.Drawing.Tests/ProfilingBenchmarks.cs b/tests/ImageSharp.Drawing.Tests/ProfilingBenchmarks.cs index 7d841c4fc..d1311a23a 100644 --- a/tests/ImageSharp.Drawing.Tests/ProfilingBenchmarks.cs +++ b/tests/ImageSharp.Drawing.Tests/ProfilingBenchmarks.cs @@ -10,7 +10,7 @@ namespace SixLabors.ImageSharp.Drawing.Tests public class ProfilingBenchmarks { [Theory] - [WithSolidFilledImages(1300, 1300, 0, 0, 0, PixelTypes.Rgba32, 1, 20, 36)] + [WithSolidFilledImages(1300, 1300, 0, 0, 0, PixelTypes.Rgba32, 5, 20, 36)] public void RenderLongText(TestImageProvider provider, int times, int rows, int cols) where TPixel : unmanaged, IPixel {