diff --git a/Recyclable.Collections.Benchmarks/BenchmarkBase.cs b/Recyclable.Collections.Benchmarks/BenchmarkBase.cs index 2d078c6..41925f4 100644 --- a/Recyclable.Collections.Benchmarks/BenchmarkBase.cs +++ b/Recyclable.Collections.Benchmarks/BenchmarkBase.cs @@ -1,4 +1,6 @@ -using Recyclable.Collections.Benchmarks.POC; +using BenchmarkDotNet.Attributes; +using Recyclable.Collections.Benchmarks.Core; +using Recyclable.Collections.Benchmarks.POC; namespace Recyclable.Collections.Benchmarks { @@ -25,9 +27,26 @@ namespace Recyclable.Collections.Benchmarks } } - protected static void DoNothing(T obj) + [GlobalSetup] + public virtual void Setup() { - _ = obj; + Console.WriteLine("******* GLOBAL SETUP *******"); + _testObjects = DataGenerator.EnumerateTestObjects(TestObjectCount); + Console.WriteLine("******* TEST DATA CREATED *******"); + } + + [GlobalCleanup] + public virtual void Cleanup() + { + Console.WriteLine("******* GLOBAL CLEANUP *******"); + // Enable this if item type is changed to reference type + //if (_testObjects != null) + //{ + // Array.Fill(_testObjects, 0); + //} + + _testObjects = default; + GC.Collect(GC.MaxGeneration, GCCollectionMode.Forced, true, true); } } diff --git a/Recyclable.Collections.Benchmarks/Benchmarks/RecyclableCollections.Benchmarks.cs b/Recyclable.Collections.Benchmarks/Benchmarks/RecyclableCollections.Benchmarks.cs index 1be4904..2d501c6 100644 --- a/Recyclable.Collections.Benchmarks/Benchmarks/RecyclableCollections.Benchmarks.cs +++ b/Recyclable.Collections.Benchmarks/Benchmarks/RecyclableCollections.Benchmarks.cs @@ -13,11 +13,9 @@ namespace Recyclable.Collections.Benchmarks public PooledList TestObjectsAsPooledList => _testPooledList ?? throw new NullReferenceException("Something is wrong and the field is not initialized"); - [GlobalSetup] - public void Setup() + public override void Setup() { - Console.WriteLine("******* GLOBAL SETUP RAISED *******"); - _testObjects = EnumerateTestObjects().ToArray(); + base.Setup(); _testRecyclableArrayList = new(TestObjects, initialCapacity: TestObjectCount); _testRecyclableList = new(TestObjects, BlockSize, expectedItemsCount: TestObjectCount); _testArray = TestObjects.ToArray(); @@ -27,18 +25,8 @@ namespace Recyclable.Collections.Benchmarks _testPooledList = new PooledList(TestObjects, ClearMode.Always); } - private IEnumerable EnumerateTestObjects() + public override void Cleanup() { - for (long i = 0; i < TestObjectCount; i++) - { - yield return i; - } - } - - [GlobalCleanup] - public void Cleanup() - { - Console.WriteLine("******* GLOBAL CLEANUP *******"); _testRecyclableArrayList?.Dispose(); _testRecyclableList?.Dispose(); _testPooledList?.Dispose(); @@ -46,7 +34,7 @@ namespace Recyclable.Collections.Benchmarks _testObjectsAsRecyclableArrayList = default; _testObjectsAsRecyclableList = default; _testObjectsAsList = default; - _testObjects = default; + base.Cleanup(); } } } diff --git a/Recyclable.Collections.Benchmarks/Core/DataGenerator.cs b/Recyclable.Collections.Benchmarks/Core/DataGenerator.cs new file mode 100644 index 0000000..b4d8321 --- /dev/null +++ b/Recyclable.Collections.Benchmarks/Core/DataGenerator.cs @@ -0,0 +1,20 @@ +namespace Recyclable.Collections.Benchmarks.Core +{ + public class DataGenerator + { + public static long[] EnumerateTestObjects(long testObjectCount) + { + var result = new long[testObjectCount]; + Console.WriteLine("******* TEST DATA ARRAY CREATED *******"); + + Span resultSpan = result; + for (int i = 0; i < testObjectCount; i++) + { + resultSpan[i] = i; + } + + return result; + } + + } +} \ No newline at end of file diff --git a/Recyclable.Collections.Benchmarks/POC/PocBenchmarkBase.cs b/Recyclable.Collections.Benchmarks/POC/PocBenchmarkBase.cs index 88bc0cc..1a56203 100644 --- a/Recyclable.Collections.Benchmarks/POC/PocBenchmarkBase.cs +++ b/Recyclable.Collections.Benchmarks/POC/PocBenchmarkBase.cs @@ -1,4 +1,6 @@ -namespace Recyclable.Collections.Benchmarks.POC +using System.Runtime.CompilerServices; + +namespace Recyclable.Collections.Benchmarks.POC { public class PocBenchmarkBase { @@ -21,5 +23,11 @@ > 0 and <= 10 => (int)TestObjectCount, _ => TestObjectCount / Divider > 0 ? checked((int)(TestObjectCount / Divider)) : checked((int)TestObjectCount) }; + + [MethodImpl(MethodImplOptions.NoInlining)] + protected static void DoNothing(in T item) + { + _ = item; + } } }