Repository navigation
Expand file tree
/
Copy pathTracingCompletionBenchmarks.cs
More file actions
244 lines (210 loc) · 7.67 KB
/
Copy pathTracingCompletionBenchmarks.cs
File metadata and controls
244 lines (210 loc) · 7.67 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
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Columns;
using BenchmarkDotNet.Engines;
using BenchmarkDotNet.Order;
namespace RequestBatcher.Benchmarks.InMemory;
/// <summary>
/// Compares pending tracing completion bridges without running the request queue.
/// </summary>
[MemoryDiagnoser]
[RankColumn]
[Orderer(SummaryOrderPolicy.FastestToSlowest)]
[SimpleJob(
RunStrategy.Throughput,
launchCount: 2,
warmupCount: 4,
iterationCount: 12,
invocationCount: 8,
id: "TracingCompletion")]
public class TracingCompletionBenchmarks
{
private const int RequestCount = 1_024;
private const string CapturedAmbientValue = "captured";
private const string ChangedAmbientValue = "changed";
private readonly CompletionProbe _probe = new();
/// <summary>
/// Compares the existing asynchronous Task helper with a completion source.
/// </summary>
[Benchmark(Baseline = true, Description = "Async Task helper + TCS")]
public Task AsyncTaskHelperAsync() => RunAsync(StartWithTaskHelper);
/// <summary>
/// Compares an ordinary asynchronous ValueTask helper with a completion source.
/// </summary>
[Benchmark(Description = "Async ValueTask helper + TCS")]
public Task AsyncValueTaskHelperAsync() => RunAsync(StartWithValueTaskHelper);
/// <summary>
/// Compares an awaiter callback object that owns the completion source.
/// </summary>
[Benchmark(Description = "Awaiter callback object + TCS")]
public Task AwaiterCallbackAsync() => RunAsync(StartWithCallback);
private async Task RunAsync(Func<Task, Action<Task>, Task> start)
{
_probe.Reset();
var sources = new TaskCompletionSource[RequestCount];
var wrappers = new Task[RequestCount];
for (var index = 0; index < RequestCount; index++)
{
var source = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);
sources[index] = source;
_probe.Ambient.Value = CapturedAmbientValue;
wrappers[index] = start(source.Task, _probe.Callback);
_probe.Ambient.Value = ChangedAmbientValue;
}
if (sources.Any(static source => source.Task.IsCompleted))
{
throw new InvalidOperationException("A source completed before every wrapper was registered.");
}
for (var index = 0; index < sources.Length; index++)
{
sources[index].TrySetResult();
}
await Task.WhenAll(wrappers).ConfigureAwait(false);
_probe.Validate(RequestCount);
_probe.Ambient.Value = null;
}
private static Task StartWithTaskHelper(Task source, Action<Task> completionAction)
{
var completion = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);
_ = CompleteWithTaskAsync(source, completion, completionAction);
return completion.Task;
}
private static async Task CompleteWithTaskAsync(
Task source,
TaskCompletionSource completion,
Action<Task> completionAction)
{
await source.ConfigureAwait(ConfigureAwaitOptions.SuppressThrowing);
completionAction(source);
TransferCompletion(source, completion);
}
private static Task StartWithValueTaskHelper(Task source, Action<Task> completionAction)
{
var completion = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);
_ = CompleteWithValueTaskAsync(source, completion, completionAction);
return completion.Task;
}
private static async ValueTask CompleteWithValueTaskAsync(
Task source,
TaskCompletionSource completion,
Action<Task> completionAction)
{
await source.ConfigureAwait(ConfigureAwaitOptions.SuppressThrowing);
completionAction(source);
TransferCompletion(source, completion);
}
private static Task StartWithCallback(Task source, Action<Task> completionAction)
{
var completion = new CallbackCompletion(source, completionAction);
completion.Register();
return completion.Task;
}
private static void TransferCompletion(Task source, TaskCompletionSource completion)
{
if (source.IsFaulted)
{
completion.TrySetException(source.Exception!.InnerExceptions);
}
else if (source.IsCanceled)
{
completion.TrySetCanceled(GetCancellationToken(source));
}
else
{
completion.TrySetResult();
}
}
private static CancellationToken GetCancellationToken(Task task)
{
try
{
task.GetAwaiter().GetResult();
}
catch (OperationCanceledException exception)
{
return exception.CancellationToken;
}
return default;
}
private sealed class CallbackCompletion :
TaskCompletionSource
{
private static readonly ContextCallback _completeWithContextCallback = static state =>
((CallbackCompletion)state!).CompleteUnderCapturedContext();
private readonly ExecutionContext? _capturedContext;
private readonly Action _completion;
private readonly Task _source;
private readonly Action<Task> _completionAction;
public CallbackCompletion(Task source, Action<Task> completionAction)
: base(TaskCreationOptions.RunContinuationsAsynchronously)
{
_source = source;
_completionAction = completionAction;
_capturedContext = ExecutionContext.Capture();
_completion = Complete;
}
public void Register()
{
_source.ConfigureAwait(ConfigureAwaitOptions.SuppressThrowing)
.GetAwaiter()
.UnsafeOnCompleted(_completion);
}
private void Complete()
{
if (_capturedContext is null)
{
CompleteUnderCapturedContext();
return;
}
ExecutionContext.Run(_capturedContext, _completeWithContextCallback, this);
}
private void CompleteUnderCapturedContext()
{
var source = _source;
_completionAction(source);
TransferCompletion(source, this);
}
}
private sealed class CompletionProbe
{
private int _callbackCount;
private string? _error;
public CompletionProbe()
{
Callback = OnCompleted;
}
public AsyncLocal<string?> Ambient { get; } = new();
public Action<Task> Callback { get; }
public void Reset()
{
_callbackCount = 0;
_error = null;
}
public void Validate(int expectedCount)
{
if (_error is not null)
{
throw new InvalidOperationException(_error);
}
if (_callbackCount != expectedCount)
{
throw new InvalidOperationException(
$"Expected {expectedCount} completion callbacks, observed {_callbackCount}.");
}
}
private void OnCompleted(Task source)
{
if (!source.IsCompletedSuccessfully)
{
Interlocked.CompareExchange(ref _error, "The source task did not complete successfully.", null);
}
else if (Ambient.Value != CapturedAmbientValue)
{
Interlocked.CompareExchange(
ref _error,
$"Expected ambient value '{CapturedAmbientValue}', observed '{Ambient.Value ?? "<null>"}'.",
null);
}
Interlocked.Increment(ref _callbackCount);
}
}
}