Repository navigation
Expand file tree
/
Copy pathExplicitCompletionBenchmarks.cs
More file actions
449 lines (375 loc) · 16.2 KB
/
Copy pathExplicitCompletionBenchmarks.cs
File metadata and controls
449 lines (375 loc) · 16.2 KB
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Columns;
using BenchmarkDotNet.Engines;
using BenchmarkDotNet.Order;
using Microsoft.Extensions.DependencyInjection;
namespace RequestBatcher.Benchmarks.InMemory;
/// <summary>
/// Measures shared completion through the public dependency-injection request batcher.
/// </summary>
[MemoryDiagnoser]
[RankColumn]
[Orderer(SummaryOrderPolicy.FastestToSlowest)]
[SimpleJob(
RunStrategy.Throughput,
launchCount: 2,
warmupCount: 5,
iterationCount: 12,
id: "ExplicitCompletion")]
[IterationTime(300)]
public class ExplicitCompletionBenchmarks
{
private const int BatchSize = 32;
private const int MaxConcurrency = 8;
private const int PartitionCount = 4;
private const int MaxRequestCount = 1_000;
private const int BlockerRequestId = -1;
private int[] _requests = [];
private BatcherFixture<SuccessHandler>? _success;
private BatcherFixture<FailureHandler>? _failure;
private BatcherFixture<PartialCancellationHandler>? _partialCancellation;
private BatcherFixture<CapacityHandler>? _capacity;
private BatcherFixture<SuccessHandler>? _mixed;
[Params(10, 1_000)]
public int GroupSize { get; set; }
private BatcherFixture<SuccessHandler> Success =>
_success ?? throw new InvalidOperationException("The success batcher has not been initialized.");
private BatcherFixture<FailureHandler> Failure =>
_failure ?? throw new InvalidOperationException("The failure batcher has not been initialized.");
private BatcherFixture<PartialCancellationHandler> PartialCancellation =>
_partialCancellation ?? throw new InvalidOperationException(
"The partial cancellation batcher has not been initialized.");
private BatcherFixture<CapacityHandler> Capacity =>
_capacity ?? throw new InvalidOperationException("The capacity batcher has not been initialized.");
private BatcherFixture<SuccessHandler> Mixed =>
_mixed ?? throw new InvalidOperationException("The mixed batcher has not been initialized.");
[GlobalSetup]
public void GlobalSetup()
{
_requests = Enumerable.Range(0, GroupSize).ToArray();
_success = CreateBatcherFixture<SuccessHandler>(ConfigureStandard);
_failure = CreateBatcherFixture<FailureHandler>(ConfigureStandard);
_partialCancellation = CreateBatcherFixture<PartialCancellationHandler>(ConfigurePartialCancellation);
_capacity = CreateBatcherFixture<CapacityHandler>(ConfigureCapacity);
_mixed = CreateBatcherFixture<SuccessHandler>(ConfigureStandard);
}
[Benchmark(Description = "Explicit group success")]
public async Task ExplicitGroupSuccessAsync()
{
Success.Handler.BeginWave();
var operation = Success.Batcher.ExecuteAsync(_requests);
var waiter = ObserveSuccessAsync(operation);
Success.Handler.ReleaseWave();
await waiter.ConfigureAwait(false);
Success.Handler.ValidateProcessed(GroupSize, expectedCount: 1);
}
[Benchmark(Description = "Explicit group observed handler failure")]
public async Task ExplicitGroupFailureAsync()
{
Failure.Handler.BeginWave();
var operation = Failure.Batcher.ExecuteAsync(_requests);
var waiter = ObserveExpectedFailureAsync(operation, Failure.Handler.Failure);
Failure.Handler.ReleaseWave();
await waiter.ConfigureAwait(false);
Failure.Handler.ValidateProcessed(GroupSize, expectedCount: 1);
}
[Benchmark(Description = "Explicit group partial caller cancellation")]
public async Task ExplicitGroupPartialCancellationAsync()
{
PartialCancellation.Handler.BeginWave();
using var cancellation = new CancellationTokenSource();
var operation = PartialCancellation.Batcher.ExecuteAsync(_requests, cancellation.Token);
await PartialCancellation.Handler.FirstHandlerStarted.ConfigureAwait(false);
var canceled = ObserveExpectedCancellationAsync(operation, cancellation.Token);
cancellation.Cancel();
PartialCancellation.Handler.ReleaseFirstHandler();
await canceled.ConfigureAwait(false);
var drain = PartialCancellation.Batcher.ExecuteAsync(BlockerRequestId);
var drainWaiter = ObserveSuccessAsync(drain);
await drainWaiter.ConfigureAwait(false);
PartialCancellation.Handler.ValidatePartialProcessing(GroupSize);
}
[Benchmark(Description = "Explicit groups with capacity waiting")]
public async Task ExplicitGroupsCapacityWaitAsync()
{
Capacity.Handler.BeginWave();
var blocker = Capacity.Batcher.ExecuteAsync(BlockerRequestId);
await Capacity.Handler.BlockerStarted.ConfigureAwait(false);
var firstGroup = Capacity.Batcher.ExecuteAsync(_requests);
var secondGroup = Capacity.Batcher.ExecuteAsync(_requests);
if (secondGroup.IsCompleted)
{
throw new InvalidOperationException("The second group did not wait for queue capacity.");
}
var all = Task.WhenAll(blocker, firstGroup, secondGroup);
var waiter = ObserveSuccessAsync(all);
Capacity.Handler.ReleaseBlocker();
await waiter.ConfigureAwait(false);
Capacity.Handler.ValidateProcessed(GroupSize, expectedCount: 2);
}
[Benchmark(Description = "Mixed single requests and one explicit group")]
public async Task MixedSinglesAndExplicitGroupAsync()
{
Mixed.Handler.BeginWave();
var submissions = new Task[GroupSize + 1];
for (var index = 0; index < GroupSize; index++)
{
submissions[index] = Mixed.Batcher.ExecuteAsync(_requests[index]);
}
submissions[GroupSize] = Mixed.Batcher.ExecuteAsync(_requests);
var all = Task.WhenAll(submissions);
var waiter = ObserveSuccessAsync(all);
Mixed.Handler.ReleaseWave();
await waiter.ConfigureAwait(false);
Mixed.Handler.ValidateProcessed(GroupSize, expectedCount: 2);
}
[GlobalCleanup]
public async Task GlobalCleanupAsync()
{
await DisposeBatcherFixtureAsync(_success).ConfigureAwait(false);
await DisposeBatcherFixtureAsync(_failure).ConfigureAwait(false);
await DisposeBatcherFixtureAsync(_partialCancellation).ConfigureAwait(false);
await DisposeBatcherFixtureAsync(_capacity).ConfigureAwait(false);
await DisposeBatcherFixtureAsync(_mixed).ConfigureAwait(false);
_success = null;
_failure = null;
_partialCancellation = null;
_capacity = null;
_mixed = null;
}
private static void ConfigureStandard(RequestBatchOptions<int> options, int groupSize)
{
options.BatchSize = BatchSize;
options.MaxConcurrency = MaxConcurrency;
options.MaxPendingRequests = groupSize * 2;
options.UsePartitionKey(static request => request % 64, PartitionCount);
}
private static void ConfigurePartialCancellation(RequestBatchOptions<int> options, int groupSize)
{
options.BatchSize = 1;
options.MaxConcurrency = 1;
options.MaxPendingRequests = groupSize * 2;
options.UsePartitionKey(static _ => 0, PartitionCount);
}
private static void ConfigureCapacity(RequestBatchOptions<int> options, int groupSize)
{
options.BatchSize = BatchSize;
options.MaxConcurrency = 1;
options.MaxPendingRequests = groupSize;
options.UsePartitionKey(static _ => 0, PartitionCount);
}
private BatcherFixture<THandler> CreateBatcherFixture<THandler>(Action<RequestBatchOptions<int>, int> configure)
where THandler : class, IRequestBatchHandler<int>
{
var services = new ServiceCollection();
services.AddRequestBatcher<int, THandler>(
ServiceLifetime.Singleton,
options => configure(options, GroupSize));
var provider = services.BuildServiceProvider(new ServiceProviderOptions
{
ValidateOnBuild = true,
ValidateScopes = true,
});
var handler = provider.GetRequiredService<IRequestBatchHandler<int>>() as THandler ??
throw new InvalidOperationException("The benchmark handler was not resolved through its public interface.");
var batcher = provider.GetRequiredService<IRequestBatcher<int>>();
return new BatcherFixture<THandler>(provider, batcher, handler);
}
private static async Task ObserveSuccessAsync(Task operation) =>
await operation.ConfigureAwait(false);
private static async Task ObserveExpectedFailureAsync(Task operation, Exception expected)
{
try
{
await operation.ConfigureAwait(false);
}
catch (BenchmarkHandlerException exception) when (ReferenceEquals(exception, expected))
{
return;
}
throw new InvalidOperationException("The explicit group did not observe the original handler exception.");
}
private static async Task ObserveExpectedCancellationAsync(Task operation, CancellationToken expectedToken)
{
try
{
await operation.ConfigureAwait(false);
}
catch (OperationCanceledException exception) when (exception.CancellationToken == expectedToken)
{
if (!operation.IsCanceled)
{
throw new InvalidOperationException("The cancellation task did not remain canceled.");
}
return;
}
throw new InvalidOperationException("The explicit group did not observe caller cancellation.");
}
private static async ValueTask DisposeBatcherFixtureAsync<THandler>(BatcherFixture<THandler>? fixture)
where THandler : class, IRequestBatchHandler<int>
{
if (fixture is not null)
{
await fixture.DisposeAsync().ConfigureAwait(false);
}
}
private sealed class BatcherFixture<THandler>(
ServiceProvider provider,
IRequestBatcher<int> batcher,
THandler handler) : IAsyncDisposable
where THandler : class, IRequestBatchHandler<int>
{
public ServiceProvider Provider { get; } = provider;
public IRequestBatcher<int> Batcher { get; } = batcher;
public THandler Handler { get; } = handler;
public ValueTask DisposeAsync() => Provider.DisposeAsync();
}
private abstract class CountingHandler : IRequestBatchHandler<int>
{
private readonly int[] _counts = new int[MaxRequestCount];
private TaskCompletionSource _release = CreateSource();
public virtual void BeginWave()
{
Array.Clear(_counts);
Volatile.Write(ref _release, CreateSource());
}
public void ReleaseWave() => Volatile.Read(ref _release).TrySetResult();
public virtual async ValueTask HandleAsync(
IReadOnlyList<int> requests,
CancellationToken cancellationToken = default)
{
await Volatile.Read(ref _release).Task.ConfigureAwait(false);
foreach (var request in requests)
{
if ((uint)request < MaxRequestCount)
{
Interlocked.Increment(ref _counts[request]);
}
}
OnBatchHandled();
}
public void ValidateProcessed(int requestCount, int expectedCount)
{
var total = 0;
for (var request = 0; request < requestCount; request++)
{
var count = Volatile.Read(ref _counts[request]);
if (count != expectedCount)
{
throw new InvalidOperationException(
$"Request {request} was processed {count} times; expected {expectedCount}.");
}
total += count;
}
var expectedTotal = requestCount * expectedCount;
if (total != expectedTotal)
{
throw new InvalidOperationException(
$"The handler processed {total} requests; expected {expectedTotal}.");
}
}
protected virtual void OnBatchHandled()
{
}
protected void Increment(int request) => Interlocked.Increment(ref _counts[request]);
protected int CountAt(int request) => Volatile.Read(ref _counts[request]);
private static TaskCompletionSource CreateSource() =>
new(TaskCreationOptions.RunContinuationsAsynchronously);
}
private sealed class SuccessHandler : CountingHandler;
private sealed class FailureHandler : CountingHandler
{
public BenchmarkHandlerException Failure { get; } = new();
protected override void OnBatchHandled() => throw Failure;
}
private sealed class PartialCancellationHandler : CountingHandler
{
private readonly object _gateLock = new();
private TaskCompletionSource _firstHandlerStarted = CreateSource();
private TaskCompletionSource _releaseFirstHandler = CreateSource();
public Task FirstHandlerStarted => Volatile.Read(ref _firstHandlerStarted).Task;
public override void BeginWave()
{
base.BeginWave();
lock (_gateLock)
{
_firstHandlerStarted = CreateSource();
_releaseFirstHandler = CreateSource();
}
}
public void ReleaseFirstHandler() => Volatile.Read(ref _releaseFirstHandler).TrySetResult();
public void ValidatePartialProcessing(int requestCount)
{
if (CountAt(0) != 1)
{
throw new InvalidOperationException("The first request was not processed exactly once.");
}
for (var request = 1; request < requestCount; request++)
{
if (CountAt(request) != 0)
{
throw new InvalidOperationException(
$"Canceled request {request} was processed unexpectedly.");
}
}
}
public override async ValueTask HandleAsync(
IReadOnlyList<int> requests,
CancellationToken cancellationToken = default)
{
if (requests.Count == 1 && requests[0] == BlockerRequestId)
{
return;
}
if (requests.Count != 1 || requests[0] != 0)
{
foreach (var request in requests)
{
if ((uint)request < MaxRequestCount)
{
Increment(request);
}
}
return;
}
Volatile.Read(ref _firstHandlerStarted).TrySetResult();
await Volatile.Read(ref _releaseFirstHandler).Task.ConfigureAwait(false);
Increment(0);
}
private static TaskCompletionSource CreateSource() =>
new(TaskCreationOptions.RunContinuationsAsynchronously);
}
private sealed class CapacityHandler : CountingHandler
{
private readonly object _gateLock = new();
private TaskCompletionSource _blockerStarted = CreateSource();
private TaskCompletionSource _releaseBlocker = CreateSource();
public Task BlockerStarted => Volatile.Read(ref _blockerStarted).Task;
public override void BeginWave()
{
base.BeginWave();
ReleaseWave();
lock (_gateLock)
{
_blockerStarted = CreateSource();
_releaseBlocker = CreateSource();
}
}
public void ReleaseBlocker() => Volatile.Read(ref _releaseBlocker).TrySetResult();
public override async ValueTask HandleAsync(
IReadOnlyList<int> requests,
CancellationToken cancellationToken = default)
{
if (requests.Count == 1 && requests[0] == BlockerRequestId)
{
Volatile.Read(ref _blockerStarted).TrySetResult();
await Volatile.Read(ref _releaseBlocker).Task.ConfigureAwait(false);
return;
}
await base.HandleAsync(requests, cancellationToken).ConfigureAwait(false);
}
private static TaskCompletionSource CreateSource() =>
new(TaskCreationOptions.RunContinuationsAsynchronously);
}
private sealed class BenchmarkHandlerException : Exception;
}