Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Port System.Drawing benchmarks from CoreFX #350

Merged
merged 5 commits into from
Mar 2, 2019
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
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'">
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the API is not available for .NET Core 2.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[]
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the original benchmark was using reflection to read that https://github.com/dotnet/corefx/blob/9940166bff52155600f25f15da13a83c982526d2/src/System.Drawing.Primitives/tests/Performance/Perf_Color.cs#L18-L22

I want to add CoreRT runs soon, so I did remove the reflection. There is also a chance (very small ofc) that in the future we might add new colors to System.Drawing.Color which would be simply seen as a regression (the array would grow and the time as well)

{
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};
Copy link
Member Author

@adamsitnik adamsitnik Mar 1, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the old benchmarks were using 4 random points for DrawBezier_Point and array of 7 different points for DrawBezier_Points

https://github.com/dotnet/corefx/blob/9940166bff52155600f25f15da13a83c982526d2/src/System.Drawing.Common/tests/Performance/Perf_Graphics_DrawBeziers.cs#L42

I unified that to make sure that we compare apples to apples (same points)

}

[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)]
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is the only benchmark that can't run on the OSes that we test (Windows RS4, Ubuntu 16.04)

there were some other conditional benchmarks that would not run for Ubuntu 14.04 and other Unixes but we don't test those OSes in our lab so I did not port those conditions

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)
};
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


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);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the old benchmarks were writing to file

https://github.com/dotnet/corefx/blob/9940166bff52155600f25f15da13a83c982526d2/src/System.Drawing.Common/tests/Performance/Perf_Image_Load.cs#L144

to avoid multimodality of the benchmarks (possibly introduced by disk IO) I decided to use MemoryStream instead.

}

return result;
}
}
}
}