| 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; |
| 6 |
using System.Collections.Generic; |
| 7 |
using System.Linq; |
| 8 |
using Xunit; |
| 9 |
using System.Diagnostics; |
| 10 |
using Microsoft.Xunit.Performance; |
| 11 |
|
| 12 |
namespace System.Linq.Tests |
| 13 |
{ |
| 14 |
/// <summary> |
| 15 |
/// Classes and methods to unify performance testing logic |
| 16 |
/// </summary> |
| 17 |
public partial class Perf_LinqTestBase |
| 18 |
{ |
| 19 |
public class EnumerableWrapper<T> : IEnumerable<T> |
| 20 |
{ |
| 21 |
private T[] _array; |
| 22 |
public EnumerableWrapper(T[] array) { _array = array; } |
| 23 |
|
| 24 |
public IEnumerator<T> GetEnumerator() { return ((IEnumerable<T>)_array).GetEnumerator(); } |
| 25 |
Collections.IEnumerator Collections.IEnumerable.GetEnumerator() { return ((IEnumerable<T>)_array).GetEnumerator(); } |
| 26 |
} |
| 27 |
|
| 28 |
public class ReadOnlyCollectionWrapper<T> : IReadOnlyCollection<T> |
| 29 |
{ |
| 30 |
private T[] _array; |
| 31 |
public ReadOnlyCollectionWrapper(T[] array) { _array = array; } |
| 32 |
|
| 33 |
public int Count { get { return _array.Length; } } |
| 34 |
|
| 35 |
public IEnumerator<T> GetEnumerator() { return ((IEnumerable<T>)_array).GetEnumerator(); } |
| 36 |
Collections.IEnumerator Collections.IEnumerable.GetEnumerator() { return ((IEnumerable<T>)_array).GetEnumerator(); } |
| 37 |
} |
| 38 |
|
| 39 |
public class ReadOnlyListWrapper<T> : IReadOnlyList<T> |
| 40 |
{ |
| 41 |
private T[] _array; |
| 42 |
public ReadOnlyListWrapper(T[] array) { _array = array; } |
| 43 |
|
| 44 |
public int Count { get { return _array.Length; } } |
| 45 |
public T this[int index] { get { return _array[index]; } } |
| 46 |
|
| 47 |
public IEnumerator<T> GetEnumerator() { return ((IEnumerable<T>)_array).GetEnumerator(); } |
| 48 |
Collections.IEnumerator Collections.IEnumerable.GetEnumerator() { return ((IEnumerable<T>)_array).GetEnumerator(); } |
| 49 |
} |
| 50 |
|
| 51 |
public class CollectionWrapper<T> : ICollection<T> |
| 52 |
{ |
| 53 |
private T[] _array; |
| 54 |
public CollectionWrapper(T[] array) { _array = array; } |
| 55 |
|
| 56 |
public int Count { get { return _array.Length; } } |
| 57 |
public bool IsReadOnly { get { return true; } } |
| 58 |
public bool Contains(T item) |
| 59 |
{ |
| 60 |
return Array.IndexOf(_array, item) >= 0; |
| 61 |
} |
| 62 |
public void CopyTo(T[] array, int arrayIndex) |
| 63 |
{ |
| 64 |
_array.CopyTo(array, arrayIndex); |
| 65 |
} |
| 66 |
|
| 67 |
public void Add(T item) { throw new NotImplementedException(); } |
| 68 |
public void Clear() { throw new NotImplementedException(); } |
| 69 |
public bool Remove(T item) { throw new NotImplementedException(); } |
| 70 |
|
| 71 |
public IEnumerator<T> GetEnumerator() { return ((IEnumerable<T>)_array).GetEnumerator(); } |
| 72 |
Collections.IEnumerator Collections.IEnumerable.GetEnumerator() { return ((IEnumerable<T>)_array).GetEnumerator(); } |
| 73 |
} |
| 74 |
|
| 75 |
public class ListWrapper<T> : IList<T> |
| 76 |
{ |
| 77 |
private T[] _array; |
| 78 |
public ListWrapper(T[] array) { _array = array; } |
| 79 |
|
| 80 |
public int Count { get { return _array.Length; } } |
| 81 |
public bool IsReadOnly { get { return true; } } |
| 82 |
public T this[int index] |
| 83 |
{ |
| 84 |
get { return _array[index]; } |
| 85 |
set { throw new NotImplementedException(); } |
| 86 |
} |
| 87 |
public bool Contains(T item) |
| 88 |
{ |
| 89 |
return Array.IndexOf(_array, item) >= 0; |
| 90 |
} |
| 91 |
public void CopyTo(T[] array, int arrayIndex) |
| 92 |
{ |
| 93 |
_array.CopyTo(array, arrayIndex); |
| 94 |
} |
| 95 |
public int IndexOf(T item) |
| 96 |
{ |
| 97 |
return Array.IndexOf(_array, item); |
| 98 |
} |
| 99 |
|
| 100 |
public void Add(T item) { throw new NotImplementedException(); } |
| 101 |
public void Clear() { throw new NotImplementedException(); } |
| 102 |
public bool Remove(T item) { throw new NotImplementedException(); } |
| 103 |
public void Insert(int index, T item) { throw new NotImplementedException(); } |
| 104 |
public void RemoveAt(int index) { throw new NotImplementedException(); } |
| 105 |
|
| 106 |
|
| 107 |
public IEnumerator<T> GetEnumerator() { return ((IEnumerable<T>)_array).GetEnumerator(); } |
| 108 |
Collections.IEnumerator Collections.IEnumerable.GetEnumerator() { return ((IEnumerable<T>)_array).GetEnumerator(); } |
| 109 |
} |
| 110 |
|
| 111 |
// ============= |
| 112 |
|
| 113 |
/// <summary> |
| 114 |
/// Measures the time of iteration over IEnumerable sequence |
| 115 |
/// </summary> |
| 116 |
/// <typeparam name="T">Elements type</typeparam> |
| 117 |
/// <param name="source">Sequence</param> |
| 118 |
/// <param name="iterationCount">Number of passes</param> |
| 119 |
public static void MeasureIteration<T>(IEnumerable<T> source, int iterationCount) |
| 120 |
{ |
| 121 |
foreach (var iteration in Benchmark.Iterations) |
| 122 |
{ |
| 123 |
using (iteration.StartMeasurement()) |
| 124 |
{ |
| 125 |
for (int i = 0; i < iterationCount; i++) |
| 126 |
{ |
| 127 |
foreach (var item in source) |
| 128 |
{ } |
| 129 |
} |
| 130 |
} |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
/// <summary> |
| 135 |
/// Measures the time of materialization of IEnumerable sequence to Array |
| 136 |
/// </summary> |
| 137 |
/// <typeparam name="T">Elements type</typeparam> |
| 138 |
/// <param name="source">Sequence</param> |
| 139 |
/// <param name="iterationCount">Number of passes</param> |
| 140 |
public static void MeasureMaterializationToArray<T>(IEnumerable<T> source, int iterationCount) |
| 141 |
{ |
| 142 |
foreach (var iteration in Benchmark.Iterations) |
| 143 |
{ |
| 144 |
using (iteration.StartMeasurement()) |
| 145 |
{ |
| 146 |
for (int i = 0; i < iterationCount; i++) |
| 147 |
{ |
| 148 |
source.ToArray(); |
| 149 |
} |
| 150 |
} |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
/// <summary> |
| 155 |
/// Measures the time of materialization of IEnumerable sequence to List |
| 156 |
/// </summary> |
| 157 |
/// <typeparam name="T">Elements type</typeparam> |
| 158 |
/// <param name="source">Sequence</param> |
| 159 |
/// <param name="iterationCount">Number of passes</param> |
| 160 |
/// <returns>Measured time</returns> |
| 161 |
public static void MeasureMaterializationToList<T>(IEnumerable<T> source, int iterationCount) |
| 162 |
{ |
| 163 |
foreach (var iteration in Benchmark.Iterations) |
| 164 |
{ |
| 165 |
using (iteration.StartMeasurement()) |
| 166 |
{ |
| 167 |
for (int i = 0; i < iterationCount; i++) |
| 168 |
{ |
| 169 |
source.ToList(); |
| 170 |
} |
| 171 |
} |
| 172 |
} |
| 173 |
} |
| 174 |
|
| 175 |
/// <summary> |
| 176 |
/// Measures the time of materialization of IEnumerable sequence to Dictionary |
| 177 |
/// </summary> |
| 178 |
/// <typeparam name="T">Elements type</typeparam> |
| 179 |
/// <param name="source">Sequence</param> |
| 180 |
/// <param name="iterationCount">Number of passes</param> |
| 181 |
public static void MeasureMaterializationToDictionary<T>(IEnumerable<T> source, int iterationCount) |
| 182 |
{ |
| 183 |
int count = 0; |
| 184 |
foreach (var iteration in Benchmark.Iterations) |
| 185 |
{ |
| 186 |
using (iteration.StartMeasurement()) |
| 187 |
{ |
| 188 |
for (int i = 0; i < iterationCount; i++) |
| 189 |
{ |
| 190 |
source.ToDictionary(key => count++); |
| 191 |
} |
| 192 |
} |
| 193 |
} |
| 194 |
} |
| 195 |
|
| 196 |
// =============== |
| 197 |
|
| 198 |
public enum WrapperType |
| 199 |
{ |
| 200 |
NoWrap, |
| 201 |
IEnumerable, |
| 202 |
IReadOnlyCollection, |
| 203 |
IReadOnlyList, |
| 204 |
ICollection, |
| 205 |
IList |
| 206 |
} |
| 207 |
|
| 208 |
/// <summary> |
| 209 |
/// Wrap array with one of wrapper types |
| 210 |
/// </summary> |
| 211 |
public static IEnumerable<T> Wrap<T>(T[] source, WrapperType wrapperKind) |
| 212 |
{ |
| 213 |
switch (wrapperKind) |
| 214 |
{ |
| 215 |
case WrapperType.NoWrap: |
| 216 |
return source; |
| 217 |
case WrapperType.IEnumerable: |
| 218 |
return new EnumerableWrapper<T>(source); |
| 219 |
case WrapperType.ICollection: |
| 220 |
return new CollectionWrapper<T>(source); |
| 221 |
case WrapperType.IReadOnlyCollection: |
| 222 |
return new ReadOnlyCollectionWrapper<T>(source); |
| 223 |
case WrapperType.IReadOnlyList: |
| 224 |
return new ReadOnlyListWrapper<T>(source); |
| 225 |
case WrapperType.IList: |
| 226 |
return new ListWrapper<T>(source); |
| 227 |
} |
| 228 |
|
| 229 |
return source; |
| 230 |
} |
| 231 |
|
| 232 |
/// <summary> |
| 233 |
/// Main method to measure performance. |
| 234 |
/// Creates array of Int32 with length 'elementCount', wraps it by one of the wrapper, applies LINQ and measures materialization to Array |
| 235 |
/// </summary> |
| 236 |
public static void Measure<TElement>(int elementCount, int iterationCount, WrapperType wrapperKind, Func<IEnumerable<int>, IEnumerable<TElement>> applyLINQ) |
| 237 |
{ |
| 238 |
int[] data = Enumerable.Range(0, elementCount).ToArray(); |
| 239 |
IEnumerable<int> wrapper = Wrap(data, wrapperKind); |
| 240 |
|
| 241 |
IEnumerable<TElement> linqExpr = applyLINQ(wrapper); |
| 242 |
MeasureMaterializationToArray(linqExpr, iterationCount); |
| 243 |
} |
| 244 |
|
| 245 |
/// <summary> |
| 246 |
/// Main method to measure performance. |
| 247 |
/// Creates array of TSource with length 'elementCount', wraps it by one of the wrapper, applies LINQ and measures materialization to Array |
| 248 |
/// </summary> |
| 249 |
public static void Measure<TSource, TElement>(int elementCount, int iterationCount, TSource defaultValue, WrapperType wrapperKind, Func<IEnumerable<TSource>, IEnumerable<TElement>> applyLINQ) |
| 250 |
{ |
| 251 |
TSource[] data = Enumerable.Repeat(defaultValue, elementCount).ToArray(); |
| 252 |
IEnumerable<TSource> wrapper = Wrap(data, wrapperKind); |
| 253 |
|
| 254 |
IEnumerable<TElement> linqExpr = applyLINQ(wrapper); |
| 255 |
MeasureMaterializationToArray(linqExpr, iterationCount); |
| 256 |
} |
| 257 |
|
| 258 |
// =========== |
| 259 |
} |
| 260 |
} |