-
Notifications
You must be signed in to change notification settings - Fork 272
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
1 parent
2f6c35c
commit dfc3d9b
Showing
5 changed files
with
289 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
49 changes: 49 additions & 0 deletions
49
src/benchmarks/micro/corefx/System.Drawing/Perf_Graphics_DrawBeziers.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
src/benchmarks/micro/corefx/System.Drawing/Perf_Graphics_Transforms.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
92
src/benchmarks/micro/corefx/System.Drawing/Perf_Image_Load.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} | ||
} |