Repository navigation
Expand file tree
/
Copy pathNamedTracingHotPathBenchmarks.cs
More file actions
314 lines (278 loc) · 11.2 KB
/
Copy pathNamedTracingHotPathBenchmarks.cs
File metadata and controls
314 lines (278 loc) · 11.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
using System.Diagnostics;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Engines;
using Microsoft.Extensions.DependencyInjection;
namespace RequestBatcher.Benchmarks.InMemory;
/// <summary>
/// Measures named batch tracing with synchronous handlers and single or explicit submissions.
/// </summary>
/// <remarks>
/// Runs with normal and suppressed submission ExecutionContext flow. Suppression covers only the synchronous public
/// submission calls and, for concurrent singles, Task.WhenAll creation; it is disposed before awaiting the returned
/// Task. The explicit-submission path returns its public Task directly, without Task.Run or a per-call async wrapper.
/// Each invocation validates 256 distinct request IDs, dispatch bounds, and the configured listener mode. Setup,
/// warmup calls, and provider disposal are outside measurement. Reported time and allocation are per wave.
/// No metrics provider, exporter, or downstream I/O is installed.
/// </remarks>
[MemoryDiagnoser]
[SimpleJob(RunStrategy.Throughput, launchCount: 2, warmupCount: 3, iterationCount: 10, id: "NamedTracingHotPath")]
public class NamedTracingHotPathBenchmarks
{
private const int RequestCount = 256;
private const int BatchSize = 32;
private const int MaxConcurrency = 8;
private const string BatcherName = "hot-path-orders";
private const string CallName = "RequestBatcher.Execute " + BatcherName;
private const string BatchName = "RequestBatcher.Handle " + BatcherName;
private readonly int[] _requests = Enumerable.Range(0, RequestCount).ToArray();
private ServiceProvider _provider = null!;
private IRequestBatcher<int> _batcher = null!;
private CountingHandler _handler = null!;
private ActivityListener? _listener;
private long _waves;
private long _expectedCalls;
private long _expectedBatches;
private long _stoppedCalls;
private long _stoppedBatches;
private string _method = string.Empty;
/// <summary>
/// Gets or sets the ActivityListener configuration.
/// </summary>
[Params(ListenerMode.Disabled, ListenerMode.Propagation, ListenerMode.Recorded)]
public ListenerMode TracingMode { get; set; }
/// <summary>
/// Gets or sets whether submission calls run while ExecutionContext flow is suppressed.
/// </summary>
/// <remarks>
/// Suppression is applied only around synchronous public submission calls and Task.WhenAll creation, then restored
/// before awaiting their returned task. Existing caller-owned suppression is preserved.
/// </remarks>
[Params(false, true)]
public bool SuppressSubmissionContextFlow { get; set; }
/// <summary>
/// Creates the named singleton batcher and warms both submission paths.
/// </summary>
[GlobalSetup]
public void GlobalSetup()
{
if (TracingMode != ListenerMode.Disabled)
{
_listener = new ActivityListener
{
ShouldListenTo = static source => source.Name == "RequestBatcher",
Sample = Sample,
ActivityStopped = OnActivityStopped,
};
ActivitySource.AddActivityListener(_listener);
}
var services = new ServiceCollection();
services.AddRequestBatcher<int, CountingHandler>(BatcherName, ServiceLifetime.Singleton, options =>
{
options.BatchSize = BatchSize;
options.MaxConcurrency = MaxConcurrency;
options.MaxPendingRequests = RequestCount;
});
_provider = services.BuildServiceProvider(new ServiceProviderOptions
{
ValidateOnBuild = true,
ValidateScopes = true,
});
_handler = (CountingHandler)_provider.GetRequiredKeyedService<IRequestBatchHandler<int>>(BatcherName);
_batcher = _provider.GetRequiredKeyedService<IRequestBatcher<int>>(BatcherName);
RunWaveAsync(explicitSubmission: false).GetAwaiter().GetResult();
RunWaveAsync(explicitSubmission: true).GetAwaiter().GetResult();
}
/// <summary>
/// Submits 256 concurrent single calls and validates each handled request.
/// </summary>
[Benchmark]
public Task ConcurrentSinglesAsync() => RunWaveAsync(explicitSubmission: false);
/// <summary>
/// Submits one explicit group of 256 requests and validates each handled request.
/// </summary>
[Benchmark]
public Task ExplicitSubmissionAsync() => RunWaveAsync(explicitSubmission: true);
/// <summary>
/// Drains the pipeline, validates stopped spans, and prints successful validation totals.
/// </summary>
[GlobalCleanup]
public async Task GlobalCleanupAsync()
{
try
{
await _provider.DisposeAsync().ConfigureAwait(false);
if (TracingMode == ListenerMode.Disabled)
{
if (_stoppedCalls != 0 || _stoppedBatches != 0)
{
throw new InvalidOperationException("Disabled tracing unexpectedly recorded spans.");
}
}
else if (_stoppedCalls != _expectedCalls || _stoppedBatches != _expectedBatches)
{
throw new InvalidOperationException(
$"Stopped spans mismatch: calls={_stoppedCalls}/{_expectedCalls}, batches={_stoppedBatches}/{_expectedBatches}.");
}
Console.WriteLine(
$"Named tracing validation: Method={_method}, Mode={TracingMode}, " +
$"SuppressSubmissionContextFlow={SuppressSubmissionContextFlow}, Waves={_waves}, " +
$"RequestsPerWave={RequestCount}, Calls={_stoppedCalls}, Batches={_stoppedBatches}, " +
"ExactlyOnce=True, DispatchBounds=True, SpanNames=True");
}
finally
{
_listener?.Dispose();
}
}
private async Task RunWaveAsync(bool explicitSubmission)
{
_method = explicitSubmission ? nameof(ExplicitSubmissionAsync) : nameof(ConcurrentSinglesAsync);
_handler.BeginWave();
var submission = SubmitWave(explicitSubmission);
await submission.ConfigureAwait(false);
_expectedBatches += _handler.ValidateWave();
_expectedCalls += explicitSubmission ? 1 : RequestCount;
_waves++;
}
private Task SubmitWave(bool explicitSubmission)
{
var wasFlowSuppressed = ExecutionContext.IsFlowSuppressed();
try
{
if (SuppressSubmissionContextFlow && !wasFlowSuppressed)
{
using (ExecutionContext.SuppressFlow())
{
return CreateSubmissionTask(explicitSubmission);
}
}
return CreateSubmissionTask(explicitSubmission);
}
finally
{
if (ExecutionContext.IsFlowSuppressed() != wasFlowSuppressed)
{
throw new InvalidOperationException("Submission changed caller-owned ExecutionContext flow suppression.");
}
}
}
private Task CreateSubmissionTask(bool explicitSubmission)
{
if (explicitSubmission)
{
return _batcher.ExecuteAsync(_requests);
}
var tasks = new Task[RequestCount];
for (var index = 0; index < tasks.Length; index++)
{
tasks[index] = _batcher.ExecuteAsync(_requests[index]);
}
return Task.WhenAll(tasks);
}
private ActivitySamplingResult Sample(ref ActivityCreationOptions<ActivityContext> options)
{
if (options.Name != CallName && options.Name != BatchName)
{
throw new InvalidOperationException($"Unexpected sampled span name: {options.Name}.");
}
return TracingMode == ListenerMode.Recorded
? ActivitySamplingResult.AllDataAndRecorded
: ActivitySamplingResult.PropagationData;
}
private void OnActivityStopped(Activity activity)
{
if (activity.Recorded != (TracingMode == ListenerMode.Recorded))
{
throw new InvalidOperationException("A stopped span has the wrong sampling flags.");
}
if (activity.OperationName == CallName)
{
Interlocked.Increment(ref _stoppedCalls);
}
else if (activity.OperationName == BatchName)
{
Interlocked.Increment(ref _stoppedBatches);
}
else
{
throw new InvalidOperationException($"Unexpected stopped span name: {activity.OperationName}.");
}
}
/// <summary>
/// Specifies whether Activities are disabled, propagation-only, or recorded.
/// </summary>
public enum ListenerMode
{
/// <summary>
/// No listener is installed.
/// </summary>
Disabled,
/// <summary>
/// Activities propagate context without recording.
/// </summary>
Propagation,
/// <summary>
/// Activities record all data and sampled trace flags.
/// </summary>
Recorded,
}
private sealed class CountingHandler : IRequestBatchHandler<int>
{
private readonly int[] _processed = new int[RequestCount];
private int _batches;
private int _active;
public void BeginWave()
{
if (Volatile.Read(ref _active) != 0)
{
throw new InvalidOperationException("A handler from the preceding wave is still active.");
}
Array.Clear(_processed);
_batches = 0;
}
public ValueTask HandleAsync(IReadOnlyList<int> requests, CancellationToken cancellationToken = default)
{
if (requests.Count is < 1 or > BatchSize)
{
throw new InvalidOperationException("The handler received an invalid batch size.");
}
var active = Interlocked.Increment(ref _active);
try
{
if (active > MaxConcurrency)
{
throw new InvalidOperationException("The handler exceeded its concurrency limit.");
}
Interlocked.Increment(ref _batches);
for (var index = 0; index < requests.Count; index++)
{
var request = requests[index];
if ((uint)request >= RequestCount || Interlocked.Increment(ref _processed[request]) != 1)
{
throw new InvalidOperationException($"Unexpected or repeated request: {request}.");
}
}
}
finally
{
Interlocked.Decrement(ref _active);
}
return ValueTask.CompletedTask;
}
public int ValidateWave()
{
if (Volatile.Read(ref _active) != 0 || _batches == 0)
{
throw new InvalidOperationException("The wave did not finish all handler work.");
}
for (var index = 0; index < _processed.Length; index++)
{
if (Volatile.Read(ref _processed[index]) != 1)
{
throw new InvalidOperationException($"Request {index} was not handled exactly once.");
}
}
return _batches;
}
}
}