From 4b4aad7217d3292650e77eec2cf4c198ea9c3b4b Mon Sep 17 00:00:00 2001 From: Jiyoung Yun Date: Wed, 23 Nov 2016 19:09:09 +0900 Subject: Imported Upstream version 1.1.0 --- .../CodeQuality/BenchI/8Queens/8Queens.cs | 100 ++++++ .../CodeQuality/BenchI/8Queens/8Queens.csproj | 43 +++ .../CodeQuality/BenchI/Ackermann/Ackermann.cs | 67 ++++ .../CodeQuality/BenchI/Ackermann/Ackermann.csproj | 43 +++ .../CodeQuality/BenchI/AddArray/AddArray.cs | 91 +++++ .../CodeQuality/BenchI/AddArray/AddArray.csproj | 43 +++ .../CodeQuality/BenchI/AddArray2/AddArray2.cs | 131 +++++++ .../CodeQuality/BenchI/AddArray2/AddArray2.csproj | 43 +++ .../CodeQuality/BenchI/Array1/Array1.cs | 154 ++++++++ .../CodeQuality/BenchI/Array1/Array1.csproj | 43 +++ .../CodeQuality/BenchI/Array2/Array2.cs | 100 ++++++ .../CodeQuality/BenchI/Array2/Array2.csproj | 43 +++ .../CodeQuality/BenchI/BenchE/BenchE.cs | 117 ++++++ .../CodeQuality/BenchI/BenchE/BenchE.csproj | 43 +++ .../CodeQuality/BenchI/BubbleSort/BubbleSort.cs | 87 +++++ .../BenchI/BubbleSort/BubbleSort.csproj | 43 +++ .../CodeQuality/BenchI/BubbleSort2/BubbleSort2.cs | 90 +++++ .../BenchI/BubbleSort2/BubbleSort2.csproj | 43 +++ .../CodeQuality/BenchI/CSieve/CSieve.cs | 82 +++++ .../CodeQuality/BenchI/CSieve/CSieve.csproj | 43 +++ .../JIT/Performance/CodeQuality/BenchI/Fib/Fib.cs | 63 ++++ .../Performance/CodeQuality/BenchI/Fib/Fib.csproj | 43 +++ .../CodeQuality/BenchI/HeapSort/HeapSort.cs | 121 +++++++ .../CodeQuality/BenchI/HeapSort/HeapSort.csproj | 43 +++ .../CodeQuality/BenchI/IniArray/IniArray.cs | 60 ++++ .../CodeQuality/BenchI/IniArray/IniArray.csproj | 43 +++ .../CodeQuality/BenchI/LogicArray/LogicArray.cs | 97 +++++ .../BenchI/LogicArray/LogicArray.csproj | 43 +++ .../CodeQuality/BenchI/Midpoint/Midpoint.cs | 104 ++++++ .../CodeQuality/BenchI/Midpoint/Midpoint.csproj | 43 +++ .../CodeQuality/BenchI/MulMatrix/MulMatrix.cs | 140 ++++++++ .../CodeQuality/BenchI/MulMatrix/MulMatrix.csproj | 43 +++ .../CodeQuality/BenchI/NDhrystone/NDhrystone.cs | 286 +++++++++++++++ .../BenchI/NDhrystone/NDhrystone.csproj | 43 +++ .../CodeQuality/BenchI/Permutate/Permutate.cs | 116 ++++++ .../CodeQuality/BenchI/Permutate/Permutate.csproj | 43 +++ .../JIT/Performance/CodeQuality/BenchI/Pi/Pi.cs | 86 +++++ .../Performance/CodeQuality/BenchI/Pi/Pi.csproj | 43 +++ .../CodeQuality/BenchI/Puzzle/Puzzle.cs | 393 +++++++++++++++++++++ .../CodeQuality/BenchI/Puzzle/Puzzle.csproj | 43 +++ .../CodeQuality/BenchI/QuickSort/QuickSort.cs | 116 ++++++ .../CodeQuality/BenchI/QuickSort/QuickSort.csproj | 43 +++ .../CodeQuality/BenchI/TreeInsert/TreeInsert.cs | 138 ++++++++ .../BenchI/TreeInsert/TreeInsert.csproj | 43 +++ .../CodeQuality/BenchI/TreeSort/TreeSort.cs | 151 ++++++++ .../CodeQuality/BenchI/TreeSort/TreeSort.csproj | 43 +++ .../CodeQuality/BenchI/XposMatrix/XposMatrix.cs | 90 +++++ .../BenchI/XposMatrix/XposMatrix.csproj | 43 +++ 48 files changed, 4012 insertions(+) create mode 100644 tests/src/JIT/Performance/CodeQuality/BenchI/8Queens/8Queens.cs create mode 100644 tests/src/JIT/Performance/CodeQuality/BenchI/8Queens/8Queens.csproj create mode 100644 tests/src/JIT/Performance/CodeQuality/BenchI/Ackermann/Ackermann.cs create mode 100644 tests/src/JIT/Performance/CodeQuality/BenchI/Ackermann/Ackermann.csproj create mode 100644 tests/src/JIT/Performance/CodeQuality/BenchI/AddArray/AddArray.cs create mode 100644 tests/src/JIT/Performance/CodeQuality/BenchI/AddArray/AddArray.csproj create mode 100644 tests/src/JIT/Performance/CodeQuality/BenchI/AddArray2/AddArray2.cs create mode 100644 tests/src/JIT/Performance/CodeQuality/BenchI/AddArray2/AddArray2.csproj create mode 100644 tests/src/JIT/Performance/CodeQuality/BenchI/Array1/Array1.cs create mode 100644 tests/src/JIT/Performance/CodeQuality/BenchI/Array1/Array1.csproj create mode 100644 tests/src/JIT/Performance/CodeQuality/BenchI/Array2/Array2.cs create mode 100644 tests/src/JIT/Performance/CodeQuality/BenchI/Array2/Array2.csproj create mode 100644 tests/src/JIT/Performance/CodeQuality/BenchI/BenchE/BenchE.cs create mode 100644 tests/src/JIT/Performance/CodeQuality/BenchI/BenchE/BenchE.csproj create mode 100644 tests/src/JIT/Performance/CodeQuality/BenchI/BubbleSort/BubbleSort.cs create mode 100644 tests/src/JIT/Performance/CodeQuality/BenchI/BubbleSort/BubbleSort.csproj create mode 100644 tests/src/JIT/Performance/CodeQuality/BenchI/BubbleSort2/BubbleSort2.cs create mode 100644 tests/src/JIT/Performance/CodeQuality/BenchI/BubbleSort2/BubbleSort2.csproj create mode 100644 tests/src/JIT/Performance/CodeQuality/BenchI/CSieve/CSieve.cs create mode 100644 tests/src/JIT/Performance/CodeQuality/BenchI/CSieve/CSieve.csproj create mode 100644 tests/src/JIT/Performance/CodeQuality/BenchI/Fib/Fib.cs create mode 100644 tests/src/JIT/Performance/CodeQuality/BenchI/Fib/Fib.csproj create mode 100644 tests/src/JIT/Performance/CodeQuality/BenchI/HeapSort/HeapSort.cs create mode 100644 tests/src/JIT/Performance/CodeQuality/BenchI/HeapSort/HeapSort.csproj create mode 100644 tests/src/JIT/Performance/CodeQuality/BenchI/IniArray/IniArray.cs create mode 100644 tests/src/JIT/Performance/CodeQuality/BenchI/IniArray/IniArray.csproj create mode 100644 tests/src/JIT/Performance/CodeQuality/BenchI/LogicArray/LogicArray.cs create mode 100644 tests/src/JIT/Performance/CodeQuality/BenchI/LogicArray/LogicArray.csproj create mode 100644 tests/src/JIT/Performance/CodeQuality/BenchI/Midpoint/Midpoint.cs create mode 100644 tests/src/JIT/Performance/CodeQuality/BenchI/Midpoint/Midpoint.csproj create mode 100644 tests/src/JIT/Performance/CodeQuality/BenchI/MulMatrix/MulMatrix.cs create mode 100644 tests/src/JIT/Performance/CodeQuality/BenchI/MulMatrix/MulMatrix.csproj create mode 100644 tests/src/JIT/Performance/CodeQuality/BenchI/NDhrystone/NDhrystone.cs create mode 100644 tests/src/JIT/Performance/CodeQuality/BenchI/NDhrystone/NDhrystone.csproj create mode 100644 tests/src/JIT/Performance/CodeQuality/BenchI/Permutate/Permutate.cs create mode 100644 tests/src/JIT/Performance/CodeQuality/BenchI/Permutate/Permutate.csproj create mode 100644 tests/src/JIT/Performance/CodeQuality/BenchI/Pi/Pi.cs create mode 100644 tests/src/JIT/Performance/CodeQuality/BenchI/Pi/Pi.csproj create mode 100644 tests/src/JIT/Performance/CodeQuality/BenchI/Puzzle/Puzzle.cs create mode 100644 tests/src/JIT/Performance/CodeQuality/BenchI/Puzzle/Puzzle.csproj create mode 100644 tests/src/JIT/Performance/CodeQuality/BenchI/QuickSort/QuickSort.cs create mode 100644 tests/src/JIT/Performance/CodeQuality/BenchI/QuickSort/QuickSort.csproj create mode 100644 tests/src/JIT/Performance/CodeQuality/BenchI/TreeInsert/TreeInsert.cs create mode 100644 tests/src/JIT/Performance/CodeQuality/BenchI/TreeInsert/TreeInsert.csproj create mode 100644 tests/src/JIT/Performance/CodeQuality/BenchI/TreeSort/TreeSort.cs create mode 100644 tests/src/JIT/Performance/CodeQuality/BenchI/TreeSort/TreeSort.csproj create mode 100644 tests/src/JIT/Performance/CodeQuality/BenchI/XposMatrix/XposMatrix.cs create mode 100644 tests/src/JIT/Performance/CodeQuality/BenchI/XposMatrix/XposMatrix.csproj (limited to 'tests/src/JIT/Performance/CodeQuality/BenchI') diff --git a/tests/src/JIT/Performance/CodeQuality/BenchI/8Queens/8Queens.cs b/tests/src/JIT/Performance/CodeQuality/BenchI/8Queens/8Queens.cs new file mode 100644 index 0000000000..76ed0c8e71 --- /dev/null +++ b/tests/src/JIT/Performance/CodeQuality/BenchI/8Queens/8Queens.cs @@ -0,0 +1,100 @@ +// 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 System; +using System.Runtime.CompilerServices; +using Xunit; + +[assembly: OptimizeForBenchmarks] +[assembly: MeasureInstructionsRetired] + +public static class EightQueens +{ + +#if DEBUG + public const int Iterations = 1; +#else + public const int Iterations = 100000; +#endif + + static int[] m_c = new int[15]; + static int[] m_x = new int[9]; + + static void TryMe(int i, ref int q, int[] a, int[] b) + { + int j = 0; + q = 0; + while ((q == 0) && (j != 8)) { + j = j + 1; + q = 0; + if ((b[j] == 1) && (a[i + j] == 1) && (m_c[i - j + 7] == 1)) { + m_x[i] = j; + b[j] = 0; + a[i + j] = 0; + m_c[i - j + 7] = 0; + if (i < 8) { + TryMe(i + 1, ref q, a, b); + if (q == 0) { + b[j] = 1; + a[i + j] = 1; + m_c[i - j + 7] = 1; + } + } + else { + q = 1; + } + } + } + } + + [MethodImpl(MethodImplOptions.NoInlining)] + static bool Bench() { + int[] a = new int[9]; + int[] b = new int[17]; + int q = 0; + int i = 0; + while (i <= 16) { + if ((i >= 1) && (i <= 8)) { + a[i] = 1; + } + if (i >= 2) { + b[i] = 1; + } + if (i <= 14) { + m_c[i] = 1; + } + i = i + 1; + } + + TryMe(1, ref q, b, a); + + return (q == 1); + } + + [Benchmark] + public static void Test() { + foreach (var iteration in Benchmark.Iterations) { + using (iteration.StartMeasurement()) { + for (int i = 0; i < Iterations; i++) { + Bench(); + } + } + } + } + + static bool TestBase() { + bool result = true; + for (int i = 0; i < Iterations; i++) { + result &= Bench(); + } + return result; + } + + public static int Main() { + bool result = TestBase(); + return (result ? 100 : -1); + } +} diff --git a/tests/src/JIT/Performance/CodeQuality/BenchI/8Queens/8Queens.csproj b/tests/src/JIT/Performance/CodeQuality/BenchI/8Queens/8Queens.csproj new file mode 100644 index 0000000000..efef461379 --- /dev/null +++ b/tests/src/JIT/Performance/CodeQuality/BenchI/8Queens/8Queens.csproj @@ -0,0 +1,43 @@ + + + + + Debug + AnyCPU + 2.0 + {95DFC527-4DC1-495E-97D7-E94EE1F7140D} + Exe + Properties + 512 + {786C830F-07A1-408B-BD7F-6EE04809D6DB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} + $(ProgramFiles)\Common Files\microsoft shared\VSTT\11.0\UITestExtensionPackages + ..\..\ + 7a9bfb7d + .NETStandard,Version=v1.4 + + + + + + pdbonly + true + + + + False + + + + + + + + + + $(JitPackagesConfigFileDirectory)benchmark\project.json + $(JitPackagesConfigFileDirectory)benchmark\project.lock.json + + + + + diff --git a/tests/src/JIT/Performance/CodeQuality/BenchI/Ackermann/Ackermann.cs b/tests/src/JIT/Performance/CodeQuality/BenchI/Ackermann/Ackermann.cs new file mode 100644 index 0000000000..512f35f82d --- /dev/null +++ b/tests/src/JIT/Performance/CodeQuality/BenchI/Ackermann/Ackermann.cs @@ -0,0 +1,67 @@ +// 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 System; +using System.Runtime.CompilerServices; +using Xunit; + +[assembly: OptimizeForBenchmarks] +[assembly: MeasureInstructionsRetired] + +public static class Ackermann +{ + +#if DEBUG + public const int Iterations = 1; +#else + public const int Iterations = 100000; +#endif + + static int Acker(int m, int n) { + if (m == 0) { + return n + 1; + } + else if (n == 0) { + return Acker(m - 1, 1); + } + else { + return Acker(m - 1, Acker(m, n - 1)); + } + } + + [MethodImpl(MethodImplOptions.NoInlining)] + static bool Bench() { + int a00 = Acker(0, 0); + int a11 = Acker(1, 1); + int a22 = Acker(2, 2); + int a33 = Acker(3, 3); + return (a00 == 1) && (a11 == 3) && (a22 == 7) & (a33 == 61); + } + + [Benchmark] + public static void Test() { + foreach (var iteration in Benchmark.Iterations) { + using (iteration.StartMeasurement()) { + for (int i = 0; i < Iterations; i++) { + Bench(); + } + } + } + } + + static bool TestBase() { + bool result = true; + for (int i = 0; i < Iterations; i++) { + result &= Bench(); + } + return result; + } + + public static int Main() { + bool result = TestBase(); + return (result ? 100 : -1); + } +} diff --git a/tests/src/JIT/Performance/CodeQuality/BenchI/Ackermann/Ackermann.csproj b/tests/src/JIT/Performance/CodeQuality/BenchI/Ackermann/Ackermann.csproj new file mode 100644 index 0000000000..8d0feb5a01 --- /dev/null +++ b/tests/src/JIT/Performance/CodeQuality/BenchI/Ackermann/Ackermann.csproj @@ -0,0 +1,43 @@ + + + + + Debug + AnyCPU + 2.0 + {95DFC527-4DC1-495E-97D7-E94EE1F7140D} + Exe + Properties + 512 + {786C830F-07A1-408B-BD7F-6EE04809D6DB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} + $(ProgramFiles)\Common Files\microsoft shared\VSTT\11.0\UITestExtensionPackages + ..\..\ + 7a9bfb7d + .NETStandard,Version=v1.4 + + + + + + pdbonly + true + + + + False + + + + + + + + + + $(JitPackagesConfigFileDirectory)benchmark\project.json + $(JitPackagesConfigFileDirectory)benchmark\project.lock.json + + + + + diff --git a/tests/src/JIT/Performance/CodeQuality/BenchI/AddArray/AddArray.cs b/tests/src/JIT/Performance/CodeQuality/BenchI/AddArray/AddArray.cs new file mode 100644 index 0000000000..0fa2ff90eb --- /dev/null +++ b/tests/src/JIT/Performance/CodeQuality/BenchI/AddArray/AddArray.cs @@ -0,0 +1,91 @@ +// 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 System; +using System.Runtime.CompilerServices; +using Xunit; + +[assembly: OptimizeForBenchmarks] +[assembly: MeasureInstructionsRetired] + +public static class AddArray +{ + +#if DEBUG + public const int Iterations = 1; +#else + public const int Iterations = 15000; +#endif + + const int Size = 6000; + + public static volatile object VolatileObject; + + [MethodImpl(MethodImplOptions.NoInlining)] + static void Escape(object obj) { + VolatileObject = obj; + } + + [MethodImpl(MethodImplOptions.NoInlining)] + static bool Bench() { + + int[] flags1 = new int[Size + 1]; + int[] flags2 = new int[Size + 1]; + int[] flags3 = new int[Size + 1]; + int[] flags4 = new int[Size + 1]; + + int j, k, l, m; + + for (j = 0; j <= Size; j++) { + flags1[j] = 70000 + j; + k = j; + flags2[k] = flags1[j] + k + k; + l = j; + flags3[l] = flags2[k] + l + l + l; + m = j; + flags4[m] = flags3[l] + m + m + m + m; + } + + for (j = 0; j <= Size; j++) { + k = j; + l = j; + m = j; + flags1[j] = flags1[j] + flags2[k] + flags3[l] + flags4[m] - flags2[k - j + l]; + } + + // Escape each flags array so that their elements will appear live-out + Escape(flags1); + Escape(flags2); + Escape(flags3); + Escape(flags4); + + return true; + } + + [Benchmark] + public static void Test() { + foreach (var iteration in Benchmark.Iterations) { + using (iteration.StartMeasurement()) { + for (int i = 0; i < Iterations; i++) { + Bench(); + } + } + } + } + + static bool TestBase() { + bool result = true; + for (int i = 0; i < Iterations; i++) { + result &= Bench(); + } + return result; + } + + public static int Main() { + bool result = TestBase(); + return (result ? 100 : -1); + } +} diff --git a/tests/src/JIT/Performance/CodeQuality/BenchI/AddArray/AddArray.csproj b/tests/src/JIT/Performance/CodeQuality/BenchI/AddArray/AddArray.csproj new file mode 100644 index 0000000000..2fd1aae521 --- /dev/null +++ b/tests/src/JIT/Performance/CodeQuality/BenchI/AddArray/AddArray.csproj @@ -0,0 +1,43 @@ + + + + + Debug + AnyCPU + 2.0 + {95DFC527-4DC1-495E-97D7-E94EE1F7140D} + Exe + Properties + 512 + {786C830F-07A1-408B-BD7F-6EE04809D6DB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} + $(ProgramFiles)\Common Files\microsoft shared\VSTT\11.0\UITestExtensionPackages + ..\..\ + 7a9bfb7d + .NETStandard,Version=v1.4 + + + + + + pdbonly + true + + + + False + + + + + + + + + + $(JitPackagesConfigFileDirectory)benchmark\project.json + $(JitPackagesConfigFileDirectory)benchmark\project.lock.json + + + + + diff --git a/tests/src/JIT/Performance/CodeQuality/BenchI/AddArray2/AddArray2.cs b/tests/src/JIT/Performance/CodeQuality/BenchI/AddArray2/AddArray2.cs new file mode 100644 index 0000000000..865b9445f6 --- /dev/null +++ b/tests/src/JIT/Performance/CodeQuality/BenchI/AddArray2/AddArray2.cs @@ -0,0 +1,131 @@ +// 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 System; +using System.Runtime.CompilerServices; +using Xunit; + +[assembly: OptimizeForBenchmarks] +[assembly: MeasureInstructionsRetired] + +public static class AddArray2 +{ +#if DEBUG + public const int Iterations = 1; +#else + public const int Iterations = 50; +#endif + + private const int Dim = 200; + + private static T[][] AllocArray(int n1, int n2) + { + T[][] a = new T[n1][]; + for (int i = 0; i < n1; ++i) + { + a[i] = new T[n2]; + } + return a; + } + + private static + void BenchInner1(int[][] a, ref int nn) + { + int n; + int l, m; + n = nn; + for (int i = 1; i <= n; i++) + { + for (int j = (i + 1); j <= n; j++) + { + for (int k = 1; k <= n; k++) + { + l = a[i][k]; + m = a[j][k]; + unchecked + { + a[j][k] = l + m; + } + } + } + } + } + + private static + void BenchInner2(int[][] a, ref int nn) + { + int n; + int l, m; + n = nn; + for (int i = 1; i <= n; i++) + { + for (int j = (i + 1); j <= n; j++) + { + for (int k = 1; k <= n; k++) + { + l = a[k][i]; + m = a[k][j]; + unchecked + { + a[k][j] = l + m; + } + } + } + } + } + + [MethodImpl(MethodImplOptions.NoInlining)] + private static bool Bench(int[][] a) + { + int n = Dim; + for (int i = 1; i <= n; i++) + { + for (int j = 1; j <= n; j++) + { + a[i][j] = i + j; + } + } + + BenchInner1(a, ref n); + n = Dim; + BenchInner2(a, ref n); + + return true; + } + + [Benchmark] + public static void Test() + { + int[][] array = AllocArray(Dim + 1, Dim + 1); + foreach (var iteration in Benchmark.Iterations) + { + using (iteration.StartMeasurement()) + { + for (int i = 1; i <= Iterations; i++) + { + Bench(array); + } + } + } + } + + private static bool TestBase() + { + int[][] array = AllocArray(Dim + 1, Dim + 1); + bool result = true; + for (int i = 1; i <= Iterations; i++) + { + result &= Bench(array); + } + return result; + } + + public static int Main() + { + bool result = TestBase(); + return (result ? 100 : -1); + } +} diff --git a/tests/src/JIT/Performance/CodeQuality/BenchI/AddArray2/AddArray2.csproj b/tests/src/JIT/Performance/CodeQuality/BenchI/AddArray2/AddArray2.csproj new file mode 100644 index 0000000000..cb8f12ddb2 --- /dev/null +++ b/tests/src/JIT/Performance/CodeQuality/BenchI/AddArray2/AddArray2.csproj @@ -0,0 +1,43 @@ + + + + + Debug + AnyCPU + 2.0 + {95DFC527-4DC1-495E-97D7-E94EE1F7140D} + Exe + Properties + 512 + {786C830F-07A1-408B-BD7F-6EE04809D6DB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} + $(ProgramFiles)\Common Files\microsoft shared\VSTT\11.0\UITestExtensionPackages + ..\..\ + 7a9bfb7d + .NETStandard,Version=v1.4 + + + + + + pdbonly + true + + + + False + + + + + + + + + + $(JitPackagesConfigFileDirectory)benchmark\project.json + $(JitPackagesConfigFileDirectory)benchmark\project.lock.json + + + + + diff --git a/tests/src/JIT/Performance/CodeQuality/BenchI/Array1/Array1.cs b/tests/src/JIT/Performance/CodeQuality/BenchI/Array1/Array1.cs new file mode 100644 index 0000000000..8d5dbd6153 --- /dev/null +++ b/tests/src/JIT/Performance/CodeQuality/BenchI/Array1/Array1.cs @@ -0,0 +1,154 @@ +// 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. +// +// The sorting benchmark calls a random number generator the number +// of times specified by Maxnum to create an array of int integers, +// then does a quicksort on the array of ints. Random numbers +// are produced using a multiplicative modulus method with known +// seed, so that the generated array is constant across compilers. +// +// This is adapted from a benchmark in BYTE Magazine, August 1984. + +using Microsoft.Xunit.Performance; +using System; +using System.Runtime.CompilerServices; +using Xunit; + +[assembly: OptimizeForBenchmarks] +[assembly: MeasureInstructionsRetired] + +public static class Array1 +{ +#if DEBUG + private const int Iterations = 1; + private const int Maxnum = 100; +#else + private const int Iterations = 125; + private const int Maxnum = 1000; +#endif + + private const int Modulus = ((int)0x20000); + private const int C = 13849; + private const int A = 25173; + static int s_seed = 7; + + private static void Quick(int lo, int hi, int[] input) + { + int i, j; + int pivot, temp; + + if (lo < hi) + { + // 0 <= lo < hi + for (i = lo, j = (hi + 1), pivot = input[lo]; ;) + { + do + { + ++i; + } while (input[i] < pivot); + + do + { + --j; + // Accessing upto hi + } while (input[j] > pivot); + + if (i < j) + { + temp = input[i]; + input[i] = input[j]; + input[j] = temp; + } + else + { + break; + } + } + temp = input[j]; + input[j] = input[lo]; + input[lo] = temp; + Quick(lo, j - 1, input); + Quick(j + 1, hi, input); + } + } + + private static int Random(int size) + { + unchecked + { + s_seed = s_seed * A + C; + } + + return (s_seed % size); + } + + private static bool VerifySort(int[] buffer) + { + for (int y = 0; y < Maxnum - 2; y++) + { + if (buffer[y] > buffer[y + 1]) + { + return false; + } + } + return true; + } + + [MethodImpl(MethodImplOptions.NoInlining)] + private static bool Bench() + { + int[] buffer = new int[Maxnum + 1]; + + for (int i = 0; i < Iterations; ++i) + { + for (int j = 0; j < Maxnum; ++j) + { + int temp = Random(Modulus); + if (temp < 0L) + { + temp = (-temp); + } + buffer[j] = temp; + } + buffer[Maxnum] = Modulus; + + Quick(0, Maxnum - 1, buffer); + } + + bool result = VerifySort(buffer); + + return result; + } + + [Benchmark] + public static void Test() + { + foreach (var iteration in Benchmark.Iterations) + { + using (iteration.StartMeasurement()) + { + for (int i = 0; i < Iterations; i++) + { + Bench(); + } + } + } + } + + private static bool TestBase() + { + bool result = true; + for (int i = 0; i < Iterations; i++) + { + result &= Bench(); + } + return result; + } + + public static int Main() + { + bool result = TestBase(); + return (result ? 100 : -1); + } +} diff --git a/tests/src/JIT/Performance/CodeQuality/BenchI/Array1/Array1.csproj b/tests/src/JIT/Performance/CodeQuality/BenchI/Array1/Array1.csproj new file mode 100644 index 0000000000..b7d7918823 --- /dev/null +++ b/tests/src/JIT/Performance/CodeQuality/BenchI/Array1/Array1.csproj @@ -0,0 +1,43 @@ + + + + + Debug + AnyCPU + 2.0 + {95DFC527-4DC1-495E-97D7-E94EE1F7140D} + Exe + Properties + 512 + {786C830F-07A1-408B-BD7F-6EE04809D6DB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} + $(ProgramFiles)\Common Files\microsoft shared\VSTT\11.0\UITestExtensionPackages + ..\..\ + 7a9bfb7d + .NETStandard,Version=v1.4 + + + + + + pdbonly + true + + + + False + + + + + + + + + + $(JitPackagesConfigFileDirectory)benchmark\project.json + $(JitPackagesConfigFileDirectory)benchmark\project.lock.json + + + + + diff --git a/tests/src/JIT/Performance/CodeQuality/BenchI/Array2/Array2.cs b/tests/src/JIT/Performance/CodeQuality/BenchI/Array2/Array2.cs new file mode 100644 index 0000000000..1b720b94fa --- /dev/null +++ b/tests/src/JIT/Performance/CodeQuality/BenchI/Array2/Array2.cs @@ -0,0 +1,100 @@ +// 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 System; +using System.Runtime.CompilerServices; +using Xunit; + +[assembly: OptimizeForBenchmarks] +[assembly: MeasureInstructionsRetired] + +public static class Array2 +{ + +#if DEBUG + public const int Iterations = 1; +#else + public const int Iterations = 500000; +#endif + + static T[][][] AllocArray(int n1, int n2, int n3) { + T[][][] a = new T[n1][][]; + for (int i = 0; i < n1; ++i) { + a[i] = new T[n2][]; + for (int j = 0; j < n2; j++) { + a[i][j] = new T[n3]; + } + } + + return a; + } + + static void Initialize(int[][][] s) { + for (int i = 0; i < 10; i++) { + for (int j = 0; j < 10; j++) { + for (int k = 0; k < 10; k++) { + s[i][j][k] = (2 * i) - (3 * j) + (5 * k); + } + } + } + } + + static bool VerifyCopy(int[][][] s, int[][][] d) { + for (int i = 0; i < 10; i++) { + for (int j = 0; j < 10; j++) { + for (int k = 0; k < 10; k++) { + if (s[i][j][k] != d[i][j][k]) { + return false; + } + } + } + } + + return true; + } + + [MethodImpl(MethodImplOptions.NoInlining)] + static bool Bench(int loop) { + + int[][][] s = AllocArray(10, 10, 10); + int[][][] d = AllocArray(10, 10, 10); + + Initialize(s); + + for (; loop != 0; loop--) { + for (int i = 0; i < 10; i++) { + for (int j = 0; j < 10; j++) { + for (int k = 0; k < 10; k++) { + d[i][j][k] = s[i][j][k]; + } + } + } + } + + bool result = VerifyCopy(s, d); + + return result; + } + + [Benchmark] + public static void Test() { + foreach (var iteration in Benchmark.Iterations) { + using (iteration.StartMeasurement()) { + Bench(Iterations); + } + } + } + + static bool TestBase() { + bool result = Bench(Iterations); + return result; + } + + public static int Main() { + bool result = TestBase(); + return (result ? 100 : -1); + } +} diff --git a/tests/src/JIT/Performance/CodeQuality/BenchI/Array2/Array2.csproj b/tests/src/JIT/Performance/CodeQuality/BenchI/Array2/Array2.csproj new file mode 100644 index 0000000000..0932bfb0ea --- /dev/null +++ b/tests/src/JIT/Performance/CodeQuality/BenchI/Array2/Array2.csproj @@ -0,0 +1,43 @@ + + + + + Debug + AnyCPU + 2.0 + {95DFC527-4DC1-495E-97D7-E94EE1F7140D} + Exe + Properties + 512 + {786C830F-07A1-408B-BD7F-6EE04809D6DB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} + $(ProgramFiles)\Common Files\microsoft shared\VSTT\11.0\UITestExtensionPackages + ..\..\ + 7a9bfb7d + .NETStandard,Version=v1.4 + + + + + + pdbonly + true + + + + False + + + + + + + + + + $(JitPackagesConfigFileDirectory)benchmark\project.json + $(JitPackagesConfigFileDirectory)benchmark\project.lock.json + + + + + diff --git a/tests/src/JIT/Performance/CodeQuality/BenchI/BenchE/BenchE.cs b/tests/src/JIT/Performance/CodeQuality/BenchI/BenchE/BenchE.cs new file mode 100644 index 0000000000..9cf7d770dc --- /dev/null +++ b/tests/src/JIT/Performance/CodeQuality/BenchI/BenchE/BenchE.cs @@ -0,0 +1,117 @@ +// 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 System; +using System.Runtime.CompilerServices; +using Xunit; + +[assembly: OptimizeForBenchmarks] +[assembly: MeasureInstructionsRetired] + +public static class BenchE +{ +#if DEBUG + public const int Iterations = 1; +#else + public const int Iterations = 5000000; +#endif + + private static int s_position; + + private static int Strsch(char[] s, char[] k, int ns, int nk) + { + int i, j; + int start, ksave, cont; + int kend, ssave; + int r; + + start = 0; + ksave = 0; + cont = ns - nk + start; + kend = ksave + nk - 1; + i = 0; + j = 0; + top: + while (s[i] != k[j]) + { + // s is accessed upto cont i.e. ns - nk + 0 + if (i >= cont) + { + r = -1; + goto bottom; + } + i = i + 1; + } + ssave = i; + j = j + 1; + while (j <= kend) + { + i = i + 1; + // j <= kend, so k is accessed upto 0 + nk - 1 + if (s[i] != k[j]) + { + i = ssave + 1; + j = ksave; + goto top; + } + j = j + 1; + } + r = ssave - start + 1; + bottom: + return r; + } + + private static void BenchInner(char[] s, char[] k) + { + int ns, nk; + + ns = 120; + nk = 15; + s_position = Strsch(s, k, ns, nk); + } + + [MethodImpl(MethodImplOptions.NoInlining)] + private static bool Bench() + { + char[] s = { + '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', 'H', 'E', 'R', 'E', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', + 'H', 'E', 'R', 'E', ' ', 'I', 'S', ' ', 'A', ' ', 'M', 'A', 'T', 'C', 'H', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0' + }; + + char[] k = { 'H', 'E', 'R', 'E', ' ', 'I', 'S', ' ', 'A', ' ', 'M', 'A', 'T', 'C', 'H', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' }; + + for (int i = 0; i < Iterations; i++) + { + BenchInner(s, k); + } + + return (s_position == 91); + } + + [Benchmark] + public static void Test() + { + foreach (var iteration in Benchmark.Iterations) + { + using (iteration.StartMeasurement()) + { + Bench(); + } + } + } + + private static bool TestBase() + { + bool result = Bench(); + return result; + } + + public static int Main() + { + bool result = TestBase(); + return (result ? 100 : -1); + } +} diff --git a/tests/src/JIT/Performance/CodeQuality/BenchI/BenchE/BenchE.csproj b/tests/src/JIT/Performance/CodeQuality/BenchI/BenchE/BenchE.csproj new file mode 100644 index 0000000000..0def91ffa2 --- /dev/null +++ b/tests/src/JIT/Performance/CodeQuality/BenchI/BenchE/BenchE.csproj @@ -0,0 +1,43 @@ + + + + + Debug + AnyCPU + 2.0 + {95DFC527-4DC1-495E-97D7-E94EE1F7140D} + Exe + Properties + 512 + {786C830F-07A1-408B-BD7F-6EE04809D6DB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} + $(ProgramFiles)\Common Files\microsoft shared\VSTT\11.0\UITestExtensionPackages + ..\..\ + 7a9bfb7d + .NETStandard,Version=v1.4 + + + + + + pdbonly + true + + + + False + + + + + + + + + + $(JitPackagesConfigFileDirectory)benchmark\project.json + $(JitPackagesConfigFileDirectory)benchmark\project.lock.json + + + + + diff --git a/tests/src/JIT/Performance/CodeQuality/BenchI/BubbleSort/BubbleSort.cs b/tests/src/JIT/Performance/CodeQuality/BenchI/BubbleSort/BubbleSort.cs new file mode 100644 index 0000000000..78626fffc9 --- /dev/null +++ b/tests/src/JIT/Performance/CodeQuality/BenchI/BubbleSort/BubbleSort.cs @@ -0,0 +1,87 @@ +// 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 System; +using System.Runtime.CompilerServices; +using Xunit; + +[assembly: OptimizeForBenchmarks] +[assembly: MeasureInstructionsRetired] + +public static class BubbleSort +{ + +#if DEBUG + public const int Iterations = 1; +#else + public const int Iterations = 55000; +#endif + + static void SortArray(int[] tab, int last) { + bool swap; + int temp; + do { + swap = false; + for (int i = 0; i < last; i++) { + if (tab[i] > tab[i + 1]) { + temp = tab[i]; + tab[i] = tab[i + 1]; + tab[i + 1] = temp; + swap = true; + } + } + } + while (swap); + } + + static bool VerifySort(int[] tab, int last) { + for (int i = 0; i < last; i++) { + if (tab[i] > tab[i + 1]) { + return false; + } + } + + return true; + } + + [MethodImpl(MethodImplOptions.NoInlining)] + static public bool Bench() { + int[] tab = new int[100]; + int k = 0; + for (int i = 9; i >= 0; i--) { + for (int j = i * 10; j < (i + 1) * 10; j++) { + tab[k++] = ((j & 1) == 1) ? j + 1 : j - 1; + } + } + SortArray(tab, 99); + bool result = VerifySort(tab, 99); + return result; + } + + [Benchmark] + public static void Test() { + foreach (var iteration in Benchmark.Iterations) { + using (iteration.StartMeasurement()) { + for (int i = 0; i < Iterations; i++) { + Bench(); + } + } + } + } + + static bool TestBase() { + bool result = true; + for (int i = 0; i < Iterations; i++) { + result &= Bench(); + } + return result; + } + + public static int Main() { + bool result = TestBase(); + return (result ? 100 : -1); + } +} diff --git a/tests/src/JIT/Performance/CodeQuality/BenchI/BubbleSort/BubbleSort.csproj b/tests/src/JIT/Performance/CodeQuality/BenchI/BubbleSort/BubbleSort.csproj new file mode 100644 index 0000000000..6fabe89c8e --- /dev/null +++ b/tests/src/JIT/Performance/CodeQuality/BenchI/BubbleSort/BubbleSort.csproj @@ -0,0 +1,43 @@ + + + + + Debug + AnyCPU + 2.0 + {95DFC527-4DC1-495E-97D7-E94EE1F7140D} + Exe + Properties + 512 + {786C830F-07A1-408B-BD7F-6EE04809D6DB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} + $(ProgramFiles)\Common Files\microsoft shared\VSTT\11.0\UITestExtensionPackages + ..\..\ + 7a9bfb7d + .NETStandard,Version=v1.4 + + + + + + pdbonly + true + + + + False + + + + + + + + + + $(JitPackagesConfigFileDirectory)benchmark\project.json + $(JitPackagesConfigFileDirectory)benchmark\project.lock.json + + + + + diff --git a/tests/src/JIT/Performance/CodeQuality/BenchI/BubbleSort2/BubbleSort2.cs b/tests/src/JIT/Performance/CodeQuality/BenchI/BubbleSort2/BubbleSort2.cs new file mode 100644 index 0000000000..55e5a5f8c5 --- /dev/null +++ b/tests/src/JIT/Performance/CodeQuality/BenchI/BubbleSort2/BubbleSort2.cs @@ -0,0 +1,90 @@ +// 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 System; +using System.Runtime.CompilerServices; +using Xunit; + +[assembly: OptimizeForBenchmarks] +[assembly: MeasureInstructionsRetired] + +public static class BubbleSort2 +{ + +#if DEBUG + public const int Iterations = 1; + public const int Bound = 5 * Iterations; +#else + public const int Iterations = 15; + public const int Bound = 500 * Iterations; +#endif + + static void Inner(int[] x) { + int limit1 = Bound - 1; + for (int i = 1; i <= limit1; i++) { + for (int j = i; j <= Bound; j++) { + if (x[i] > x[j]) { + int temp = x[j]; + x[j] = x[i]; + x[i] = temp; + } + } + } + } + + [MethodImpl(MethodImplOptions.NoInlining)] + static bool Bench() { + int[] x = new int[Bound + 1]; + int i, j; + int limit; + j = 99999; + limit = Bound - 2; + i = 1; + do { + x[i] = j & 32767; + x[i + 1] = (j + 11111) & 32767; + x[i + 2] = (j + 22222) & 32767; + j = j + 33333; + i = i + 3; + } while (i <= limit); + x[Bound - 1] = j; + x[Bound] = j; + + Inner(x); + + for (i = 0; i < Bound - 1; i++) { + if (x[i] > x[i + 1]) { + return false; + } + } + + return true; + } + + [Benchmark] + public static void Test() { + foreach (var iteration in Benchmark.Iterations) { + using (iteration.StartMeasurement()) { + for (int i = 0; i < Iterations; i++) { + Bench(); + } + } + } + } + + static bool TestBase() { + bool result = true; + for (int i = 0; i < Iterations; i++) { + result &= Bench(); + } + return result; + } + + public static int Main() { + bool result = TestBase(); + return (result ? 100 : -1); + } +} diff --git a/tests/src/JIT/Performance/CodeQuality/BenchI/BubbleSort2/BubbleSort2.csproj b/tests/src/JIT/Performance/CodeQuality/BenchI/BubbleSort2/BubbleSort2.csproj new file mode 100644 index 0000000000..7b5fb93ed9 --- /dev/null +++ b/tests/src/JIT/Performance/CodeQuality/BenchI/BubbleSort2/BubbleSort2.csproj @@ -0,0 +1,43 @@ + + + + + Debug + AnyCPU + 2.0 + {95DFC527-4DC1-495E-97D7-E94EE1F7140D} + Exe + Properties + 512 + {786C830F-07A1-408B-BD7F-6EE04809D6DB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} + $(ProgramFiles)\Common Files\microsoft shared\VSTT\11.0\UITestExtensionPackages + ..\..\ + 7a9bfb7d + .NETStandard,Version=v1.4 + + + + + + pdbonly + true + + + + False + + + + + + + + + + $(JitPackagesConfigFileDirectory)benchmark\project.json + $(JitPackagesConfigFileDirectory)benchmark\project.lock.json + + + + + diff --git a/tests/src/JIT/Performance/CodeQuality/BenchI/CSieve/CSieve.cs b/tests/src/JIT/Performance/CodeQuality/BenchI/CSieve/CSieve.cs new file mode 100644 index 0000000000..606f740fb6 --- /dev/null +++ b/tests/src/JIT/Performance/CodeQuality/BenchI/CSieve/CSieve.cs @@ -0,0 +1,82 @@ +// 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. +// +// Based on Eratosthenes Sieve Prime Number Program in C, Byte Magazine, January 1983. + +using Microsoft.Xunit.Performance; +using System; +using System.Runtime.CompilerServices; +using Xunit; + +[assembly: OptimizeForBenchmarks] +[assembly: MeasureInstructionsRetired] + +public static class CSieve +{ + +#if DEBUG + public const int Iterations = 1; +#else + public const int Iterations = 200; +#endif + + const int Size = 8190; + + [MethodImpl(MethodImplOptions.NoInlining)] + static bool Bench() { + bool[] flags = new bool[Size + 1]; + int count = 0; + for (int iter = 1; iter <= Iterations; iter++) + { + count = 0; + + // Initially, assume all are prime + for (int i = 0; i <= Size; i++) + { + flags[i] = true; + } + + // Refine + for (int i = 2; i <= Size; i++) + { + if (flags[i]) + { + // Found a prime + for (int k = i + i; k <= Size; k += i) + { + // Cancel its multiples + flags[k] = false; + } + count++; + } + } + } + + return (count == 1027); + } + + [Benchmark] + public static void Test() { + foreach (var iteration in Benchmark.Iterations) { + using (iteration.StartMeasurement()) { + for (int i = 0; i < Iterations; i++) { + Bench(); + } + } + } + } + + static bool TestBase() { + bool result = true; + for (int i = 0; i < Iterations; i++) { + result &= Bench(); + } + return result; + } + + public static int Main() { + bool result = TestBase(); + return (result ? 100 : -1); + } +} diff --git a/tests/src/JIT/Performance/CodeQuality/BenchI/CSieve/CSieve.csproj b/tests/src/JIT/Performance/CodeQuality/BenchI/CSieve/CSieve.csproj new file mode 100644 index 0000000000..599dcd7b74 --- /dev/null +++ b/tests/src/JIT/Performance/CodeQuality/BenchI/CSieve/CSieve.csproj @@ -0,0 +1,43 @@ + + + + + Debug + AnyCPU + 2.0 + {95DFC527-4DC1-495E-97D7-E94EE1F7140D} + Exe + Properties + 512 + {786C830F-07A1-408B-BD7F-6EE04809D6DB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} + $(ProgramFiles)\Common Files\microsoft shared\VSTT\11.0\UITestExtensionPackages + ..\..\ + 7a9bfb7d + .NETStandard,Version=v1.4 + + + + + + pdbonly + true + + + + False + + + + + + + + + + $(JitPackagesConfigFileDirectory)benchmark\project.json + $(JitPackagesConfigFileDirectory)benchmark\project.lock.json + + + + + diff --git a/tests/src/JIT/Performance/CodeQuality/BenchI/Fib/Fib.cs b/tests/src/JIT/Performance/CodeQuality/BenchI/Fib/Fib.cs new file mode 100644 index 0000000000..01064bb168 --- /dev/null +++ b/tests/src/JIT/Performance/CodeQuality/BenchI/Fib/Fib.cs @@ -0,0 +1,63 @@ +// 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 System; +using System.Runtime.CompilerServices; +using Xunit; + +[assembly: OptimizeForBenchmarks] +[assembly: MeasureInstructionsRetired] + +public static class Fib +{ + +#if DEBUG + public const int Iterations = 1; +#else + public const int Iterations = 3500; +#endif + + const int Number = 24; + + static int Fibonacci(int x) { + if (x > 2) { + return (Fibonacci(x - 1) + Fibonacci(x - 2)); + } + else { + return 1; + } + } + + [MethodImpl(MethodImplOptions.NoInlining)] + static bool Bench() { + int fib = Fibonacci(Number); + return (fib == 46368); + } + + [Benchmark] + public static void Test() { + foreach (var iteration in Benchmark.Iterations) { + using (iteration.StartMeasurement()) { + for (int i = 0; i < Iterations; i++) { + Bench(); + } + } + } + } + + static bool TestBase() { + bool result = true; + for (int i = 0; i < Iterations; i++) { + result &= Bench(); + } + return result; + } + + public static int Main() { + bool result = TestBase(); + return (result ? 100 : -1); + } +} diff --git a/tests/src/JIT/Performance/CodeQuality/BenchI/Fib/Fib.csproj b/tests/src/JIT/Performance/CodeQuality/BenchI/Fib/Fib.csproj new file mode 100644 index 0000000000..2b8c995bd8 --- /dev/null +++ b/tests/src/JIT/Performance/CodeQuality/BenchI/Fib/Fib.csproj @@ -0,0 +1,43 @@ + + + + + Debug + AnyCPU + 2.0 + {95DFC527-4DC1-495E-97D7-E94EE1F7140D} + Exe + Properties + 512 + {786C830F-07A1-408B-BD7F-6EE04809D6DB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} + $(ProgramFiles)\Common Files\microsoft shared\VSTT\11.0\UITestExtensionPackages + ..\..\ + 7a9bfb7d + .NETStandard,Version=v1.4 + + + + + + pdbonly + true + + + + False + + + + + + + + + + $(JitPackagesConfigFileDirectory)benchmark\project.json + $(JitPackagesConfigFileDirectory)benchmark\project.lock.json + + + + + diff --git a/tests/src/JIT/Performance/CodeQuality/BenchI/HeapSort/HeapSort.cs b/tests/src/JIT/Performance/CodeQuality/BenchI/HeapSort/HeapSort.cs new file mode 100644 index 0000000000..9fd7aa32e0 --- /dev/null +++ b/tests/src/JIT/Performance/CodeQuality/BenchI/HeapSort/HeapSort.cs @@ -0,0 +1,121 @@ +// 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 System; +using System.Runtime.CompilerServices; +using Xunit; + +[assembly: OptimizeForBenchmarks] +[assembly: MeasureInstructionsRetired] + +public static class HeapSort +{ + +#if DEBUG + public const int Iterations = 1; +#else + public const int Iterations = 2500; +#endif + + const int ArraySize = 5500; + + static void Inner(int[] x, int n) { + int i, j, k, m; + + // pass1 -- put vector in heap form + // that is to say, guarantee that x(i)>=x(2*i) and x(i)>=x(2*i+1). + // after pass 1, the largest item will be at x(1). + for (i = 2; i <= n; i++) { + j = i; + k = j / 2; + m = x[i]; + + // 0 < k <= (n / 2) + // 1 <= j <= n + while (k > 0) { + if (m <= x[k]) { + break; + } + x[j] = x[k]; + j = k; + k = k / 2; + } + x[j] = m; + } + + // pass 2 -- swap first and last items. now with the last + // item correctly placed, consider the list shorter by one item. + // restore the shortened list to heap sort, and repeat + // process until list is only two items long. + i = n; + do { + // do i = n to 2 by -1; + m = x[i]; + x[i] = x[1]; // last item, i.e. item(i) now correct. + j = 1; // we now find the appropriate resting point for m + k = 2; + + // 2 <= k < i ==> 2 <= k < n + // 1 <= j < n + while (k < i) { + if ((k + 1) < i) { + if (x[k + 1] > x[k]) { + k = k + 1; + } + } + if (x[k] <= m) { + break; + } + + x[j] = x[k]; + j = k; + k = k + k; + } + + x[j] = m; + i = i - 1; + } while (i >= 2); + } + + [MethodImpl(MethodImplOptions.NoInlining)] + static bool Bench() { + int[] x = new int[ArraySize + 1]; + for (int i = 1; i <= ArraySize; i++) { + x[i] = ArraySize - i + 1; + } + Inner(x, ArraySize); + for (int j = 1; j <= ArraySize; j++) { + if (x[j] != j) { + return false; + } + } + return true; + } + + [Benchmark] + public static void Test() { + foreach (var iteration in Benchmark.Iterations) { + using (iteration.StartMeasurement()) { + for (int i = 0; i < Iterations; i++) { + Bench(); + } + } + } + } + + static bool TestBase() { + bool result = true; + for (int i = 0; i < Iterations; i++) { + result &= Bench(); + } + return result; + } + + public static int Main() { + bool result = TestBase(); + return (result ? 100 : -1); + } +} diff --git a/tests/src/JIT/Performance/CodeQuality/BenchI/HeapSort/HeapSort.csproj b/tests/src/JIT/Performance/CodeQuality/BenchI/HeapSort/HeapSort.csproj new file mode 100644 index 0000000000..2afbe54997 --- /dev/null +++ b/tests/src/JIT/Performance/CodeQuality/BenchI/HeapSort/HeapSort.csproj @@ -0,0 +1,43 @@ + + + + + Debug + AnyCPU + 2.0 + {95DFC527-4DC1-495E-97D7-E94EE1F7140D} + Exe + Properties + 512 + {786C830F-07A1-408B-BD7F-6EE04809D6DB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} + $(ProgramFiles)\Common Files\microsoft shared\VSTT\11.0\UITestExtensionPackages + ..\..\ + 7a9bfb7d + .NETStandard,Version=v1.4 + + + + + + pdbonly + true + + + + False + + + + + + + + + + $(JitPackagesConfigFileDirectory)benchmark\project.json + $(JitPackagesConfigFileDirectory)benchmark\project.lock.json + + + + + diff --git a/tests/src/JIT/Performance/CodeQuality/BenchI/IniArray/IniArray.cs b/tests/src/JIT/Performance/CodeQuality/BenchI/IniArray/IniArray.cs new file mode 100644 index 0000000000..78fa233dbf --- /dev/null +++ b/tests/src/JIT/Performance/CodeQuality/BenchI/IniArray/IniArray.cs @@ -0,0 +1,60 @@ +// 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 System; +using System.Runtime.CompilerServices; +using Xunit; + +[assembly: OptimizeForBenchmarks] +[assembly: MeasureInstructionsRetired] + +public static class IniArray +{ + +#if DEBUG + public const int Iterations = 1; +#else + public const int Iterations = 10000000; +#endif + + const int Allotted = 16; + static volatile object VolatileObject; + + static void Escape(object obj) { + VolatileObject = obj; + } + + [MethodImpl(MethodImplOptions.NoInlining)] + static bool Bench() { + char[] workarea = new char[Allotted]; + for (int i = 0; i < Iterations; i++) { + for (int j = 0; j < Allotted; j++) { + workarea[j] = ' '; + } + } + Escape(workarea); + return true; + } + + [Benchmark] + public static void Test() { + foreach (var iteration in Benchmark.Iterations) { + using (iteration.StartMeasurement()) { + Bench(); + } + } + } + + static bool TestBase() { + bool result = Bench(); + return result; + } + + public static int Main() { + bool result = TestBase(); + return (result ? 100 : -1); + } +} diff --git a/tests/src/JIT/Performance/CodeQuality/BenchI/IniArray/IniArray.csproj b/tests/src/JIT/Performance/CodeQuality/BenchI/IniArray/IniArray.csproj new file mode 100644 index 0000000000..2fab51071a --- /dev/null +++ b/tests/src/JIT/Performance/CodeQuality/BenchI/IniArray/IniArray.csproj @@ -0,0 +1,43 @@ + + + + + Debug + AnyCPU + 2.0 + {95DFC527-4DC1-495E-97D7-E94EE1F7140D} + Exe + Properties + 512 + {786C830F-07A1-408B-BD7F-6EE04809D6DB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} + $(ProgramFiles)\Common Files\microsoft shared\VSTT\11.0\UITestExtensionPackages + ..\..\ + 7a9bfb7d + .NETStandard,Version=v1.4 + + + + + + pdbonly + true + + + + False + + + + + + + + + + $(JitPackagesConfigFileDirectory)benchmark\project.json + $(JitPackagesConfigFileDirectory)benchmark\project.lock.json + + + + + diff --git a/tests/src/JIT/Performance/CodeQuality/BenchI/LogicArray/LogicArray.cs b/tests/src/JIT/Performance/CodeQuality/BenchI/LogicArray/LogicArray.cs new file mode 100644 index 0000000000..f5ff746b86 --- /dev/null +++ b/tests/src/JIT/Performance/CodeQuality/BenchI/LogicArray/LogicArray.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 System; +using System.Runtime.CompilerServices; +using Xunit; + +[assembly: OptimizeForBenchmarks] +[assembly: MeasureInstructionsRetired] + +public static class LogicArray +{ + +#if DEBUG + public const int Iterations = 1; +#else + public const int Iterations = 3000; +#endif + + const int ArraySize = 50; + + struct Workarea + { + public int X; + public int[][] A; + } + + static T[][] AllocArray(int n1, int n2) { + T[][] a = new T[n1][]; + for (int i = 0; i < n1; ++i) { + a[i] = new T[n2]; + } + return a; + } + + static bool Inner(ref Workarea cmn) { + int i, j, k; + cmn.X = 0; + for (i = 1; i <= 50; i++) { + for (j = 1; j <= 50; j++) { + cmn.A[i][j] = 1; + } + } + for (k = 1; k <= 50; k++) { + for (j = 1; j <= 50; j++) { + i = 1; + do { + cmn.X = cmn.X | cmn.A[i][j] & cmn.A[i + 1][k]; + i = i + 2; + } while (i <= 50); + } + } + if (cmn.X != 1) { + return false; + } + else { + return true; + } + } + + [MethodImpl(MethodImplOptions.NoInlining)] + static bool Bench() { + Workarea cmn = new Workarea(); + cmn.X = 0; + cmn.A = AllocArray(51, 51); + for (int n = 1; n <= Iterations; n++) { + bool result = Inner(ref cmn); + if (!result) { + return false; + } + } + + return true; + } + + [Benchmark] + public static void Test() { + foreach (var iteration in Benchmark.Iterations) { + using (iteration.StartMeasurement()) { + Bench(); + } + } + } + + static bool TestBase() { + bool result = Bench(); + return result; + } + + public static int Main() { + bool result = TestBase(); + return (result ? 100 : -1); + } +} diff --git a/tests/src/JIT/Performance/CodeQuality/BenchI/LogicArray/LogicArray.csproj b/tests/src/JIT/Performance/CodeQuality/BenchI/LogicArray/LogicArray.csproj new file mode 100644 index 0000000000..0281fb4ca7 --- /dev/null +++ b/tests/src/JIT/Performance/CodeQuality/BenchI/LogicArray/LogicArray.csproj @@ -0,0 +1,43 @@ + + + + + Debug + AnyCPU + 2.0 + {95DFC527-4DC1-495E-97D7-E94EE1F7140D} + Exe + Properties + 512 + {786C830F-07A1-408B-BD7F-6EE04809D6DB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} + $(ProgramFiles)\Common Files\microsoft shared\VSTT\11.0\UITestExtensionPackages + ..\..\ + 7a9bfb7d + .NETStandard,Version=v1.4 + + + + + + pdbonly + true + + + + False + + + + + + + + + + $(JitPackagesConfigFileDirectory)benchmark\project.json + $(JitPackagesConfigFileDirectory)benchmark\project.lock.json + + + + + diff --git a/tests/src/JIT/Performance/CodeQuality/BenchI/Midpoint/Midpoint.cs b/tests/src/JIT/Performance/CodeQuality/BenchI/Midpoint/Midpoint.cs new file mode 100644 index 0000000000..6e4f193746 --- /dev/null +++ b/tests/src/JIT/Performance/CodeQuality/BenchI/Midpoint/Midpoint.cs @@ -0,0 +1,104 @@ +// 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 System; +using System.Runtime.CompilerServices; +using Xunit; + +[assembly: OptimizeForBenchmarks] +[assembly: MeasureInstructionsRetired] + +public static class Midpoint +{ + +#if DEBUG + public const int Iterations = 1; +#else + public const int Iterations = 70000; +#endif + + static T[][] AllocArray(int n1, int n2) { + T[][] a = new T[n1][]; + for (int i = 0; i < n1; ++i) { + a[i] = new T[n2]; + } + return a; + } + + static int Inner(ref int x, ref int y, ref int z) { + int mid; + + if (x < y) { + if (y < z) { + mid = y; + } + else { + if (x < z) { + mid = z; + } + else { + mid = x; + } + } + } + else { + if (x < z) { + mid = x; + } + else { + if (y < z) { + mid = z; + } + else { + mid = y; + } + } + } + + return (mid); + } + + [MethodImpl(MethodImplOptions.NoInlining)] + static bool Bench() { + int[][] a = AllocArray(2001, 4); + int[] mid = new int[2001]; + int j = 99999; + + for (int i = 1; i <= 2000; i++) { + a[i][1] = j & 32767; + a[i][2] = (j + 11111) & 32767; + a[i][3] = (j + 22222) & 32767; + j = j + 33333; + } + + for (int k = 1; k <= Iterations; k++) { + for (int l = 1; l <= 2000; l++) { + mid[l] = Inner(ref a[l][1], ref a[l][2], ref a[l][3]); + } + } + + return (mid[2000] == 17018); + } + + [Benchmark] + public static void Test() { + foreach (var iteration in Benchmark.Iterations) { + using (iteration.StartMeasurement()) { + Bench(); + } + } + } + + static bool TestBase() { + bool result = Bench(); + return result; + } + + public static int Main() { + bool result = TestBase(); + return (result ? 100 : -1); + } +} diff --git a/tests/src/JIT/Performance/CodeQuality/BenchI/Midpoint/Midpoint.csproj b/tests/src/JIT/Performance/CodeQuality/BenchI/Midpoint/Midpoint.csproj new file mode 100644 index 0000000000..e7e379be83 --- /dev/null +++ b/tests/src/JIT/Performance/CodeQuality/BenchI/Midpoint/Midpoint.csproj @@ -0,0 +1,43 @@ + + + + + Debug + AnyCPU + 2.0 + {95DFC527-4DC1-495E-97D7-E94EE1F7140D} + Exe + Properties + 512 + {786C830F-07A1-408B-BD7F-6EE04809D6DB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} + $(ProgramFiles)\Common Files\microsoft shared\VSTT\11.0\UITestExtensionPackages + ..\..\ + 7a9bfb7d + .NETStandard,Version=v1.4 + + + + + + pdbonly + true + + + + False + + + + + + + + + + $(JitPackagesConfigFileDirectory)benchmark\project.json + $(JitPackagesConfigFileDirectory)benchmark\project.lock.json + + + + + diff --git a/tests/src/JIT/Performance/CodeQuality/BenchI/MulMatrix/MulMatrix.cs b/tests/src/JIT/Performance/CodeQuality/BenchI/MulMatrix/MulMatrix.cs new file mode 100644 index 0000000000..3f9c224d3d --- /dev/null +++ b/tests/src/JIT/Performance/CodeQuality/BenchI/MulMatrix/MulMatrix.cs @@ -0,0 +1,140 @@ +// 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 System; +using System.Runtime.CompilerServices; +using Xunit; + +[assembly: OptimizeForBenchmarks] +[assembly: MeasureInstructionsRetired] + +public static class MulMatrix +{ + +#if DEBUG + public const int Iterations = 1; +#else + public const int Iterations = 100; +#endif + + const int Size = 75; + static volatile object VolatileObject; + + static void Escape(object obj) { + VolatileObject = obj; + } + + static T[][] AllocArray(int n1, int n2) { + T[][] a = new T[n1][]; + for (int i = 0; i < n1; ++i) { + a[i] = new T[n2]; + } + return a; + } + + static void Inner(int[][] a, int[][] b, int[][] c) { + + int i, j, k, l; + + // setup + for (j = 0; j < Size; j++) { + for (i = 0; i < Size; i++) { + a[i][j] = i; + b[i][j] = 2 * j; + c[i][j] = a[i][j] + b[i][j]; + } + } + + // jkl + for (j = 0; j < Size; j++) { + for (k = 0; k < Size; k++) { + for (l = 0; l < Size; l++) { + c[j][k] += a[j][l] * b[l][k]; + } + } + } + + // jlk + for (j = 0; j < Size; j++) { + for (l = 0; l < Size; l++) { + for (k = 0; k < Size; k++) { + c[j][k] += a[j][l] * b[l][k]; + } + } + } + + // kjl + for (k = 0; k < Size; k++) { + for (j = 0; j < Size; j++) { + for (l = 0; l < Size; l++) { + c[j][k] += a[j][l] * b[l][k]; + } + } + } + + // klj + for (k = 0; k < Size; k++) { + for (l = 0; l < Size; l++) { + for (j = 0; j < Size; j++) { + c[j][k] += a[j][l] * b[l][k]; + } + } + } + + // ljk + for (l = 0; l < Size; l++) { + for (j = 0; j < Size; j++) { + for (k = 0; k < Size; k++) { + c[j][k] += a[j][l] * b[l][k]; + } + } + } + + // lkj + for (l = 0; l < Size; l++) { + for (k = 0; k < Size; k++) { + for (j = 0; j < Size; j++) { + c[j][k] += a[j][l] * b[l][k]; + } + } + } + + return; + } + + [MethodImpl(MethodImplOptions.NoInlining)] + static bool Bench() { + int[][] a = AllocArray(Size, Size); + int[][] b = AllocArray(Size, Size); + int[][] c = AllocArray(Size, Size); + + for (int i = 0; i < Iterations; ++i) { + Inner(a, b, c); + } + + Escape(c); + return true; + } + + [Benchmark] + public static void Test() { + foreach (var iteration in Benchmark.Iterations) { + using (iteration.StartMeasurement()) { + Bench(); + } + } + } + + static bool TestBase() { + bool result = Bench(); + return result; + } + + public static int Main() { + bool result = TestBase(); + return (result ? 100 : -1); + } +} diff --git a/tests/src/JIT/Performance/CodeQuality/BenchI/MulMatrix/MulMatrix.csproj b/tests/src/JIT/Performance/CodeQuality/BenchI/MulMatrix/MulMatrix.csproj new file mode 100644 index 0000000000..5046b02d9e --- /dev/null +++ b/tests/src/JIT/Performance/CodeQuality/BenchI/MulMatrix/MulMatrix.csproj @@ -0,0 +1,43 @@ + + + + + Debug + AnyCPU + 2.0 + {95DFC527-4DC1-495E-97D7-E94EE1F7140D} + Exe + Properties + 512 + {786C830F-07A1-408B-BD7F-6EE04809D6DB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} + $(ProgramFiles)\Common Files\microsoft shared\VSTT\11.0\UITestExtensionPackages + ..\..\ + 7a9bfb7d + .NETStandard,Version=v1.4 + + + + + + pdbonly + true + + + + False + + + + + + + + + + $(JitPackagesConfigFileDirectory)benchmark\project.json + $(JitPackagesConfigFileDirectory)benchmark\project.lock.json + + + + + diff --git a/tests/src/JIT/Performance/CodeQuality/BenchI/NDhrystone/NDhrystone.cs b/tests/src/JIT/Performance/CodeQuality/BenchI/NDhrystone/NDhrystone.cs new file mode 100644 index 0000000000..c2526748ce --- /dev/null +++ b/tests/src/JIT/Performance/CodeQuality/BenchI/NDhrystone/NDhrystone.cs @@ -0,0 +1,286 @@ +// 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. +// +// Adapted from +// +// Dhrystone: a synthetic systems programming benchmark +// Reinhold P. Weicker +// Communications of the ACM, Volume 27 Issue 10, Oct 1984, Pages 1013-1030 + +using Microsoft.Xunit.Performance; +using System; +using System.Runtime.CompilerServices; +using Xunit; + +[assembly: OptimizeForBenchmarks] +[assembly: MeasureInstructionsRetired] + +public static class NDhrystone +{ + +#if DEBUG + public const int Iterations = 1; +#else + public const int Iterations = 7000000; +#endif + + static T[][] AllocArray(int n1, int n2) { + T[][] a = new T[n1][]; + for (int i = 0; i < n1; ++i) { + a[i] = new T[n2]; + } + return a; + } + + enum Enumeration + { + Ident1 = 1, Ident2, Ident3, Ident4, Ident5 + } + + sealed class Record + { + public Record PtrComp; + public Enumeration Discr; + public Enumeration EnumComp; + public int IntComp; + public char[] StringComp; + } + + static int s_intGlob; + static bool s_boolGlob; + static char s_char1Glob; + static char s_char2Glob; + static int[] m_array1Glob = new int[51]; + static int[][] m_array2Glob; + static Record m_ptrGlb = new Record(); + static Record m_ptrGlbNext = new Record(); + static char[] m_string1Loc; + static char[] m_string2Loc; + + static void Proc0() { + int intLoc1; + int intLoc2; + int intLoc3 = 0; + Enumeration enumLoc; + + int i; /* modification */ + + m_ptrGlb.PtrComp = m_ptrGlbNext; + m_ptrGlb.Discr = Enumeration.Ident1; + m_ptrGlb.EnumComp = Enumeration.Ident3; + m_ptrGlb.IntComp = 40; + m_ptrGlb.StringComp = "DHRYSTONE PROGRAM, SOME STRING".ToCharArray(); + m_string1Loc = "DHRYSTONE PROGRAM, 1'ST STRING".ToCharArray(); + m_array2Glob[8][7] = 10; /* Was missing in published program */ + + for (i = 0; i < Iterations; ++i) { + Proc5(); + Proc4(); + intLoc1 = 2; + intLoc2 = 3; + m_string2Loc = "DHRYSTONE PROGRAM, 2'ND STRING".ToCharArray(); + enumLoc = Enumeration.Ident2; + s_boolGlob = !Func2(m_string1Loc, m_string2Loc); + while (intLoc1 < intLoc2) { + intLoc3 = 5 * intLoc1 - intLoc2; + Proc7(intLoc1, intLoc2, ref intLoc3); + ++intLoc1; + } + Proc8(m_array1Glob, m_array2Glob, intLoc1, intLoc3); + Proc1(ref m_ptrGlb); + for (char charIndex = 'A'; charIndex <= s_char2Glob; ++charIndex) { + if (enumLoc == Func1(charIndex, 'C')) { + Proc6(Enumeration.Ident1, ref enumLoc); + } + } + intLoc3 = intLoc2 * intLoc1; + intLoc2 = intLoc3 / intLoc1; + intLoc2 = 7 * (intLoc3 - intLoc2) - intLoc1; + Proc2(ref intLoc1); + } + } + + static void Proc1(ref Record ptrParIn) { + ptrParIn.PtrComp = m_ptrGlb; + ptrParIn.IntComp = 5; + ptrParIn.PtrComp.IntComp = ptrParIn.IntComp; + ptrParIn.PtrComp.PtrComp = ptrParIn.PtrComp; + Proc3(ref ptrParIn.PtrComp.PtrComp); + if (ptrParIn.PtrComp.Discr == Enumeration.Ident1) { + ptrParIn.PtrComp.IntComp = 6; + Proc6(ptrParIn.EnumComp, ref ptrParIn.PtrComp.EnumComp); + ptrParIn.PtrComp.PtrComp = m_ptrGlb.PtrComp; + Proc7(ptrParIn.PtrComp.IntComp, 10, ref ptrParIn.PtrComp.IntComp); + } + else { + ptrParIn = ptrParIn.PtrComp; + } + } + + static void Proc2(ref int intParIO) { + int intLoc; + Enumeration enumLoc = Enumeration.Ident2; + intLoc = intParIO + 10; + + for (;;) { + if (s_char1Glob == 'A') { + --intLoc; + intParIO = intLoc - s_intGlob; + enumLoc = Enumeration.Ident1; + } + if (enumLoc == Enumeration.Ident1) { + break; + } + } + } + + static void Proc3(ref Record ptrParOut) { + if (m_ptrGlb != null) { + ptrParOut = m_ptrGlb.PtrComp; + } + else { + s_intGlob = 100; + } + + Proc7(10, s_intGlob, ref m_ptrGlb.IntComp); + } + + static void Proc4() { + bool boolLoc; + boolLoc = s_char1Glob == 'A'; + boolLoc |= s_boolGlob; + s_char2Glob = 'B'; + } + + static void Proc5() { + s_char1Glob = 'A'; + s_boolGlob = false; + } + + static void Proc6(Enumeration enumParIn, ref Enumeration enumParOut) { + enumParOut = enumParIn; + if (!Func3(enumParIn)) { + enumParOut = Enumeration.Ident4; + } + + switch (enumParIn) { + case Enumeration.Ident1: + enumParOut = Enumeration.Ident1; + break; + case Enumeration.Ident2: + if (s_intGlob > 100) { + enumParOut = Enumeration.Ident1; + } + else { + enumParOut = Enumeration.Ident4; + } + break; + case Enumeration.Ident3: + enumParOut = Enumeration.Ident2; + break; + case Enumeration.Ident4: + break; + case Enumeration.Ident5: + enumParOut = Enumeration.Ident3; + break; + } + } + + static void Proc7(int intParI1, int intParI2, ref int intParOut) { + int intLoc; + intLoc = intParI1 + 2; + intParOut = intParI2 + intLoc; + } + + static void Proc8(int[] array1Par, int[][] array2Par, int intParI1, int intParI2) { + int intLoc; + intLoc = intParI1 + 5; + array1Par[intLoc] = intParI2; + array1Par[intLoc + 1] = array1Par[intLoc]; + array1Par[intLoc + 30] = intLoc; + for (int intIndex = intLoc; intIndex <= (intLoc + 1); ++intIndex) { + array2Par[intLoc][intIndex] = intLoc; + } + ++array2Par[intLoc][intLoc - 1]; + array2Par[intLoc + 20][intLoc] = array1Par[intLoc]; + s_intGlob = 5; + } + + static Enumeration Func1(char charPar1, char charPar2) { + char charLoc1; + char charLoc2; + charLoc1 = charPar1; + charLoc2 = charLoc1; + if (charLoc2 != charPar2) { + return (Enumeration.Ident1); + } + else { + return (Enumeration.Ident2); + } + } + + static bool Func2(char[] strParI1, char[] strParI2) { + int intLoc; + char charLoc = '\0'; + intLoc = 1; + while (intLoc <= 1) { + if (Func1(strParI1[intLoc], strParI2[intLoc + 1]) == Enumeration.Ident1) { + charLoc = 'A'; + ++intLoc; + } + } + if (charLoc >= 'W' && charLoc <= 'Z') { + intLoc = 7; + } + if (charLoc == 'X') { + return true; + } + else { + for (int i = 0; i < 30; i++) { + if (strParI1[i] > strParI2[i]) { + intLoc += 7; + return true; + } + } + + return false; + } + } + + static bool Func3(Enumeration enumParIn) { + Enumeration enumLoc; + enumLoc = enumParIn; + if (enumLoc == Enumeration.Ident3) { + return true; + } + + return false; + } + + [MethodImpl(MethodImplOptions.NoInlining)] + static bool Bench() { + m_array2Glob = AllocArray(51, 51); + Proc0(); + return true; + } + + [Benchmark] + public static void Test() { + foreach (var iteration in Benchmark.Iterations) { + using (iteration.StartMeasurement()) { + Bench(); + } + } + } + + static bool TestBase() { + bool result = Bench(); + return result; + } + + public static int Main() { + bool result = TestBase(); + return (result ? 100 : -1); + } +} diff --git a/tests/src/JIT/Performance/CodeQuality/BenchI/NDhrystone/NDhrystone.csproj b/tests/src/JIT/Performance/CodeQuality/BenchI/NDhrystone/NDhrystone.csproj new file mode 100644 index 0000000000..ea175a5d04 --- /dev/null +++ b/tests/src/JIT/Performance/CodeQuality/BenchI/NDhrystone/NDhrystone.csproj @@ -0,0 +1,43 @@ + + + + + Debug + AnyCPU + 2.0 + {95DFC527-4DC1-495E-97D7-E94EE1F7140D} + Exe + Properties + 512 + {786C830F-07A1-408B-BD7F-6EE04809D6DB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} + $(ProgramFiles)\Common Files\microsoft shared\VSTT\11.0\UITestExtensionPackages + ..\..\ + 7a9bfb7d + .NETStandard,Version=v1.4 + + + + + + pdbonly + true + + + + False + + + + + + + + + + $(JitPackagesConfigFileDirectory)benchmark\project.json + $(JitPackagesConfigFileDirectory)benchmark\project.lock.json + + + + + diff --git a/tests/src/JIT/Performance/CodeQuality/BenchI/Permutate/Permutate.cs b/tests/src/JIT/Performance/CodeQuality/BenchI/Permutate/Permutate.cs new file mode 100644 index 0000000000..d7457cdbc1 --- /dev/null +++ b/tests/src/JIT/Performance/CodeQuality/BenchI/Permutate/Permutate.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 System; +using System.Runtime.CompilerServices; +using Xunit; + +[assembly: OptimizeForBenchmarks] +[assembly: MeasureInstructionsRetired] + +public class Permutate +{ +#if DEBUG + public const int Iterations = 1; +#else + public const int Iterations = 20000; +#endif + + private int[] _permArray = new int[11]; + private static int s_pctr; + + private static + void Swap(int[] arr, int i, int j) + { + int t = arr[i]; + arr[i] = arr[j]; + arr[j] = t; + } + + private void Initialize() + { + for (int i = 1; i <= 7; i++) + { + _permArray[i] = i - 1; + } + } + + private void PermuteArray(int n) + { + int k; + s_pctr = s_pctr + 1; + if (n != 1) + { + PermuteArray(n - 1); + for (k = n - 1; k >= 1; k--) + { + Swap(_permArray, n, k); + PermuteArray(n - 1); + Swap(_permArray, n, k); + } + } + } + + private bool Validate() + { + int k = 0; + + for (int i = 0; i <= 6; i++) + { + for (int j = 1; j <= 7; j++) + { + if (_permArray[j] == i) + { + k = k + 1; + } + } + } + + return (k == 7); + } + + [MethodImpl(MethodImplOptions.NoInlining)] + private bool Bench() + { + Initialize(); + + for (int i = 0; i < Iterations; ++i) + { + s_pctr = 0; + PermuteArray(7); + } + + bool result = Validate(); + + return result; + } + + [Benchmark] + public static void Test() + { + Permutate P = new Permutate(); + foreach (var iteration in Benchmark.Iterations) + { + using (iteration.StartMeasurement()) + { + P.Bench(); + } + } + } + + private static bool TestBase() + { + Permutate P = new Permutate(); + bool result = P.Bench(); + return result; + } + + public static int Main() + { + bool result = TestBase(); + return (result ? 100 : -1); + } +} diff --git a/tests/src/JIT/Performance/CodeQuality/BenchI/Permutate/Permutate.csproj b/tests/src/JIT/Performance/CodeQuality/BenchI/Permutate/Permutate.csproj new file mode 100644 index 0000000000..cbe2114304 --- /dev/null +++ b/tests/src/JIT/Performance/CodeQuality/BenchI/Permutate/Permutate.csproj @@ -0,0 +1,43 @@ + + + + + Debug + AnyCPU + 2.0 + {95DFC527-4DC1-495E-97D7-E94EE1F7140D} + Exe + Properties + 512 + {786C830F-07A1-408B-BD7F-6EE04809D6DB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} + $(ProgramFiles)\Common Files\microsoft shared\VSTT\11.0\UITestExtensionPackages + ..\..\ + 7a9bfb7d + .NETStandard,Version=v1.4 + + + + + + pdbonly + true + + + + False + + + + + + + + + + $(JitPackagesConfigFileDirectory)benchmark\project.json + $(JitPackagesConfigFileDirectory)benchmark\project.lock.json + + + + + diff --git a/tests/src/JIT/Performance/CodeQuality/BenchI/Pi/Pi.cs b/tests/src/JIT/Performance/CodeQuality/BenchI/Pi/Pi.cs new file mode 100644 index 0000000000..112ff6df9b --- /dev/null +++ b/tests/src/JIT/Performance/CodeQuality/BenchI/Pi/Pi.cs @@ -0,0 +1,86 @@ +// 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 System; +using System.Runtime.CompilerServices; +using Xunit; + +[assembly: OptimizeForBenchmarks] +[assembly: MeasureInstructionsRetired] + +public static class Pi +{ + +#if DEBUG + public const int Iterations = 1; +#else + public const int Iterations = 100; +#endif + + static int[] ComputePi(int[] a) { + + int d = 4; + int r = 10000; + int n = 251; + int m = (int)(3.322 * n * d); + int[] digits = new int[n]; + int i, k, q; + + for (i = 0; i <= m; i++) { + a[i] = 2; + } + + a[m] = 4; + + for (i = 1; i <= n; i++) { + q = 0; + for (k = m; k > 0L; k--) { + a[k] = a[k] * r + q; + q = a[k] / (2 * k + 1); + a[k] -= (2 * k + 1) * q; + q *= k; + } + a[0] = a[0] * r + q; + q = a[0] / r; + a[0] -= q * r; + digits[i-1] = q; + } + + return digits; + } + + [MethodImpl(MethodImplOptions.NoInlining)] + static bool Bench(int[] a) { + int[] digits = ComputePi(a); + return (digits[0] == 3 && digits[1] == 1415 && digits[2] == 9265 && digits[250] == 1989); + } + + [Benchmark] + public static void Test() { + int[] a = new int[3340]; + foreach (var iteration in Benchmark.Iterations) { + using (iteration.StartMeasurement()) { + for (int i = 0; i < Iterations; i++) { + Bench(a); + } + } + } + } + + static bool TestBase() { + bool result = true; + int[] a = new int[3340]; + for (int i = 0; i < Iterations; i++) { + result &= Bench(a); + } + return result; + } + + public static int Main() { + bool result = TestBase(); + return (result ? 100 : -1); + } +} diff --git a/tests/src/JIT/Performance/CodeQuality/BenchI/Pi/Pi.csproj b/tests/src/JIT/Performance/CodeQuality/BenchI/Pi/Pi.csproj new file mode 100644 index 0000000000..d7d212eace --- /dev/null +++ b/tests/src/JIT/Performance/CodeQuality/BenchI/Pi/Pi.csproj @@ -0,0 +1,43 @@ + + + + + Debug + AnyCPU + 2.0 + {95DFC527-4DC1-495E-97D7-E94EE1F7140D} + Exe + Properties + 512 + {786C830F-07A1-408B-BD7F-6EE04809D6DB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} + $(ProgramFiles)\Common Files\microsoft shared\VSTT\11.0\UITestExtensionPackages + ..\..\ + 7a9bfb7d + .NETStandard,Version=v1.4 + + + + + + pdbonly + true + + + + False + + + + + + + + + + $(JitPackagesConfigFileDirectory)benchmark\project.json + $(JitPackagesConfigFileDirectory)benchmark\project.lock.json + + + + + diff --git a/tests/src/JIT/Performance/CodeQuality/BenchI/Puzzle/Puzzle.cs b/tests/src/JIT/Performance/CodeQuality/BenchI/Puzzle/Puzzle.cs new file mode 100644 index 0000000000..2b1c2847f4 --- /dev/null +++ b/tests/src/JIT/Performance/CodeQuality/BenchI/Puzzle/Puzzle.cs @@ -0,0 +1,393 @@ +// 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 System; +using System.Runtime.CompilerServices; +using Xunit; + +[assembly: OptimizeForBenchmarks] +[assembly: MeasureInstructionsRetired] + +public class Puzzle +{ +#if DEBUG + public const int Iterations = 1; +#else + public const int Iterations = 400; +#endif + + private const int PuzzleSize = 511; + private const int ClassMax = 3; + private const int TypeMax = 12; + private const int D = 8; + + private int[] _pieceCount = new int[ClassMax + 1]; + private int[] _class = new int[TypeMax + 1]; + private int[] _pieceMax = new int[TypeMax + 1]; + private bool[] _puzzle = new bool[PuzzleSize + 1]; + private bool[][] _p; + private int _count; + + private static T[][] AllocArray(int n1, int n2) + { + T[][] a = new T[n1][]; + for (int i = 0; i < n1; ++i) + { + a[i] = new T[n2]; + } + + return a; + } + + private bool Fit(int i, int j) + { + for (int k = 0; k <= _pieceMax[i]; k++) + { + if (_p[i][k]) + { + if (_puzzle[j + k]) + { + return false; + } + } + } + + return true; + } + + private int Place(int i, int j) + { + int k; + for (k = 0; k <= _pieceMax[i]; k++) + { + if (_p[i][k]) + { + _puzzle[j + k] = true; + } + } + + _pieceCount[_class[i]] = _pieceCount[_class[i]] - 1; + + for (k = j; k <= PuzzleSize; k++) + { + if (!_puzzle[k]) + { + return k; + } + } + + return 0; + } + + private void RemoveLocal(int i, int j) + { + for (int k = 0; k <= _pieceMax[i]; k++) + { + if (_p[i][k]) + { + _puzzle[j + k] = false; + } + } + + _pieceCount[_class[i]] = _pieceCount[_class[i]] + 1; + } + + private bool Trial(int j) + { + for (int i = 0; i <= TypeMax; i++) + { + if (_pieceCount[_class[i]] != 0) + { + if (Fit(i, j)) + { + int k = Place(i, j); + if (Trial(k) || (k == 0)) + { + _count = _count + 1; + return true; + } + else + { + RemoveLocal(i, j); + } + } + } + } + + _count = _count + 1; + return false; + } + + private bool DoIt() + { + int i, j, k, m, n; + + for (m = 0; m <= PuzzleSize; m++) + { + _puzzle[m] = true; + } + + for (i = 1; i <= 5; i++) + { + for (j = 1; j <= 5; j++) + { + for (k = 1; k <= 5; k++) + { + _puzzle[i + D * (j + D * k)] = false; + } + } + } + + for (i = 0; i <= TypeMax; i++) + { + for (m = 0; m <= PuzzleSize; m++) + { + _p[i][m] = false; + } + } + + for (i = 0; i <= 3; i++) + { + for (j = 0; j <= 1; j++) + { + for (k = 0; k <= 0; k++) + { + _p[0][i + D * (j + D * k)] = true; + } + } + } + + _class[0] = 0; + _pieceMax[0] = 3 + D * 1 + D * D * 0; + + for (i = 0; i <= 1; i++) + { + for (j = 0; j <= 0; j++) + { + for (k = 0; k <= 3; k++) + { + _p[1][i + D * (j + D * k)] = true; + } + } + } + + _class[1] = 0; + _pieceMax[1] = 1 + D * 0 + D * D * 3; + + for (i = 0; i <= 0; i++) + { + for (j = 0; j <= 3; j++) + { + for (k = 0; k <= 1; k++) + { + _p[2][i + D * (j + D * k)] = true; + } + } + } + _class[2] = 0; + _pieceMax[2] = 0 + D * 3 + D * D * 1; + + for (i = 0; i <= 1; i++) + { + for (j = 0; j <= 3; j++) + { + for (k = 0; k <= 0; k++) + { + _p[3][i + D * (j + D * k)] = true; + } + } + } + + _class[3] = 0; + _pieceMax[3] = 1 + D * 3 + D * D * 0; + + for (i = 0; i <= 3; i++) + { + for (j = 0; j <= 0; j++) + { + for (k = 0; k <= 1; k++) + { + _p[4][i + D * (j + D * k)] = true; + } + } + } + + _class[4] = 0; + _pieceMax[4] = 3 + D * 0 + D * D * 1; + + for (i = 0; i <= 0; i++) + { + for (j = 0; j <= 1; j++) + { + for (k = 0; k <= 3; k++) + { + _p[5][i + D * (j + D * k)] = true; + } + } + } + + _class[5] = 0; + _pieceMax[5] = 0 + D * 1 + D * D * 3; + + for (i = 0; i <= 2; i++) + { + for (j = 0; j <= 0; j++) + { + for (k = 0; k <= 0; k++) + { + _p[6][i + D * (j + D * k)] = true; + } + } + } + + _class[6] = 1; + _pieceMax[6] = 2 + D * 0 + D * D * 0; + + for (i = 0; i <= 0; i++) + { + for (j = 0; j <= 2; j++) + { + for (k = 0; k <= 0; k++) + { + _p[7][i + D * (j + D * k)] = true; + } + } + } + + _class[7] = 1; + _pieceMax[7] = 0 + D * 2 + D * D * 0; + + for (i = 0; i <= 0; i++) + { + for (j = 0; j <= 0; j++) + { + for (k = 0; k <= 2; k++) + { + _p[8][i + D * (j + D * k)] = true; + } + } + } + + _class[8] = 1; + _pieceMax[8] = 0 + D * 0 + D * D * 2; + + for (i = 0; i <= 1; i++) + { + for (j = 0; j <= 1; j++) + { + for (k = 0; k <= 0; k++) + { + _p[9][i + D * (j + D * k)] = true; + } + } + } + _class[9] = 2; + _pieceMax[9] = 1 + D * 1 + D * D * 0; + + for (i = 0; i <= 1; i++) + { + for (j = 0; j <= 0; j++) + { + for (k = 0; k <= 1; k++) + { + _p[10][i + D * (j + D * k)] = true; + } + } + } + + _class[10] = 2; + _pieceMax[10] = 1 + D * 0 + D * D * 1; + + for (i = 0; i <= 0; i++) + { + for (j = 0; j <= 1; j++) + { + for (k = 0; k <= 1; k++) + { + _p[11][i + D * (j + D * k)] = true; + } + } + } + + _class[11] = 2; + _pieceMax[11] = 0 + D * 1 + D * D * 1; + + for (i = 0; i <= 1; i++) + { + for (j = 0; j <= 1; j++) + { + for (k = 0; k <= 1; k++) + { + _p[12][i + D * (j + D * k)] = true; + } + } + } + + _class[12] = 3; + _pieceMax[12] = 1 + D * 1 + D * D * 1; + _pieceCount[0] = 13; + _pieceCount[1] = 3; + _pieceCount[2] = 1; + _pieceCount[3] = 1; + m = 1 + D * (1 + D * 1); + _count = 0; + + bool result = true; + + if (Fit(0, m)) + { + n = Place(0, m); + result = Trial(n); + } + else + { + result = false; + } + + return result; + } + + [MethodImpl(MethodImplOptions.NoInlining)] + private bool Bench() + { + _p = AllocArray(TypeMax + 1, PuzzleSize + 1); + + bool result = true; + + for (int i = 0; i < Iterations; ++i) + { + result &= DoIt(); + } + + return result; + } + + [Benchmark] + public static void Test() + { + Puzzle P = new Puzzle(); + foreach (var iteration in Benchmark.Iterations) + { + using (iteration.StartMeasurement()) + { + P.Bench(); + } + } + } + + private static bool TestBase() + { + Puzzle P = new Puzzle(); + bool result = P.Bench(); + return result; + } + + public static int Main() + { + bool result = TestBase(); + return (result ? 100 : -1); + } +} diff --git a/tests/src/JIT/Performance/CodeQuality/BenchI/Puzzle/Puzzle.csproj b/tests/src/JIT/Performance/CodeQuality/BenchI/Puzzle/Puzzle.csproj new file mode 100644 index 0000000000..185bb7c08e --- /dev/null +++ b/tests/src/JIT/Performance/CodeQuality/BenchI/Puzzle/Puzzle.csproj @@ -0,0 +1,43 @@ + + + + + Debug + AnyCPU + 2.0 + {95DFC527-4DC1-495E-97D7-E94EE1F7140D} + Exe + Properties + 512 + {786C830F-07A1-408B-BD7F-6EE04809D6DB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} + $(ProgramFiles)\Common Files\microsoft shared\VSTT\11.0\UITestExtensionPackages + ..\..\ + 7a9bfb7d + .NETStandard,Version=v1.4 + + + + + + pdbonly + true + + + + False + + + + + + + + + + $(JitPackagesConfigFileDirectory)benchmark\project.json + $(JitPackagesConfigFileDirectory)benchmark\project.lock.json + + + + + diff --git a/tests/src/JIT/Performance/CodeQuality/BenchI/QuickSort/QuickSort.cs b/tests/src/JIT/Performance/CodeQuality/BenchI/QuickSort/QuickSort.cs new file mode 100644 index 0000000000..191211aaa8 --- /dev/null +++ b/tests/src/JIT/Performance/CodeQuality/BenchI/QuickSort/QuickSort.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 System; +using System.Runtime.CompilerServices; +using Xunit; + +[assembly: OptimizeForBenchmarks] +[assembly: MeasureInstructionsRetired] + +public static class QuickSort +{ + +#if DEBUG + public const int Iterations = 1; +#else + public const int Iterations = 80000; +#endif + + const int MAXNUM = 200; + const int MODULUS = 0x20000; + const int C = 13849; + const int A = 25173; + static int s_seed = 7; + + static int Random(int size) { + unchecked { + s_seed = s_seed * A + C; + } + return (s_seed % size); + } + + static void Quick(int lo, int hi, int[] arr) { + + int i, j; + int pivot, temp; + + if (lo < hi) { + for (i = lo, j = hi, pivot = arr[hi]; i < j;) { + while (i < j && arr[i] <= pivot){ + ++i; + } + while (j > i && arr[j] >= pivot) { + --j; + } + if (i < j) { + temp = arr[i]; + arr[i] = arr[j]; + arr[j] = temp; + } + } + + // need to swap the pivot and a[i](or a[j] as i==j) so + // that the pivot will be at its final place in the sorted array + + if (i != hi) { + temp = arr[i]; + arr[i] = pivot; + arr[hi] = temp; + } + Quick(lo, i - 1, arr); + Quick(i + 1, hi, arr); + } + } + + [MethodImpl(MethodImplOptions.NoInlining)] + static bool Bench() { + + int[] buffer = new int[MAXNUM]; + + for (int j = 0; j < MAXNUM; ++j) { + int temp = Random(MODULUS); + if (temp < 0){ + temp = (-temp); + } + buffer[j] = temp; + } + + Quick(0, MAXNUM - 1, buffer); + + for (int j = 0; j < MAXNUM - 1; ++j) { + if (buffer[j] > buffer[j+1]) { + return false; + } + } + + return true; + } + + [Benchmark] + public static void Test() { + foreach (var iteration in Benchmark.Iterations) { + using (iteration.StartMeasurement()) { + for (int i = 0; i < Iterations; i++) { + Bench(); + } + } + } + } + + static bool TestBase() { + bool result = true; + for (int i = 0; i < Iterations; i++) { + result &= Bench(); + } + return result; + } + + public static int Main() { + bool result = TestBase(); + return (result ? 100 : -1); + } +} diff --git a/tests/src/JIT/Performance/CodeQuality/BenchI/QuickSort/QuickSort.csproj b/tests/src/JIT/Performance/CodeQuality/BenchI/QuickSort/QuickSort.csproj new file mode 100644 index 0000000000..f079692c3c --- /dev/null +++ b/tests/src/JIT/Performance/CodeQuality/BenchI/QuickSort/QuickSort.csproj @@ -0,0 +1,43 @@ + + + + + Debug + AnyCPU + 2.0 + {95DFC527-4DC1-495E-97D7-E94EE1F7140D} + Exe + Properties + 512 + {786C830F-07A1-408B-BD7F-6EE04809D6DB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} + $(ProgramFiles)\Common Files\microsoft shared\VSTT\11.0\UITestExtensionPackages + ..\..\ + 7a9bfb7d + .NETStandard,Version=v1.4 + + + + + + pdbonly + true + + + + False + + + + + + + + + + $(JitPackagesConfigFileDirectory)benchmark\project.json + $(JitPackagesConfigFileDirectory)benchmark\project.lock.json + + + + + diff --git a/tests/src/JIT/Performance/CodeQuality/BenchI/TreeInsert/TreeInsert.cs b/tests/src/JIT/Performance/CodeQuality/BenchI/TreeInsert/TreeInsert.cs new file mode 100644 index 0000000000..8a24240538 --- /dev/null +++ b/tests/src/JIT/Performance/CodeQuality/BenchI/TreeInsert/TreeInsert.cs @@ -0,0 +1,138 @@ +// 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 System; +using System.Runtime.CompilerServices; +using Xunit; + +[assembly: OptimizeForBenchmarks] +[assembly: MeasureInstructionsRetired] + +public class TreeInsert +{ +#if DEBUG + public const int Iterations = 1; +#else + public const int Iterations = 15000; +#endif + + private struct Node + { + public int A; + public int L; + public int R; + } + + private struct Tree + { + public int Root; + public int NextAvail; + public Node[] Nodes; + } + + private Tree _s; + + public TreeInsert() + { + _s.Nodes = new Node[10001]; + } + + private void BenchInner(int x) + { + /* a tree insertion routine from knuth */ + int i = _s.Root; + int j = _s.NextAvail; + + L10: + /* compare */ + if (_s.Nodes[i].A < x) + { + if (_s.Nodes[i].L != 0) + { + i = _s.Nodes[i].L; + goto L10; + } + else + { + _s.Nodes[i].L = j; + goto L20; + } + } + else + { + if (_s.Nodes[i].R != 0) + { + i = _s.Nodes[i].R; + goto L10; + } + else + { + _s.Nodes[i].R = j; + goto L20; + } + } + + L20: + /* insert */ + _s.Nodes[j].A = x; + _s.Nodes[j].L = 0; + _s.Nodes[j].R = 0; + _s.NextAvail = j + 1; + } + + + [MethodImpl(MethodImplOptions.NoInlining)] + private bool Bench() + { + _s.Root = 1; + _s.NextAvail = 2; + _s.Nodes[1].A = 300; + _s.Nodes[1].L = 0; + _s.Nodes[1].R = 0; + + int j = 99999; + for (int i = 1; i <= 900; i++) + { + BenchInner(j & 4095); + j = j + 33333; + } + + return (_s.Nodes[500].A == 441); + } + + [Benchmark] + public static void Test() + { + TreeInsert T = new TreeInsert(); + foreach (var iteration in Benchmark.Iterations) + { + using (iteration.StartMeasurement()) + { + for (int i = 1; i <= Iterations; i++) + { + T.Bench(); + } + } + } + } + + private static bool TestBase() + { + TreeInsert T = new TreeInsert(); + bool result = true; + for (int i = 1; i <= Iterations; i++) + { + result &= T.Bench(); + } + return result; + } + + public static int Main() + { + bool result = TestBase(); + return (result ? 100 : -1); + } +} diff --git a/tests/src/JIT/Performance/CodeQuality/BenchI/TreeInsert/TreeInsert.csproj b/tests/src/JIT/Performance/CodeQuality/BenchI/TreeInsert/TreeInsert.csproj new file mode 100644 index 0000000000..919c7d2b6e --- /dev/null +++ b/tests/src/JIT/Performance/CodeQuality/BenchI/TreeInsert/TreeInsert.csproj @@ -0,0 +1,43 @@ + + + + + Debug + AnyCPU + 2.0 + {95DFC527-4DC1-495E-97D7-E94EE1F7140D} + Exe + Properties + 512 + {786C830F-07A1-408B-BD7F-6EE04809D6DB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} + $(ProgramFiles)\Common Files\microsoft shared\VSTT\11.0\UITestExtensionPackages + ..\..\ + 7a9bfb7d + .NETStandard,Version=v1.4 + + + + + + pdbonly + true + + + + False + + + + + + + + + + $(JitPackagesConfigFileDirectory)benchmark\project.json + $(JitPackagesConfigFileDirectory)benchmark\project.lock.json + + + + + diff --git a/tests/src/JIT/Performance/CodeQuality/BenchI/TreeSort/TreeSort.cs b/tests/src/JIT/Performance/CodeQuality/BenchI/TreeSort/TreeSort.cs new file mode 100644 index 0000000000..68b5c965f7 --- /dev/null +++ b/tests/src/JIT/Performance/CodeQuality/BenchI/TreeSort/TreeSort.cs @@ -0,0 +1,151 @@ +// 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 System; +using System.Runtime.CompilerServices; +using Xunit; + +[assembly: OptimizeForBenchmarks] +[assembly: MeasureInstructionsRetired] + +public static class TreeSort +{ + +#if DEBUG + public const int Iterations = 1; +#else + public const int Iterations = 1200; +#endif + + const int SortElements = 5000; + const int Modulus = 65536; + + sealed class Node + { + public Node Left; + public Node Right; + public int Val; + public Node(int n) + { + Left = null; + Right = null; + Val = n; + } + } + + static int s_biggest; + static int s_littlest; + static int s_seed; + + static void InitRand() { + s_seed = 33; + } + + static int Rand(ref int seed) { + int multiplier = 25173; + int increment = 13849; + seed = (multiplier * seed + increment) % Modulus; + return seed; + } + + static void InitArray(int[] sortList) { + InitRand(); + s_biggest = 0; + s_littlest = Modulus; + for (int i = 1; i <= SortElements; i++) { + sortList[i] = Rand(ref s_seed) - 1; + if (sortList[i] > s_biggest) { + s_biggest = sortList[i]; + } + else if (sortList[i] < s_littlest) { + s_littlest = sortList[i]; + } + } + } + + static void Insert(int n, Node t) { + if (n > t.Val) { + if (t.Left == null) { + t.Left = new Node(n); + } + else { + Insert(n, t.Left); + } + } + else if (n < t.Val) { + if (t.Right == null) { + t.Right = new Node(n); + } + else { + Insert(n, t.Right); + } + } + } + + static bool CheckTree(Node p) { + bool result = true; + if (p.Left != null) { + if (p.Left.Val <= p.Val) { + result = false; + } + else { + result &= CheckTree(p.Left); + } + } + + if (p.Right != null) { + if (p.Right.Val >= p.Val) { + result = false; + } + else { + result &= CheckTree(p.Right); + } + } + + return result; + } + + static bool Trees(int[] sortList) { + InitArray(sortList); + Node tree = new Node(sortList[1]); + for (int i = 2; i <= SortElements; i++) { + Insert(sortList[i], tree); + } + bool result = CheckTree(tree); + return result; + } + + [MethodImpl(MethodImplOptions.NoInlining)] + static bool Bench() { + int[] sortList = new int[SortElements + 1]; + bool result = Trees(sortList); + return result; + } + + [Benchmark] + public static void Test() { + foreach (var iteration in Benchmark.Iterations) { + using (iteration.StartMeasurement()) { + for (int i = 0; i < Iterations; i++) { + Bench(); + } + } + } + } + + static bool TestBase() { + bool result = true; + for (int i = 0; i < Iterations; i++) { + result &= Bench(); + } + return result; + } + + public static int Main() { + bool result = TestBase(); + return (result ? 100 : -1); + } +} diff --git a/tests/src/JIT/Performance/CodeQuality/BenchI/TreeSort/TreeSort.csproj b/tests/src/JIT/Performance/CodeQuality/BenchI/TreeSort/TreeSort.csproj new file mode 100644 index 0000000000..b9f78517a9 --- /dev/null +++ b/tests/src/JIT/Performance/CodeQuality/BenchI/TreeSort/TreeSort.csproj @@ -0,0 +1,43 @@ + + + + + Debug + AnyCPU + 2.0 + {95DFC527-4DC1-495E-97D7-E94EE1F7140D} + Exe + Properties + 512 + {786C830F-07A1-408B-BD7F-6EE04809D6DB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} + $(ProgramFiles)\Common Files\microsoft shared\VSTT\11.0\UITestExtensionPackages + ..\..\ + 7a9bfb7d + .NETStandard,Version=v1.4 + + + + + + pdbonly + true + + + + False + + + + + + + + + + $(JitPackagesConfigFileDirectory)benchmark\project.json + $(JitPackagesConfigFileDirectory)benchmark\project.lock.json + + + + + diff --git a/tests/src/JIT/Performance/CodeQuality/BenchI/XposMatrix/XposMatrix.cs b/tests/src/JIT/Performance/CodeQuality/BenchI/XposMatrix/XposMatrix.cs new file mode 100644 index 0000000000..0bb17b8cc5 --- /dev/null +++ b/tests/src/JIT/Performance/CodeQuality/BenchI/XposMatrix/XposMatrix.cs @@ -0,0 +1,90 @@ +// 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 System; +using System.Runtime.CompilerServices; +using Xunit; + +[assembly: OptimizeForBenchmarks] +[assembly: MeasureInstructionsRetired] + +public static class XposMatrix +{ + public const int ArraySize = 100; + +#if DEBUG + public const int Iterations = 1; +#else + public const int Iterations = 25000; +#endif + + static T[][] AllocArray(int n1, int n2) { + T[][] a = new T[n1][]; + for (int i = 0; i < n1; ++i) { + a[i] = new T[n2]; + } + return a; + } + + static void Inner(int[][] x, int n) { + for (int i = 1; i <= n; i++) { + for (int j = 1; j <= n; j++) { + int t = x[i][j]; + x[i][j] = x[j][i]; + x[j][i] = t; + } + } + } + + [MethodImpl(MethodImplOptions.NoInlining)] + static bool Bench(int[][] matrix) { + + int n = ArraySize; + for (int i = 1; i <= n; i++) { + for (int j = 1; j <= n; j++) { + matrix[i][j] = 1; + } + } + + if (matrix[n][n] != 1) { + return false; + } + + Inner(matrix, n); + + if (matrix[n][n] != 1) { + return false; + } + + return true; + } + + [Benchmark] + public static void Test() { + int[][] matrix = AllocArray(ArraySize + 1, ArraySize + 1); + foreach (var iteration in Benchmark.Iterations) { + using (iteration.StartMeasurement()) { + for (int i = 0; i < Iterations; i++) { + Bench(matrix); + } + } + } + } + + static bool TestBase() { + int[][] matrix = AllocArray(ArraySize + 1, ArraySize + 1); + bool result = true; + for (int i = 0; i < Iterations; i++) { + result &= Bench(matrix); + } + return result; + } + + public static int Main() { + bool result = TestBase(); + return (result ? 100 : -1); + } +} diff --git a/tests/src/JIT/Performance/CodeQuality/BenchI/XposMatrix/XposMatrix.csproj b/tests/src/JIT/Performance/CodeQuality/BenchI/XposMatrix/XposMatrix.csproj new file mode 100644 index 0000000000..922fd5fc39 --- /dev/null +++ b/tests/src/JIT/Performance/CodeQuality/BenchI/XposMatrix/XposMatrix.csproj @@ -0,0 +1,43 @@ + + + + + Debug + AnyCPU + 2.0 + {95DFC527-4DC1-495E-97D7-E94EE1F7140D} + Exe + Properties + 512 + {786C830F-07A1-408B-BD7F-6EE04809D6DB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} + $(ProgramFiles)\Common Files\microsoft shared\VSTT\11.0\UITestExtensionPackages + ..\..\ + 7a9bfb7d + .NETStandard,Version=v1.4 + + + + + + pdbonly + true + + + + False + + + + + + + + + + $(JitPackagesConfigFileDirectory)benchmark\project.json + $(JitPackagesConfigFileDirectory)benchmark\project.lock.json + + + + + -- cgit v1.2.3