|
| 1 | +using BenchmarkDotNet.Attributes; |
| 2 | +using BenchmarkDotNet.Configs; |
| 3 | +using BenchmarkDotNet.Diagnosers; |
| 4 | +using BenchmarkDotNet.Helpers; |
| 5 | +using BenchmarkDotNet.Running; |
| 6 | +using BenchmarkDotNet.Tests.XUnit; |
| 7 | +using System.Buffers.Tests; |
| 8 | +using System.Linq; |
| 9 | +using System.Threading.Tasks; |
| 10 | +using Xunit; |
| 11 | + |
| 12 | +namespace BenchmarkDotNet.Tests |
| 13 | +{ |
| 14 | + public class ArtifactFileNameHelperTests |
| 15 | + { |
| 16 | + [FactWindowsOnly(nonWindowsSkipReason: "ETW Sessions can be created only on Windows")] |
| 17 | + public void OnWindowsWeMustAlwaysUseOldLongPathsLimitForSessionFiles() |
| 18 | + { |
| 19 | + var config = DefaultConfig.Instance |
| 20 | + .WithArtifactsPath(@"C:\Projects\performance\artifacts\bin\MicroBenchmarks\Release\netcoreapp5.0\BenchmarkDotNet.Artifacts"); |
| 21 | + |
| 22 | + var benchmarkCase = BenchmarkConverter.TypeToBenchmarks(typeof(RentReturnArrayPoolTests<byte>), config).BenchmarksCases.First(); |
| 23 | + |
| 24 | + var parameters = new DiagnoserActionParameters( |
| 25 | + process: null, |
| 26 | + benchmarkCase: benchmarkCase, |
| 27 | + new BenchmarkId(0, benchmarkCase)); |
| 28 | + |
| 29 | + foreach (string fileExtension in new[] { "etl", "kernel.etl", "userheap.etl" }) |
| 30 | + { |
| 31 | + var traceFilePath = ArtifactFileNameHelper.GetTraceFilePath(parameters, new System.DateTime(2020, 10, 1), fileExtension); |
| 32 | + |
| 33 | + Assert.InRange(actual: traceFilePath.Length, low: 0, high: 260); |
| 34 | + } |
| 35 | + } |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +namespace System.Buffers.Tests |
| 40 | +{ |
| 41 | + [GenericTypeArguments(typeof(byte))] // value type |
| 42 | + [GenericTypeArguments(typeof(object))] // reference type |
| 43 | + public class RentReturnArrayPoolTests<T> |
| 44 | + { |
| 45 | + private readonly ArrayPool<T> _createdPool = ArrayPool<T>.Create(); |
| 46 | + private const int Iterations = 100_000; |
| 47 | + [Params(4096)] |
| 48 | + public int RentalSize; |
| 49 | + |
| 50 | + [Params(false, true)] |
| 51 | + public bool ManipulateArray { get; set; } |
| 52 | + |
| 53 | + [Params(false, true)] |
| 54 | + public bool Async { get; set; } |
| 55 | + |
| 56 | + [Params(false, true)] |
| 57 | + public bool UseSharedPool { get; set; } |
| 58 | + |
| 59 | + private ArrayPool<T> Pool => UseSharedPool ? ArrayPool<T>.Shared : _createdPool; |
| 60 | + |
| 61 | + private static void Clear(T[] arr) => arr.AsSpan().Clear(); |
| 62 | + |
| 63 | + private static T IterateAll(T[] arr) |
| 64 | + { |
| 65 | + T ret = default; |
| 66 | + foreach (T item in arr) |
| 67 | + { |
| 68 | + ret = item; |
| 69 | + } |
| 70 | + return ret; |
| 71 | + } |
| 72 | + |
| 73 | + [Benchmark(OperationsPerInvoke = Iterations)] |
| 74 | + public async Task SingleSerial() |
| 75 | + { |
| 76 | + ArrayPool<T> pool = Pool; |
| 77 | + for (int i = 0; i < Iterations; i++) |
| 78 | + { |
| 79 | + T[] arr = pool.Rent(RentalSize); |
| 80 | + if (ManipulateArray) Clear(arr); |
| 81 | + if (Async) await Task.Yield(); |
| 82 | + if (ManipulateArray) IterateAll(arr); |
| 83 | + pool.Return(arr); |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | +} |
0 commit comments