diff options
author | Stephen Toub <stoub@microsoft.com> | 2018-04-04 22:31:40 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-04-04 22:31:40 +0300 |
commit | 7a118af732b709543a35031f12138b4a9d1f73d1 (patch) | |
tree | 4dd65b4f1aa4a84a060cdcdfd701ae688622e672 /src | |
parent | 81e82818527c1fab743ced76ad33441f61af2636 (diff) |
Add perf tests for {U}Int32/64 ToString/TryFormat/Parse (#28812)
These are ports of the equivalent tests from the Utf8Formatter tests in System.Memory.
Diffstat (limited to 'src')
5 files changed, 339 insertions, 29 deletions
diff --git a/src/System.Runtime/tests/Performance/Perf.Int32.cs b/src/System.Runtime/tests/Performance/Perf.Int32.cs index 958eeedb48..0bb67c6b68 100644 --- a/src/System.Runtime/tests/Performance/Perf.Int32.cs +++ b/src/System.Runtime/tests/Performance/Perf.Int32.cs @@ -3,37 +3,79 @@ // See the LICENSE file in the project root for more information. using Microsoft.Xunit.Performance; +using Xunit; namespace System.Tests { public class Perf_Int32 { - [Benchmark] - public void ToString_() + private const int InnerCount = 500_000; + + private static string s_resultString; + private static int s_resultInt32; + + public static object[][] Int32Values => new[] + { + new object[] { 0 }, + new object[] { -1 }, + new object[] { 1 }, + new object[] { -1283 }, + new object[] { 1283 }, + new object[] { -12837467 }, + new object[] { 12837467 }, + new object[] { -2147483648 }, + new object[] { 2147483647 }, + }; + + [Benchmark(InnerIterationCount = InnerCount)] + [MemberData(nameof(Int32Values))] + public void ToString(int value) + { + Span<char> destination = new char[value.ToString().Length]; + foreach (BenchmarkIteration iteration in Benchmark.Iterations) + { + using (iteration.StartMeasurement()) + { + for (int i = 0; i < InnerCount; i++) + { + s_resultString = value.ToString(); + } + } + } + } + + [Benchmark(InnerIterationCount = InnerCount)] + [MemberData(nameof(Int32Values))] + public void TryFormat(int value) { - int i32 = 32; - foreach (var iteration in Benchmark.Iterations) + Span<char> destination = new char[value.ToString().Length]; + foreach (BenchmarkIteration iteration in Benchmark.Iterations) + { using (iteration.StartMeasurement()) - for (int i = 0; i < 10000; i++) + { + for (int i = 0; i < InnerCount; i++) { - i32.ToString(); i32.ToString(); i32.ToString(); - i32.ToString(); i32.ToString(); i32.ToString(); - i32.ToString(); i32.ToString(); i32.ToString(); + value.TryFormat(destination, out s_resultInt32); } + } + } } - [Benchmark] - public void Parse_str() + [Benchmark(InnerIterationCount = InnerCount)] + [MemberData(nameof(Int32Values))] + public void Parse(int value) { - string builtString = "1111111"; - foreach (var iteration in Benchmark.Iterations) + ReadOnlySpan<char> valueSpan = value.ToString(); + foreach (BenchmarkIteration iteration in Benchmark.Iterations) + { using (iteration.StartMeasurement()) - for (int i = 0; i < 10000; i++) + { + for (int i = 0; i < InnerCount; i++) { - int.Parse(builtString); int.Parse(builtString); int.Parse(builtString); - int.Parse(builtString); int.Parse(builtString); int.Parse(builtString); - int.Parse(builtString); int.Parse(builtString); int.Parse(builtString); + s_resultInt32 = int.Parse(valueSpan); } + } + } } } } diff --git a/src/System.Runtime/tests/Performance/Perf.Int64.cs b/src/System.Runtime/tests/Performance/Perf.Int64.cs new file mode 100644 index 0000000000..9f9833edb6 --- /dev/null +++ b/src/System.Runtime/tests/Performance/Perf.Int64.cs @@ -0,0 +1,116 @@ +// 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 Microsoft.Xunit.Performance; +using Xunit; + +namespace System.Tests +{ + public class Perf_Int64 + { + private const int InnerCount = 500_000; + + private static string s_resultString; + private static int s_resultInt32; + private static long s_resultInt64; + + public static object[][] Int64Values => new[] + { + new object[] { 214748364L }, + new object[] { 2L }, + new object[] { 21474836L }, + new object[] { 21474L }, + new object[] { 214L }, + new object[] { 2147L }, + new object[] { 214748L }, + new object[] { 21L }, + new object[] { 2147483L }, + new object[] { 922337203685477580L }, + new object[] { 92233720368547758L }, + new object[] { 9223372036854775L }, + new object[] { 922337203685477L }, + new object[] { 92233720368547L }, + new object[] { 9223372036854L }, + new object[] { 922337203685L }, + new object[] { 92233720368L }, + new object[] { -214748364L }, + new object[] { -2L }, + new object[] { -21474836L }, + new object[] { -21474L }, + new object[] { -214L }, + new object[] { -2147L }, + new object[] { -214748L }, + new object[] { -21L }, + new object[] { -2147483L }, + new object[] { -922337203685477580L }, + new object[] { -92233720368547758L }, + new object[] { -9223372036854775L }, + new object[] { -922337203685477L }, + new object[] { -92233720368547L }, + new object[] { -9223372036854L }, + new object[] { -922337203685L }, + new object[] { -92233720368L }, + new object[] { 0L }, + new object[] { -9223372036854775808L }, // min value + new object[] { 9223372036854775807L }, // max value + new object[] { -2147483648L }, // int32 min value + new object[] { 2147483647L }, // int32 max value + new object[] { -4294967295000000000L }, // -(uint.MaxValue * Billion) + new object[] { 4294967295000000000L }, // uint.MaxValue * Billion + new object[] { -4294967295000000001L }, // -(uint.MaxValue * Billion + 1) + new object[] { 4294967295000000001L }, // uint.MaxValue * Billion + 1 + }; + + [Benchmark(InnerIterationCount = InnerCount)] + [MemberData(nameof(Int64Values))] + public void ToString(long value) + { + Span<char> destination = new char[value.ToString().Length]; + foreach (BenchmarkIteration iteration in Benchmark.Iterations) + { + using (iteration.StartMeasurement()) + { + for (int i = 0; i < InnerCount; i++) + { + s_resultString = value.ToString(); + } + } + } + } + + [Benchmark(InnerIterationCount = InnerCount)] + [MemberData(nameof(Int64Values))] + public void TryFormat(long value) + { + Span<char> destination = new char[value.ToString().Length]; + foreach (BenchmarkIteration iteration in Benchmark.Iterations) + { + using (iteration.StartMeasurement()) + { + for (int i = 0; i < InnerCount; i++) + { + value.TryFormat(destination, out s_resultInt32); + } + } + } + } + + [Benchmark(InnerIterationCount = InnerCount)] + [MemberData(nameof(Int64Values))] + public void Parse(long value) + { + ReadOnlySpan<char> valueSpan = value.ToString(); + foreach (BenchmarkIteration iteration in Benchmark.Iterations) + { + using (iteration.StartMeasurement()) + { + for (int i = 0; i < InnerCount; i++) + { + s_resultInt64 = long.Parse(valueSpan); + } + } + } + } + } +} diff --git a/src/System.Runtime/tests/Performance/Perf.UInt32.cs b/src/System.Runtime/tests/Performance/Perf.UInt32.cs index 129286bb2e..e9af2fe97b 100644 --- a/src/System.Runtime/tests/Performance/Perf.UInt32.cs +++ b/src/System.Runtime/tests/Performance/Perf.UInt32.cs @@ -3,23 +3,76 @@ // See the LICENSE file in the project root for more information. using Microsoft.Xunit.Performance; +using Xunit; namespace System.Tests { public class Perf_UInt32 { - [Benchmark] - public void ToString_() + private const int InnerCount = 500_000; + + private static string s_resultString; + private static int s_resultInt32; + private static uint s_resultUInt32; + + public static object[][] UInt32Values => new[] + { + new object[] { 0u }, + new object[] { 1u }, + new object[] { 1283u }, + new object[] { 12837467u }, + new object[] { 4294967295u }, + }; + + [Benchmark(InnerIterationCount = InnerCount)] + [MemberData(nameof(UInt32Values))] + public void ToString(uint value) + { + Span<char> destination = new char[value.ToString().Length]; + foreach (BenchmarkIteration iteration in Benchmark.Iterations) + { + using (iteration.StartMeasurement()) + { + for (int i = 0; i < InnerCount; i++) + { + s_resultString = value.ToString(); + } + } + } + } + + [Benchmark(InnerIterationCount = InnerCount)] + [MemberData(nameof(UInt32Values))] + public void TryFormat(uint value) + { + Span<char> destination = new char[value.ToString().Length]; + foreach (BenchmarkIteration iteration in Benchmark.Iterations) + { + using (iteration.StartMeasurement()) + { + for (int i = 0; i < InnerCount; i++) + { + value.TryFormat(destination, out s_resultInt32); + } + } + } + } + + [Benchmark(InnerIterationCount = InnerCount)] + [MemberData(nameof(UInt32Values))] + public void Parse(uint value) { - uint testint = new uint(); - foreach (var iteration in Benchmark.Iterations) + ReadOnlySpan<char> valueSpan = value.ToString(); + foreach (BenchmarkIteration iteration in Benchmark.Iterations) + { using (iteration.StartMeasurement()) - for (int i = 0; i < 10000; i++) + { + for (int i = 0; i < InnerCount; i++) { - testint.ToString(); testint.ToString(); testint.ToString(); - testint.ToString(); testint.ToString(); testint.ToString(); - testint.ToString(); testint.ToString(); testint.ToString(); + s_resultUInt32 = uint.Parse(valueSpan); } + } + } } } } diff --git a/src/System.Runtime/tests/Performance/Perf.UInt64.cs b/src/System.Runtime/tests/Performance/Perf.UInt64.cs new file mode 100644 index 0000000000..c8bdbea263 --- /dev/null +++ b/src/System.Runtime/tests/Performance/Perf.UInt64.cs @@ -0,0 +1,97 @@ +// 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 Microsoft.Xunit.Performance; +using Xunit; + +namespace System.Tests +{ + public class Perf_UInt64 + { + private const int InnerCount = 100_000; + + private static string s_resultString; + private static int s_resultInt32; + private static ulong s_resultUInt64; + + public static object[][] UInt64Values => new[] + { + new object[] { 214748364LU }, + new object[] { 2LU }, + new object[] { 21474836LU }, + new object[] { 21474LU }, + new object[] { 214LU }, + new object[] { 2147LU }, + new object[] { 214748LU }, + new object[] { 21LU }, + new object[] { 2147483LU }, + new object[] { 922337203685477580LU }, + new object[] { 92233720368547758LU }, + new object[] { 9223372036854775LU }, + new object[] { 922337203685477LU }, + new object[] { 92233720368547LU }, + new object[] { 9223372036854LU }, + new object[] { 922337203685LU }, + new object[] { 92233720368LU }, + new object[] { 0LU }, // min value + new object[] { 18446744073709551615LU }, // max value + new object[] { 2147483647LU }, // int32 max value + new object[] { 9223372036854775807LU }, // int64 max value + new object[] { 1000000000000000000LU }, // quintillion + new object[] { 4294967295000000000LU }, // uint.MaxValue * Billion + new object[] { 4294967295000000001LU }, // uint.MaxValue * Billion + 1 + }; + + [Benchmark(InnerIterationCount = InnerCount)] + [MemberData(nameof(UInt64Values))] + public void ToString(ulong value) + { + Span<char> destination = new char[value.ToString().Length]; + foreach (BenchmarkIteration iteration in Benchmark.Iterations) + { + using (iteration.StartMeasurement()) + { + for (int i = 0; i < InnerCount; i++) + { + s_resultString = value.ToString(); + } + } + } + } + + [Benchmark(InnerIterationCount = InnerCount)] + [MemberData(nameof(UInt64Values))] + public void TryFormat(ulong value) + { + Span<char> destination = new char[value.ToString().Length]; + foreach (BenchmarkIteration iteration in Benchmark.Iterations) + { + using (iteration.StartMeasurement()) + { + for (int i = 0; i < InnerCount; i++) + { + value.TryFormat(destination, out s_resultInt32); + } + } + } + } + + [Benchmark(InnerIterationCount = InnerCount)] + [MemberData(nameof(UInt64Values))] + public void Parse(ulong value) + { + ReadOnlySpan<char> valueSpan = value.ToString(); + foreach (BenchmarkIteration iteration in Benchmark.Iterations) + { + using (iteration.StartMeasurement()) + { + for (int i = 0; i < InnerCount; i++) + { + s_resultUInt64 = ulong.Parse(valueSpan); + } + } + } + } + } +} diff --git a/src/System.Runtime/tests/Performance/System.Runtime.Performance.Tests.csproj b/src/System.Runtime/tests/Performance/System.Runtime.Performance.Tests.csproj index 54adbd2672..fb012aa9de 100644 --- a/src/System.Runtime/tests/Performance/System.Runtime.Performance.Tests.csproj +++ b/src/System.Runtime/tests/Performance/System.Runtime.Performance.Tests.csproj @@ -12,19 +12,21 @@ <ItemGroup> <Compile Include="Perf.Boolean.cs" /> <Compile Include="Perf.Char.cs" /> + <Compile Include="Perf.DateTime.cs" /> <Compile Include="Perf.Double.cs" /> <Compile Include="Perf.Enum.cs" /> <Compile Include="Perf.Guid.cs" /> <Compile Include="Perf.HashCode.cs" /> + <Compile Include="Perf.Int32.cs" /> + <Compile Include="Perf.Int64.cs" /> + <Compile Include="Perf.IntPtr.cs" /> <Compile Include="Perf.Object.cs" /> <Compile Include="Perf.String.cs" /> + <Compile Include="Perf.StringBuilder.cs" /> <Compile Include="Perf.TimeSpan.cs" /> <Compile Include="Perf.Type.cs" /> <Compile Include="Perf.UInt32.cs" /> - <Compile Include="Perf.DateTime.cs" /> - <Compile Include="Perf.Int32.cs" /> - <Compile Include="Perf.IntPtr.cs" /> - <Compile Include="Perf.StringBuilder.cs" /> + <Compile Include="Perf.UInt64.cs" /> <Compile Include="$(CommonTestPath)\System\PerfUtils.cs"> <Link>Common\System\PerfUtils.cs</Link> </Compile> @@ -42,4 +44,4 @@ <Service Include="{82A7F48D-3B50-4B1E-B82E-3ADA8210C358}" /> </ItemGroup> <Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), dir.targets))\dir.targets" /> -</Project>
\ No newline at end of file +</Project> |