-
Notifications
You must be signed in to change notification settings - Fork 272
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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[] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
{ | ||
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}; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the old benchmarks were using 4 random points for 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)] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(); | ||
} | ||
} | ||
} |
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) | ||
}; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the old benchmarks were most probably by accident using only |
||
|
||
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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the old benchmarks were writing to file to avoid multimodality of the benchmarks (possibly introduced by disk IO) I decided to use |
||
} | ||
|
||
return result; | ||
} | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
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