Skip to content

Commit

Permalink
Add missing tests from .NET Core 3.0 perf tests blog post (#478)
Browse files Browse the repository at this point in the history
* Add missing tests from .NET Core 3.0 perf tests blog post
  • Loading branch information
stephentoub authored and adamsitnik committed Jul 18, 2019
1 parent 8e2e09d commit 7d96fd3
Show file tree
Hide file tree
Showing 25 changed files with 391 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public IEnumerable<object> UInt64HexValues
public bool TryParseUInt64Hex(Utf8TestCase value) => Utf8Parser.TryParse(value.Utf8Bytes, out ulong _, out int _, 'X');

public IEnumerable<object> Int32Values
=> Perf_Int32.StringValues.OfType<string>().Select(formatted => new Utf8TestCase(formatted));
=> Perf_Int32.StringValuesDecimal.OfType<string>().Select(formatted => new Utf8TestCase(formatted));

[Benchmark]
[ArgumentsSource(nameof(Int32Values))]
Expand Down
13 changes: 11 additions & 2 deletions src/benchmarks/micro/corefx/System.Collections/Concurrent/Count.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,20 @@ public void Setup()

[Benchmark]
public int Queue() => _queue.Count;


[Benchmark]
public int Queue_EnqueueCountDequeue()
{
_queue.Enqueue(default);
int c = _queue.Count;
_queue.TryDequeue(out _);
return c;
}

[Benchmark]
public int Stack() => _stack.Count;

[Benchmark]
public int Bag() => _bag.Count;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,13 @@ public void Setup()

[Benchmark]
public int Max() => _set.Max;

[Benchmark]
public int EnumerateViewBetween()
{
int count = 0;
foreach (int item in _set.GetViewBetween(100, 200)) count++;
return count;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ namespace System.Globalization.Tests
[BenchmarkCategory(Categories.CoreFX)]
public class Perf_DateTimeCultureInfo
{
private readonly DateTime _time = DateTime.Now;
private readonly DateTime _time = new DateTime(654321098765432109);

public IEnumerable<object> Cultures()
{
yield return new CultureInfo("fr");
Expand All @@ -35,5 +35,17 @@ public IEnumerable<object> Cultures()
[ArgumentsSource(nameof(Cultures))]
public DateTime Parse(CultureInfo culturestring)
=> DateTime.Parse("10/10/2010 12:00:00 AM", culturestring);

private readonly CultureInfo _hebrewIsrael = CreateHebrewIsraelCultureInfo();

private static CultureInfo CreateHebrewIsraelCultureInfo()
{
var c = new CultureInfo("he-IL");
c.DateTimeFormat.Calendar = new HebrewCalendar();
return c;
}

[Benchmark]
public string ToStringHebrewIsrael() => _time.ToString(_hebrewIsrael);
}
}
3 changes: 3 additions & 0 deletions src/benchmarks/micro/corefx/System.Linq/Perf.Enumerable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,9 @@ public IEnumerable<object> ElementAtArguments()
[ArgumentsSource(nameof(IEnumerableArgument))]
public void Except(LinqTestData input) => input.Collection.Except(input.Collection).Consume(_consumer);

[Benchmark]
public void EmptyTakeSelectToArray() => Enumerable.Empty<int>().Take(10).Select(i => i).ToArray();

#if NETCOREAPP3_0 // API Available in .NET Core 3.0+
// Append() has two execution paths: AppendPrependIterator (a result of another Append or Prepend) and IEnumerable, this benchmark tests both
// https://github.com/dotnet/corefx/blob/dcf1c8f51bcdbd79e08cc672e327d50612690a25/src/System.Linq/src/System/Linq/AppendPrepend.cs
Expand Down
13 changes: 12 additions & 1 deletion src/benchmarks/micro/corefx/System.Memory/ReadOnlySpan.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,18 @@ public int IndexOfString(string input, string value, StringComparison comparison
new object[] { GenerateInputString('\u3060', 1000, 'x', 500), "x", StringComparison.Ordinal },
new object[] { GenerateInputString('\u3060', 100, '\u3059', 50), "\u3059", StringComparison.Ordinal }
};


[Benchmark]
[ArgumentsSource(nameof(TrimArguments))]
public ReadOnlySpan<char> Trim(string input) => input.AsSpan().Trim();

public static IEnumerable<object> TrimArguments()
{
yield return "";
yield return " abcdefg ";
yield return "abcdefg";
}

private static string GenerateInputString(char source, int count, char replaceChar, int replacePos)
{
char[] str = new char[count];
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// 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.Linq;
using BenchmarkDotNet.Attributes;
using MicroBenchmarks;

namespace System.Net.NetworkInformation.Tests
{
[BenchmarkCategory(Categories.CoreFX)]
public class PhysicalAddressTests
{
private readonly PhysicalAddress _short = new PhysicalAddress(new byte[1] { 42 });
private readonly PhysicalAddress _long = new PhysicalAddress(Enumerable.Range(0, 256).Select(i => (byte)i).ToArray());

[Benchmark]
public void PAShort() => _short.ToString();

[Benchmark]
public void PALong() => _long.ToString();
}
}
19 changes: 19 additions & 0 deletions src/benchmarks/micro/corefx/System.Net.Primitives/DnsTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// 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;

namespace System.Net.Tests
{
[BenchmarkCategory(Categories.CoreFX)]
public class DnsTests
{
[Benchmark]
public IPHostEntry GetHostEntry() => Dns.GetHostEntry("34.206.253.53");

[Benchmark]
public string GetHostName() => Dns.GetHostName();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -107,5 +107,12 @@ public void WriteString(int writeLength)
stream.Position = 0;
}
}

[Benchmark]
public void WriteFormat()
{
_memoryStream.Position = 0;
_streamWriter.Write("Writing out a value: {0}", 42);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

using BenchmarkDotNet.Attributes;
using MicroBenchmarks;
using System.Net;

namespace System.Net.Tests
{
Expand All @@ -16,5 +15,8 @@ public class Perf_WebUtility

[Benchmark]
public string Decode_NoDecodingRequired() => WebUtility.UrlDecode("abcdefghijklmnopqrstuvwxyz");

[Benchmark]
public void HtmlDecode_Entities() => WebUtility.HtmlDecode("&#x6C34;&#x6C34;&#x6C34;&#x6C34;&#x6C34;&#x6C34;&#x6C34;");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// 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 Microsoft.Win32.SafeHandles;

namespace System.Runtime.InteropServices.Tests
{
[BenchmarkCategory(Categories.CoreFX)]
public class SafeHandleTests
{
private readonly SafeFileHandle _sfh = new SafeFileHandle((IntPtr)12345, ownsHandle: false);

[Benchmark]
public IntPtr AddRef_GetHandle_Release()
{
bool success = false;
try
{
_sfh.DangerousAddRef(ref success);
return _sfh.DangerousGetHandle();
}
finally
{
if (success)
{
_sfh.DangerousRelease();
}
}
}

}
}
42 changes: 42 additions & 0 deletions src/benchmarks/micro/corefx/System.Runtime/Perf.Array.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

using BenchmarkDotNet.Attributes;
using MicroBenchmarks;
using System.Linq;
using System.Runtime.InteropServices;

namespace System.Tests
{
Expand Down Expand Up @@ -270,5 +272,45 @@ public void ArrayResize()
for (int i = 0; i < _byteArrays.Length; i++)
Array.Resize<byte>(ref _byteArrays[i], NewSize);
}

private readonly int[] _reversibleArray = Enumerable.Range(0, 256).ToArray();

[Benchmark]
public void Reverse() => Array.Reverse(_reversibleArray);

[GlobalSetup(Target = nameof(ClearUnaligned))]
public void SetupClearUnaligned()
{
while (true)
{
var buffer = new byte[8192];
GCHandle handle = GCHandle.Alloc(buffer, GCHandleType.Pinned);
if (((long)handle.AddrOfPinnedObject()) % 32 != 0)
{
_clearableArrayHandle = handle;
_clearableArray = buffer;
return;
}
handle.Free();
}
}

[GlobalCleanup(Target = nameof(ClearUnaligned))]
public void CleanupClearUnaligned() => _clearableArrayHandle.Free();

private GCHandle _clearableArrayHandle;
private byte[] _clearableArray;

[Benchmark]
public void ClearUnaligned() => Array.Clear(_clearableArray, 0, _clearableArray.Length);

private readonly char[] _indexOfCharArray = "This is a test of a reasonably long string to see how IndexOf works".ToCharArray();
private readonly short[] _indexOfShortArray = "This is a test of a reasonably long string to see how IndexOf works".Select(c => (short)c).ToArray();

[Benchmark]
public int IndexOfChar() => Array.IndexOf(_indexOfCharArray, '.');

[Benchmark]
public void IndexOfShort() => Array.IndexOf(_indexOfShortArray, (short)'.');
}
}
11 changes: 11 additions & 0 deletions src/benchmarks/micro/corefx/System.Runtime/Perf.Char.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,16 @@ public static IEnumerable<object[]> Char_ChangeCase_MemberData()
[Benchmark]
[ArgumentsSource(nameof(Char_ChangeCase_MemberData))]
public char Char_ToUpper(char c, CultureInfo cultureName)=> char.ToUpper(c, cultureName);

[Benchmark]
[ArgumentsSource(nameof(GetUnicodeCategoryArguments))]
public UnicodeCategory GetUnicodeCategory(char c) => char.GetUnicodeCategory(c);

public static IEnumerable<object[]> GetUnicodeCategoryArguments()
{
yield return new object[] { '.' };
yield return new object[] { 'a' };
yield return new object[] { '\x05D0' };
}
}
}
6 changes: 6 additions & 0 deletions src/benchmarks/micro/corefx/System.Runtime/Perf.DateTime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,11 @@ public static IEnumerable<string> ToString_MemberData()

[Benchmark]
public TimeSpan op_Subtraction() => date1 - date2;

[Benchmark]
public DateTime ParseR() => DateTime.ParseExact("Mon, 03 Jun 1996 22:15:00 GMT", "r", null);

[Benchmark]
public DateTime ParseO() => DateTime.ParseExact("1996-06-03T22:15:00.0000000", "o", null);
}
}
24 changes: 24 additions & 0 deletions src/benchmarks/micro/corefx/System.Runtime/Perf.Decimal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,29 @@ public static IEnumerable<object> StringValues
[Benchmark]
[ArgumentsSource(nameof(StringValues))]
public bool TryParse(string value) => decimal.TryParse(value, out _);

