Skip to content

Commit

Permalink
Port System.Drawing benchmarks from CoreFX (#350), part of #344
Browse files Browse the repository at this point in the history
* copy existing tests

* port System.Drawing.Primitives tests

* port Perf_Graphics_Transforms

* port Perf_Image_Load tests

* port Perf_Graphics_DrawBeziers
  • Loading branch information
adamsitnik authored Mar 2, 2019
1 parent 2f6c35c commit dfc3d9b
Show file tree
Hide file tree
Showing 5 changed files with 289 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/benchmarks/micro/MicroBenchmarks.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
<PackageReference Include="MessagePackAnalyzer" Version="1.6.0" />
<PackageReference Include="Newtonsoft.Json" Version="11.0.2" />
<PackageReference Include="protobuf-net" Version="2.3.7" />
<PackageReference Include="System.Drawing.Common" Version="4.5.1" />
<PackageReference Include="System.Memory" Version="4.5.0" />
<PackageReference Include="System.Numerics.Vectors" Version="4.5.0" />
<PackageReference Include="System.Runtime.CompilerServices.Unsafe" Version="4.5.2" />
Expand Down Expand Up @@ -61,4 +62,8 @@
<Compile Remove="corefx\System.Security.Cryptography\Perf.Rfc2898DeriveBytes.cs" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'netcoreapp2.0'">
<Compile Remove="corefx\System.Drawing\Perf_Graphics*.cs" />
</ItemGroup>

</Project>
95 changes: 95 additions & 0 deletions src/benchmarks/micro/corefx/System.Drawing/Perf_Color.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using BenchmarkDotNet.Attributes;
using MicroBenchmarks;
using static System.Drawing.Color;

namespace System.Drawing.Tests
{
[BenchmarkCategory(Categories.CoreFX)]
public class Perf_Color
{
private static readonly Color[] AllKnownColors;

private readonly Color _field = DarkSalmon;

static Perf_Color()
{
AllKnownColors = new[]
{
AliceBlue, AntiqueWhite, Aqua, Aquamarine, Azure, Beige,
Bisque, Black, BlanchedAlmond, Blue, BlueViolet,
Brown, BurlyWood, CadetBlue, Chartreuse, Chocolate,
Coral, CornflowerBlue, Cornsilk, Crimson, Cyan,
DarkBlue, DarkCyan, DarkGoldenrod, DarkGray, DarkGreen,
DarkKhaki, DarkMagenta, DarkOliveGreen, DarkOrange, DarkOrchid,
DarkRed, DarkSalmon, DarkSeaGreen, DarkSlateBlue, DarkSlateGray,
DarkTurquoise, DarkViolet, DeepPink, DeepSkyBlue, DimGray,
DodgerBlue, Firebrick, FloralWhite, ForestGreen, Fuchsia,
Gainsboro, GhostWhite, Gold, Goldenrod, Gray,
Green, GreenYellow, Honeydew, HotPink, IndianRed,
Indigo, Ivory, Khaki, Lavender, LavenderBlush,
LawnGreen, LemonChiffon, LightBlue, LightCoral, LightCyan,
LightGoldenrodYellow, LightGray, LightGreen, LightPink, LightSalmon,
LightSeaGreen, LightSkyBlue, LightSlateGray, LightSteelBlue, LightYellow,
Lime, LimeGreen, Linen, Magenta, Maroon,
MediumAquamarine, MediumBlue, MediumOrchid, MediumPurple, MediumSeaGreen,
MediumSlateBlue, MediumSpringGreen, MediumTurquoise, MediumVioletRed, MidnightBlue,
MintCream, MistyRose, Moccasin, NavajoWhite, Navy,
OldLace, Olive, OliveDrab, Orange, OrangeRed,
Orchid, PaleGoldenrod, PaleGreen, PaleTurquoise, PaleVioletRed,
PapayaWhip, PeachPuff, Peru, Pink, Plum,
PowderBlue, Purple, Red, RosyBrown, RoyalBlue,
SaddleBrown, Salmon, SandyBrown, SeaGreen, SeaShell,
Sienna, Silver, SkyBlue, SlateBlue, SlateGray,
Snow, SpringGreen, SteelBlue, Tan, Teal,
Thistle, Tomato, Transparent, Turquoise, Violet,
Wheat, White, WhiteSmoke, Yellow, YellowGreen
};
}

[Benchmark]
public Color FromArgb_Channels() => FromArgb(byte.MaxValue, 0xFF, byte.MinValue, 0xFF);

[Benchmark]
public Color FromArgb_AlphaColor() => FromArgb(0xFF, _field);

[Benchmark]
public float GetBrightness()
{
float brightness = 0.0f;
var colors = AllKnownColors;

for (int j = 0; j < colors.Length; j++)
brightness += colors[j].GetBrightness();

return brightness;
}

[Benchmark]
public float GetHue()
{
float hue = 0.0f;
var colors = AllKnownColors;

for (int j = 0; j < colors.Length; j++)
hue += colors[j].GetHue();

return hue;
}

[Benchmark]
public float GetSaturation()
{
float saturation = 0.0f;
var colors = AllKnownColors;

for (int j = 0; j < colors.Length; j++)
saturation += colors[j].GetSaturation();

return saturation;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Extensions;
using MicroBenchmarks;

namespace System.Drawing.Tests
{
[BenchmarkCategory(Categories.CoreFX)]
public class Perf_Graphics_DrawBeziers
{
private Bitmap _image;
private Pen _pen;
private Graphics _graphics;
private Point _point1, _point2, _point3, _point4;
private Point[] _points;

[GlobalSetup]
public void Setup()
{
_image = new Bitmap(100, 100);
_pen = new Pen(Color.White);
_graphics = Graphics.FromImage(_image);

_point1 = new Point(10, 10);
_point2 = new Point(20, 1);
_point3 = new Point(35, 5);
_point4 = new Point(50, 10);

_points = new[] {_point1, _point2, _point3, _point4};
}

[GlobalCleanup]
public void Cleanup()
{
_graphics.Dispose();
_pen.Dispose();
_image.Dispose();
}

[Benchmark]
public void DrawBezier_Point() => _graphics.DrawBezier(_pen, _point1, _point2, _point3, _point4);

[Benchmark]
public void DrawBezier_Points() => _graphics.DrawBeziers(_pen, _points);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System.Drawing.Drawing2D;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Extensions;
using MicroBenchmarks;

namespace System.Drawing.Tests
{
[BenchmarkCategory(Categories.CoreFX)]
public class Perf_Graphics_Transforms
{
private Bitmap _image;
private Graphics _graphics;
private Point[] _points;

[GlobalSetup]
public void Setup()
{
_points = new []
{
new Point(10, 10), new Point(20, 1), new Point(35, 5), new Point(50, 10),
new Point(60, 15), new Point(65, 25), new Point(50, 30)
};

_image = new Bitmap(100, 100);
_graphics = Graphics.FromImage(_image);
}

[Benchmark]
[AllowedOperatingSystems("Graphics.TransformPoints is not implemented in libgdiplus yet. See dotnet/corefx 20884", OS.Windows)]
public void TransformPoints()
{
_graphics.TransformPoints(CoordinateSpace.World, CoordinateSpace.Page, _points);
_graphics.TransformPoints(CoordinateSpace.Device, CoordinateSpace.World, _points);
_graphics.TransformPoints(CoordinateSpace.Page, CoordinateSpace.Device, _points);
}

[GlobalCleanup]
public void Cleanup()
{
_graphics.Dispose();
_image.Dispose();
}
}
}
92 changes: 92 additions & 0 deletions src/benchmarks/micro/corefx/System.Drawing/Perf_Image_Load.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System.Collections.Generic;
using System.Drawing.Imaging;
using System.IO;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Extensions;
using MicroBenchmarks;

namespace System.Drawing.Tests
{
[BenchmarkCategory(Categories.CoreFX)]
public class Perf_Image_Load
{
private static readonly ImageTestData[] TestCases = {
new ImageTestData(ImageFormat.Bmp),
new ImageTestData(ImageFormat.Jpeg),
new ImageTestData(ImageFormat.Png),
new ImageTestData(ImageFormat.Gif)
};

public IEnumerable<object> ImageFormats() => TestCases;

[Benchmark]
[ArgumentsSource(nameof(ImageFormats))]
public void Bitmap_FromStream(ImageTestData format)
{
using (new Bitmap(format.Stream))
{
}
}

[Benchmark]
[ArgumentsSource(nameof(ImageFormats))]
public void Image_FromStream(ImageTestData format)
{
using (Image.FromStream(format.Stream))
{
}
}

[Benchmark]
[ArgumentsSource(nameof(ImageFormats))]
public void Image_FromStream_NoValidation(ImageTestData format)
{
using (Image.FromStream(format.Stream, false, false))
{
}
}

public class ImageTestData
{
public Stream Stream { get; }
private string FormatName { get; }

public ImageTestData(ImageFormat format)
{
Stream = CreateTestImage(format);
FormatName = format.ToString();
}

// the value returned by ToString is used in the text representation of Benchmark ID in our reporting system
public override string ToString() => FormatName;

private static Stream CreateTestImage(ImageFormat format)
{
Random r = new Random(1066); // the seed must not be changed

const int Size = 1000;
Point RandomPoint() => new Point(r.Next(Size), r.Next(Size));

var result = new MemoryStream();

using (Bitmap bitmap = new Bitmap(Size, Size))
using (Pen pen = new Pen(Color.Blue))
using (Graphics graphics = Graphics.FromImage(bitmap))
{
for (int i = 0; i < 100; i++)
{
graphics.DrawBezier(pen, RandomPoint(), RandomPoint(), RandomPoint(), RandomPoint());
}

bitmap.Save(result, format);
}

return result;
}
}
}
}

0 comments on commit dfc3d9b

Please sign in to comment.