Create benchmarks updated. Global benchmark setup & cleanup added.

This commit is contained in:
mlemanczyk committed 2023-01-16 22:00:45 +01:00
1 parent 4bfb40007a
commit cc364c5fd7
7 files changed
+38 -24

No files matched your search

@@ -1,10 +1,12 @@
namespace Recyclable.Collections.Benchmarks using BenchmarkDotNet.Attributes;
namespace Recyclable.Collections.Benchmarks
{ {
public class BenchmarkBase public class BenchmarkBase
{ {
//[Params(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 128, 256, 512, 1024, 2048, 4096, 8192, 1_000_000)] [Params(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 128, 256, 512, 1024, 2048, 4096, 8192, 1_000_000)]
public const int TestObjectCount = 1_000_000; public int TestObjectCount;
public static int BlockSize = TestObjectCount switch public int BlockSize => TestObjectCount switch
{ {
0 => 1, 0 => 1,
> 0 and <= 10 => TestObjectCount, > 0 and <= 10 => TestObjectCount,
@@ -14,12 +16,12 @@
protected object[]? _testObjects; protected object[]? _testObjects;
protected object[] TestObjects => _testObjects ??= Enumerable.Range(1, TestObjectCount).Select(x => new object()).ToArray(); protected object[] TestObjects => _testObjects ??= Enumerable.Range(1, TestObjectCount).Select(x => new object()).ToArray();
private List<object>? _testObjectsAsList; protected List<object>? _testObjectsAsList;
protected List<object> TestObjectsAsList => _testObjectsAsList ??= TestObjects.ToList(); protected List<object> TestObjectsAsList => _testObjectsAsList ??= TestObjects.ToList();
private RecyclableArrayList<object>? _testObjectsAsRecyclableArrayList; protected RecyclableArrayList<object>? _testObjectsAsRecyclableArrayList;
protected RecyclableArrayList<object> TestObjectsAsRecyclableArrayList => _testObjectsAsRecyclableArrayList ??= TestObjects.ToRecyclableArrayList(); protected RecyclableArrayList<object> TestObjectsAsRecyclableArrayList => _testObjectsAsRecyclableArrayList ??= TestObjects.ToRecyclableArrayList();
protected IEnumerable<object> TestObjectsAsIEnumerable = Enumerable.Range(1, TestObjectCount).Select(static x => new object()); protected IEnumerable<object> TestObjectsAsIEnumerable => Enumerable.Range(1, TestObjectCount).Select(static x => new object());
protected static void DoNothing<T>(T obj) protected static void DoNothing<T>(T obj)
{ {
@@ -5,16 +5,16 @@ namespace Recyclable.Collections.Benchmarks
[MemoryDiagnoser] [MemoryDiagnoser]
public partial class RecyclableCollectionsBenchmarks : BenchmarkBase public partial class RecyclableCollectionsBenchmarks : BenchmarkBase
{ {
private readonly object[] _testArray; private object[] _testArray;
//[Benchmark(Baseline = false)] [Benchmark(Baseline = true)]
public void Array_Create_WithCapacity() public void Array_Create_WithCapacity()
{ {
var data = new object[TestObjectCount]; var data = new object[TestObjectCount];
DoNothing(data); DoNothing(data);
} }
[Benchmark(Baseline = true)] //[Benchmark(Baseline = true)]
public void Array_SetItem() public void Array_SetItem()
{ {
var data = TestObjects; var data = TestObjects;
@@ -4,9 +4,7 @@ namespace Recyclable.Collections.Benchmarks
{ {
public partial class RecyclableCollectionsBenchmarks : BenchmarkBase public partial class RecyclableCollectionsBenchmarks : BenchmarkBase
{ {
private readonly List<object> _testList; //[Benchmark(Baseline = true)]
//[Benchmark(Baseline = false)]
public void List_Create() public void List_Create()
{ {
var list = new List<object>(); var list = new List<object>();
@@ -20,7 +18,7 @@ namespace Recyclable.Collections.Benchmarks
DoNothing(list); DoNothing(list);
} }
//[Benchmark] [Benchmark]
public void List_Create_WithCapacity() public void List_Create_WithCapacity()
{ {
var list = new List<object>(TestObjectCount); var list = new List<object>(TestObjectCount);
@@ -75,7 +73,7 @@ namespace Recyclable.Collections.Benchmarks
list.AddRange(data); list.AddRange(data);
} }
[Benchmark] //[Benchmark]
public void List_AddRangeWhenSourceIsIEnumerable() public void List_AddRangeWhenSourceIsIEnumerable()
{ {
var data = TestObjectsAsIEnumerable; var data = TestObjectsAsIEnumerable;
@@ -117,7 +115,7 @@ namespace Recyclable.Collections.Benchmarks
//[Benchmark] //[Benchmark]
public void List_Count() public void List_Count()
{ {
var data = _testList; var data = TestObjectsAsList;
DoNothing(data.Count); DoNothing(data.Count);
} }
} }
@@ -4,23 +4,32 @@ namespace Recyclable.Collections.Benchmarks
{ {
public partial class RecyclableCollectionsBenchmarks : BenchmarkBase public partial class RecyclableCollectionsBenchmarks : BenchmarkBase
{ {
private readonly RecyclableArrayList<object> _testRecyclableArrayList; private RecyclableArrayList<object> _testRecyclableArrayList;
public RecyclableCollectionsBenchmarks() [GlobalSetup]
public void Setup()
{ {
_testRecyclableArrayList = new(TestObjects, TestObjectCount); _testRecyclableArrayList = new(TestObjects, TestObjectCount);
_testRecyclableList = new(TestObjects, TestObjectCount); _testRecyclableList = new(TestObjects, TestObjectCount);
_testArray = TestObjects.ToArray(); _testArray = TestObjects.ToArray();
_testList = new(TestObjects);
_ = TestObjectsAsList; _ = TestObjectsAsList;
_ = TestObjectsAsRecyclableArrayList; _ = TestObjectsAsRecyclableArrayList;
_ = TestObjectsAsIEnumerable;
} }
~RecyclableCollectionsBenchmarks()
[GlobalCleanup]
public void Cleanup()
{ {
_testRecyclableArrayList?.Dispose(); _testRecyclableArrayList?.Dispose();
_testRecyclableArrayList = default;
_testRecyclableList?.Dispose(); _testRecyclableList?.Dispose();
_testRecyclableList = default;
TestObjectsAsRecyclableArrayList?.Dispose(); TestObjectsAsRecyclableArrayList?.Dispose();
_testObjectsAsRecyclableArrayList = default;
_testArray = default;
_testObjectsAsList = default;
_testObjects = default;
} }
//[Benchmark] //[Benchmark]
@@ -37,7 +46,7 @@ namespace Recyclable.Collections.Benchmarks
DoNothing(list); DoNothing(list);
} }
//[Benchmark] [Benchmark]
public void RecyclableArrayList_Create_WithCapacity() public void RecyclableArrayList_Create_WithCapacity()
{ {
using var list = new RecyclableArrayList<object>(TestObjectCount); using var list = new RecyclableArrayList<object>(TestObjectCount);
@@ -92,7 +101,7 @@ namespace Recyclable.Collections.Benchmarks
list.AddRange(data); list.AddRange(data);
} }
[Benchmark] //[Benchmark]
public void RecyclableArrayList_AddRangeWhenSourceIsIEnumerable() public void RecyclableArrayList_AddRangeWhenSourceIsIEnumerable()
{ {
var data = TestObjectsAsIEnumerable; var data = TestObjectsAsIEnumerable;
@@ -142,7 +151,7 @@ namespace Recyclable.Collections.Benchmarks
//[Benchmark] //[Benchmark]
public void RecyclableArrayList_Count() public void RecyclableArrayList_Count()
{ {
var data = _testRecyclableArrayList; var data = TestObjectsAsRecyclableArrayList;
DoNothing(data.Count); DoNothing(data.Count);
} }
} }
@@ -4,7 +4,7 @@ namespace Recyclable.Collections.Benchmarks
{ {
public partial class RecyclableCollectionsBenchmarks : BenchmarkBase public partial class RecyclableCollectionsBenchmarks : BenchmarkBase
{ {
private readonly RecyclableList<object> _testRecyclableList; private RecyclableList<object> _testRecyclableList;
//[Benchmark] //[Benchmark]
public void RecyclableList_Create() public void RecyclableList_Create()
Binary file not shown.

Before

Width:  |  Height:  |  Size: 22 KiB

After

Width:  |  Height:  |  Size: 1.1 MiB

+5
View File
@@ -70,6 +70,11 @@ namespace Recyclable.Collections
private static RecyclableArrayList<T[]> SetupMemoryBlocks(RecyclableList<T> owner, int blockSize, long? totalItemsCount) private static RecyclableArrayList<T[]> SetupMemoryBlocks(RecyclableList<T> owner, int blockSize, long? totalItemsCount)
{ {
if (blockSize <= 0)
{
return new();
}
totalItemsCount ??= 8 * blockSize; totalItemsCount ??= 8 * blockSize;
int additionalArray = totalItemsCount % blockSize > 0 ? 1 : 0; int additionalArray = totalItemsCount % blockSize > 0 ? 1 : 0;
int memoryBlockCount = (int)(totalItemsCount.Value / blockSize).LimitTo(int.MaxValue) + additionalArray; int memoryBlockCount = (int)(totalItemsCount.Value / blockSize).LimitTo(int.MaxValue) + additionalArray;