| 1 |
// Licensed to the .NET Foundation under one or more agreements. |
| 2 |
// The .NET Foundation licenses this file to you under the MIT license. |
| 3 |
// See the LICENSE file in the project root for more information. |
| 4 |
|
| 5 |
using System.Collections.Generic; |
| 6 |
using System.Linq; |
| 7 |
using Xunit; |
| 8 |
using Microsoft.Xunit.Performance; |
| 9 |
|
| 10 |
namespace System.Linq.Tests |
| 11 |
{ |
| 12 |
public class Perf_Linq |
| 13 |
{ |
| 14 |
#region Helper Methods |
| 15 |
|
| 16 |
/// <summary> |
| 17 |
/// Provides TestInfo data to xunit performance tests |
| 18 |
/// </summary> |
| 19 |
public static IEnumerable<object[]> IterationSizeWrapperData() |
| 20 |
{ |
| 21 |
int[] iterations = { 1000 }; |
| 22 |
int[] sizes = { 100 }; |
| 23 |
foreach (int iteration in iterations) |
| 24 |
foreach (int size in sizes) |
| 25 |
{ |
| 26 |
yield return new object[] { size, iteration, Perf_LinqTestBase.WrapperType.NoWrap }; |
| 27 |
yield return new object[] { size, iteration, Perf_LinqTestBase.WrapperType.IEnumerable }; |
| 28 |
yield return new object[] { size, iteration, Perf_LinqTestBase.WrapperType.IReadOnlyCollection }; |
| 29 |
yield return new object[] { size, iteration, Perf_LinqTestBase.WrapperType.ICollection }; |
| 30 |
} |
| 31 |
} |
| 32 |
|
| 33 |
/// <summary> |
| 34 |
/// Provides TestInfo data to xunit performance tests |
| 35 |
/// </summary> |
| 36 |
public static IEnumerable<object[]> IterationSizeWrapperDataNoWrapper() |
| 37 |
{ |
| 38 |
int[] iterations = { 1000 }; |
| 39 |
int[] sizes = { 100 }; |
| 40 |
foreach (int iteration in iterations) |
| 41 |
foreach (int size in sizes) |
| 42 |
yield return new object[] { size, iteration }; |
| 43 |
} |
| 44 |
|
| 45 |
private class BaseClass |
| 46 |
{ |
| 47 |
public int Value; |
| 48 |
} |
| 49 |
private class ChildClass : BaseClass |
| 50 |
{ |
| 51 |
public int ChildValue; |
| 52 |
} |
| 53 |
|
| 54 |
#endregion |
| 55 |
|
| 56 |
#region Perf Tests |
| 57 |
|
| 58 |
[Benchmark] |
| 59 |
[MemberData(nameof(IterationSizeWrapperData))] |
| 60 |
public void Select(int size, int iteration, Perf_LinqTestBase.WrapperType wrapType) |
| 61 |
{ |
| 62 |
Perf_LinqTestBase.Measure<int>(size, iteration, wrapType, col => col.Select(o => o + 1)); |
| 63 |
} |
| 64 |
|
| 65 |
[Benchmark] |
| 66 |
[MemberData(nameof(IterationSizeWrapperData))] |
| 67 |
public void SelectSelect(int size, int iteration, Perf_LinqTestBase.WrapperType wrapType) |
| 68 |
{ |
| 69 |
Perf_LinqTestBase.Measure<int>(size, iteration, wrapType, col => col.Select(o => o + 1).Select(o => o - 1)); |
| 70 |
} |
| 71 |
|
| 72 |
[Benchmark] |
| 73 |
[MemberData(nameof(IterationSizeWrapperData))] |
| 74 |
public void Where(int size, int iteration, Perf_LinqTestBase.WrapperType wrapType) |
| 75 |
{ |
| 76 |
Perf_LinqTestBase.Measure<int>(size, iteration, wrapType, col => col.Where(o => o >= 0)); |
| 77 |
} |
| 78 |
|
| 79 |
[Benchmark] |
| 80 |
[MemberData(nameof(IterationSizeWrapperData))] |
| 81 |
public void WhereWhere(int size, int iteration, Perf_LinqTestBase.WrapperType wrapType) |
| 82 |
{ |
| 83 |
Perf_LinqTestBase.Measure<int>(size, iteration, wrapType, col => col.Where(o => o >= 0).Where(o => o >= -1)); |
| 84 |
} |
| 85 |
|
| 86 |
[Benchmark] |
| 87 |
[MemberData(nameof(IterationSizeWrapperData))] |
| 88 |
public void WhereSelect(int size, int iteration, Perf_LinqTestBase.WrapperType wrapType) |
| 89 |
{ |
| 90 |
Perf_LinqTestBase.Measure<int>(size, iteration, wrapType, col => col.Where(o => o >= 0).Select(o => o + 1)); |
| 91 |
} |
| 92 |
|
| 93 |
[Benchmark] |
| 94 |
[MemberData(nameof(IterationSizeWrapperData))] |
| 95 |
public void Cast_ToBaseClass(int size, int iteration, Perf_LinqTestBase.WrapperType wrapType) |
| 96 |
{ |
| 97 |
Func<IEnumerable<ChildClass>, IEnumerable<BaseClass>> linqApply = col => col.Cast<BaseClass>(); |
| 98 |
ChildClass val = new ChildClass() { Value = 1, ChildValue = 2 }; |
| 99 |
Perf_LinqTestBase.Measure<ChildClass, BaseClass>(10, 5, val, wrapType, linqApply); |
| 100 |
} |
| 101 |
|
| 102 |
[Benchmark] |
| 103 |
[MemberData(nameof(IterationSizeWrapperData))] |
| 104 |
public void Cast_SameType(int size, int iteration, Perf_LinqTestBase.WrapperType wrapType) |
| 105 |
{ |
| 106 |
Func<IEnumerable<int>, IEnumerable<int>> linqApply = col => col.Cast<int>(); |
| 107 |
int val = 1; |
| 108 |
Perf_LinqTestBase.Measure<int, int>(10, 5, val, wrapType, linqApply); |
| 109 |
} |
| 110 |
|
| 111 |
[Benchmark] |
| 112 |
[MemberData(nameof(IterationSizeWrapperData))] |
| 113 |
public void OrderBy(int size, int iteration, Perf_LinqTestBase.WrapperType wrapType) |
| 114 |
{ |
| 115 |
Perf_LinqTestBase.Measure<int>(size, iteration, wrapType, col => col.OrderBy(o => -o)); |
| 116 |
} |
| 117 |
|
| 118 |
[Benchmark] |
| 119 |
[MemberData(nameof(IterationSizeWrapperData))] |
| 120 |
public void OrderByDescending(int size, int iteration, Perf_LinqTestBase.WrapperType wrapType) |
| 121 |
{ |
| 122 |
Perf_LinqTestBase.Measure<int>(size, iteration, wrapType, col => col.OrderByDescending(o => -o)); |
| 123 |
} |
| 124 |
|
| 125 |
[Benchmark] |
| 126 |
[MemberData(nameof(IterationSizeWrapperData))] |
| 127 |
public void OrderByThenBy(int size, int iteration, Perf_LinqTestBase.WrapperType wrapType) |
| 128 |
{ |
| 129 |
Perf_LinqTestBase.Measure<int>(size, iteration, wrapType, col => col.OrderBy(o => -o).ThenBy(o => o)); |
| 130 |
} |
| 131 |
|
| 132 |
[Benchmark] |
| 133 |
[MemberData(nameof(IterationSizeWrapperDataNoWrapper))] |
| 134 |
public void Range(int size, int iteration) |
| 135 |
{ |
| 136 |
Perf_LinqTestBase.Measure<int>(1, iteration, Perf_LinqTestBase.WrapperType.NoWrap, col => Enumerable.Range(0, size)); |
| 137 |
} |
| 138 |
|
| 139 |
[Benchmark] |
| 140 |
[MemberData(nameof(IterationSizeWrapperDataNoWrapper))] |
| 141 |
public void Repeat(int size, int iteration) |
| 142 |
{ |
| 143 |
Perf_LinqTestBase.Measure<int>(1, iteration, Perf_LinqTestBase.WrapperType.NoWrap, col => Enumerable.Repeat(0, size)); |
| 144 |
} |
| 145 |
|
| 146 |
[Benchmark] |
| 147 |
[MemberData(nameof(IterationSizeWrapperData))] |
| 148 |
public void Reverse(int size, int iteration, Perf_LinqTestBase.WrapperType wrapType) |
| 149 |
{ |
| 150 |
Perf_LinqTestBase.Measure<int>(size, iteration, wrapType, col => col.Reverse()); |
| 151 |
} |
| 152 |
|
| 153 |
[Benchmark] |
| 154 |
[MemberData(nameof(IterationSizeWrapperData))] |
| 155 |
public void Skip(int size, int iteration, Perf_LinqTestBase.WrapperType wrapType) |
| 156 |
{ |
| 157 |
Perf_LinqTestBase.Measure<int>(size, iteration, wrapType, col => col.Skip(1)); |
| 158 |
} |
| 159 |
|
| 160 |
[Benchmark] |
| 161 |
[MemberData(nameof(IterationSizeWrapperData))] |
| 162 |
public void Take(int size, int iteration, Perf_LinqTestBase.WrapperType wrapType) |
| 163 |
{ |
| 164 |
Perf_LinqTestBase.Measure<int>(size, iteration, wrapType, col => col.Take(size - 1)); |
| 165 |
} |
| 166 |
|
| 167 |
[Benchmark] |
| 168 |
[MemberData(nameof(IterationSizeWrapperData))] |
| 169 |
public void SkipTake(int size, int iteration, Perf_LinqTestBase.WrapperType wrapType) |
| 170 |
{ |
| 171 |
Perf_LinqTestBase.Measure<int>(size, iteration, wrapType, col => col.Skip(1).Take(size - 2)); |
| 172 |
} |
| 173 |
|
| 174 |
[Benchmark] |
| 175 |
[MemberData(nameof(IterationSizeWrapperData))] |
| 176 |
public void ToArray(int size, int iteration, Perf_LinqTestBase.WrapperType wrapType) |
| 177 |
{ |
| 178 |
int[] array = Enumerable.Range(0, size).ToArray(); |
| 179 |
Perf_LinqTestBase.MeasureMaterializationToArray<int>(Perf_LinqTestBase.Wrap(array, wrapType), iteration); |
| 180 |
} |
| 181 |
|
| 182 |
[Benchmark] |
| 183 |
[MemberData(nameof(IterationSizeWrapperData))] |
| 184 |
public void ToList(int size, int iteration, Perf_LinqTestBase.WrapperType wrapType) |
| 185 |
{ |
| 186 |
int[] array = Enumerable.Range(0, size).ToArray(); |
| 187 |
Perf_LinqTestBase.MeasureMaterializationToList<int>(Perf_LinqTestBase.Wrap(array, wrapType), iteration); |
| 188 |
} |
| 189 |
|
| 190 |
[Benchmark] |
| 191 |
[MemberData(nameof(IterationSizeWrapperData))] |
| 192 |
public void ToDictionary(int size, int iteration, Perf_LinqTestBase.WrapperType wrapType) |
| 193 |
{ |
| 194 |
int[] array = Enumerable.Range(0, size).ToArray(); |
| 195 |
Perf_LinqTestBase.MeasureMaterializationToDictionary<int>(Perf_LinqTestBase.Wrap(array, wrapType), iteration); |
| 196 |
} |
| 197 |
|
| 198 |
[Benchmark] |
| 199 |
[MemberData(nameof(IterationSizeWrapperData))] |
| 200 |
public void Contains_ElementNotFound(int size, int iterationCount, Perf_LinqTestBase.WrapperType wrapType) |
| 201 |
{ |
| 202 |
IEnumerable<int> source = Perf_LinqTestBase.Wrap(Enumerable.Range(0, size).ToArray(), wrapType); |
| 203 |
|
| 204 |
foreach (BenchmarkIteration iteration in Benchmark.Iterations) |
| 205 |
{ |
| 206 |
using (iteration.StartMeasurement()) |
| 207 |
{ |
| 208 |
for (int i = 0; i < iterationCount; i++) |
| 209 |
{ |
| 210 |
source.Contains(size + 1); |
| 211 |
} |
| 212 |
} |
| 213 |
} |
| 214 |
} |
| 215 |
|
| 216 |
[Benchmark] |
| 217 |
[MemberData(nameof(IterationSizeWrapperData))] |
| 218 |
public void Contains_FirstElementMatches(int size, int iterationCount, Perf_LinqTestBase.WrapperType wrapType) |
| 219 |
{ |
| 220 |
IEnumerable<int> source = Perf_LinqTestBase.Wrap(Enumerable.Range(0, size).ToArray(), wrapType); |
| 221 |
|
| 222 |
foreach (BenchmarkIteration iteration in Benchmark.Iterations) |
| 223 |
{ |
| 224 |
using (iteration.StartMeasurement()) |
| 225 |
{ |
| 226 |
for (int i = 0; i < iterationCount; i++) |
| 227 |
{ |
| 228 |
source.Contains(0); |
| 229 |
} |
| 230 |
} |
| 231 |
} |
| 232 |
} |
| 233 |
|
| 234 |
#endregion |
| 235 |
} |
| 236 |
} |