private decimal _a = 67891.2345m;
private decimal _b = 12345.6789m;

[Benchmark]
public decimal Add() => _a + _b;

[Benchmark]
public decimal Subtract() => _a - _b;

[Benchmark]
public decimal Multiply() => _a * _b;

[Benchmark]
public decimal Divide() => _a / _b;

[Benchmark]
public decimal Mod() => _a % _b;

[Benchmark]
public decimal Floor() => decimal.Floor(_a);

[Benchmark]
public decimal Round() => decimal.Round(_a);
}
}
16 changes: 16 additions & 0 deletions src/benchmarks/micro/corefx/System.Runtime/Perf.Enum.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// 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 BenchmarkDotNet.Attributes;
using MicroBenchmarks;

Expand All @@ -20,6 +21,16 @@ public enum Colors
Blue = 0x10
}

public enum ByteEnum : byte
{
A,
B
}

[Benchmark]
[Arguments(Colors.Yellow)]
public string EnumToString(Colors value) => value.ToString();

[Benchmark]
[Arguments("Red")]
[Arguments("Red, Orange, Yellow, Green, Blue")]
Expand All @@ -34,5 +45,10 @@ public enum Colors

[Benchmark]
public bool HasFlag() => _greenAndRed.HasFlag(Colors.Green);

private ByteEnum _byteEnum = ByteEnum.A;

[Benchmark]
public void Compare() => Comparer<ByteEnum>.Default.Compare(_byteEnum, _byteEnum);
}
}
Loading

0 comments on commit 7d96fd3

Please sign in to comment.