-
Notifications
You must be signed in to change notification settings - Fork 272
/
ArrayPoolTests.cs
179 lines (155 loc) · 5.63 KB
/
ArrayPoolTests.cs
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using BenchmarkDotNet.Attributes;
using MicroBenchmarks;
using System.Linq;
using System.Threading.Channels;
using System.Threading.Tasks;
namespace System.Buffers.Tests
{
[BenchmarkCategory(Categories.Libraries, Categories.NoWASM)]
[GenericTypeArguments(typeof(byte))] // value type
[GenericTypeArguments(typeof(object))] // reference type
public class RentReturnArrayPoolTests<T>
{
private readonly ArrayPool<T> _createdPool = ArrayPool<T>.Create();
private readonly T[][] _nestedArrays = new T[NestedDepth][];
private const int Iterations = 100_000;
private const int NestedDepth = 8;
[Params(4096)]
public int RentalSize;
[Params(false, true)]
public bool ManipulateArray { get; set; }
[Params(false, true)]
public bool Async { get; set; }
[Params(false, true)]
public bool UseSharedPool { get; set; }
private ArrayPool<T> Pool => UseSharedPool ? ArrayPool<T>.Shared : _createdPool;
private static void Clear(T[] arr) => arr.AsSpan().Clear();
private static T IterateAll(T[] arr)
{
T ret = default;
foreach (T item in arr)
{
ret = item;
}
return ret;
}
[Benchmark(OperationsPerInvoke = Iterations)]
public async Task SingleSerial()
{
ArrayPool<T> pool = Pool;
for (int i = 0; i < Iterations; i++)
{
T[] arr = pool.Rent(RentalSize);
if (ManipulateArray) Clear(arr);
if (Async) await Task.Yield();
if (ManipulateArray) IterateAll(arr);
pool.Return(arr);
}
}
[Benchmark(OperationsPerInvoke = Iterations)]
public async Task SingleParallel()
{
ArrayPool<T> pool = Pool;
await Task.WhenAll(
Enumerable
.Range(0, Environment.ProcessorCount)
.Select(_ =>
Task.Run(async delegate
{
for (int i = 0; i < Iterations; i++)
{
T[] arr = pool.Rent(RentalSize);
if (ManipulateArray) Clear(arr);
if (Async) await Task.Yield();
if (ManipulateArray) IterateAll(arr);
pool.Return(arr);
}
})));
}
[Benchmark(OperationsPerInvoke = Iterations)]
[BenchmarkCategory(Categories.NoInterpreter)]
public async Task MultipleSerial()
{
ArrayPool<T> pool = Pool;
for (int i = 0; i < Iterations; i++)
{
for (int j = 0; j < _nestedArrays.Length; j++)
{
_nestedArrays[j] = pool.Rent(RentalSize);
if (ManipulateArray) Clear(_nestedArrays[j]);
}
if (Async) await Task.Yield();
for (int j = _nestedArrays.Length - 1; j >= 0; j--)
{
if (ManipulateArray) IterateAll(_nestedArrays[j]);
pool.Return(_nestedArrays[j]);
}
}
}
[Benchmark(OperationsPerInvoke = Iterations)]
public async Task ProducerConsumer()
{
ArrayPool<T> pool = Pool;
Channel<T[]> buffers = Channel.CreateBounded<T[]>(1);
Task consumer = Task.Run(async delegate
{
ChannelReader<T[]> reader = buffers.Reader;
while (true)
{
ValueTask<bool> read = reader.WaitToReadAsync();
if (!(Async ? await read : read.AsTask().Result))
{
break;
}
while (reader.TryRead(out T[] buffer))
{
if (ManipulateArray) IterateAll(buffer);
pool.Return(buffer);
}
}
});
ChannelWriter<T[]> writer = buffers.Writer;
for (int i = 0; i < Iterations; i++)
{
T[] buffer = pool.Rent(RentalSize);
if (ManipulateArray) IterateAll(buffer);
ValueTask write = writer.WriteAsync(buffer);
if (Async)
{
await write;
}
else
{
write.AsTask().Wait();
}
}
writer.Complete();
await consumer;
}
}
[BenchmarkCategory(Categories.Libraries)]
[GenericTypeArguments(typeof(byte))] // value type
[GenericTypeArguments(typeof(object))] // reference type
public class NonStandardArrayPoolTests<T>
{
private readonly ArrayPool<T> _createdPool = ArrayPool<T>.Create();
private const int Iterations = 100_000;
[Params(64)]
public int RentalSize;
[Params(false, true)]
public bool UseSharedPool { get; set; }
public ArrayPool<T> Pool => UseSharedPool ? ArrayPool<T>.Shared : _createdPool;
[Benchmark(OperationsPerInvoke = Iterations)]
public void RentNoReturn()
{
ArrayPool<T> pool = Pool;
for (int i = 0; i < Iterations; i++)
{
pool.Rent(RentalSize);
}
}
}
}