Skip to content

Commit e3bf01e

Browse files
authored
Adding more streaming benchmark scenarios (#10049)
1 parent 48d2f40 commit e3bf01e

File tree

1 file changed

+134
-27
lines changed

1 file changed

+134
-27
lines changed

src/SignalR/perf/Microbenchmarks/DefaultHubDispatcherBenchmark.cs

Lines changed: 134 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
using System;
55
using System.Buffers;
6+
using System.Collections.Generic;
67
using System.IO;
78
using System.IO.Pipelines;
89
using System.Reactive.Linq;
@@ -168,6 +169,26 @@ public ChannelReader<int> StreamChannelReaderCount(int count)
168169
return channel.Reader;
169170
}
170171

172+
public async IAsyncEnumerable<int> StreamIAsyncEnumerableCount(int count)
173+
{
174+
await Task.Yield();
175+
176+
for (var i = 0; i < count; i++)
177+
{
178+
yield return i;
179+
}
180+
}
181+
182+
public async IAsyncEnumerable<int> StreamIAsyncEnumerableCountCompletedTask(int count)
183+
{
184+
await Task.CompletedTask;
185+
186+
for (var i = 0; i < count; i++)
187+
{
188+
yield return i;
189+
}
190+
}
191+
171192
public async Task UploadStream(ChannelReader<string> channelReader)
172193
{
173194
while (await channelReader.WaitToReadAsync())
@@ -177,63 +198,88 @@ public async Task UploadStream(ChannelReader<string> channelReader)
177198
}
178199
}
179200
}
201+
202+
public async Task UploadStreamIAsynEnumerable(IAsyncEnumerable<string> stream)
203+
{
204+
await foreach (var item in stream)
205+
{
206+
}
207+
}
180208
}
181209

182210
[Benchmark]
183211
public Task Invocation()
184212
{
185-
return _dispatcher.DispatchMessageAsync(_connectionContext, new InvocationMessage("123", "Invocation", Array.Empty<object>()));
213+
return _dispatcher.DispatchMessageAsync(_connectionContext, new InvocationMessage("123", "Invocation", Array.Empty<object>()));
186214
}
187215

188216
[Benchmark]
189217
public Task InvocationAsync()
190218
{
191-
return _dispatcher.DispatchMessageAsync(_connectionContext, new InvocationMessage("123", "InvocationAsync", Array.Empty<object>()));
219+
return _dispatcher.DispatchMessageAsync(_connectionContext, new InvocationMessage("123", "InvocationAsync", Array.Empty<object>()));
192220
}
193221

194222
[Benchmark]
195223
public Task InvocationReturnValue()
196224
{
197-
return _dispatcher.DispatchMessageAsync(_connectionContext, new InvocationMessage("123", "InvocationReturnValue", Array.Empty<object>()));
225+
return _dispatcher.DispatchMessageAsync(_connectionContext, new InvocationMessage("123", "InvocationReturnValue", Array.Empty<object>()));
198226
}
199227

200228
[Benchmark]
201229
public Task InvocationReturnAsync()
202230
{
203-
return _dispatcher.DispatchMessageAsync(_connectionContext, new InvocationMessage("123", "InvocationReturnAsync", Array.Empty<object>()));
231+
return _dispatcher.DispatchMessageAsync(_connectionContext, new InvocationMessage("123", "InvocationReturnAsync", Array.Empty<object>()));
204232
}
205233

206234
[Benchmark]
207235
public Task InvocationValueTaskAsync()
208236
{
209-
return _dispatcher.DispatchMessageAsync(_connectionContext, new InvocationMessage("123", "InvocationValueTaskAsync", Array.Empty<object>()));
237+
return _dispatcher.DispatchMessageAsync(_connectionContext, new InvocationMessage("123", "InvocationValueTaskAsync", Array.Empty<object>()));
210238
}
211239

212240
[Benchmark]
213241
public Task StreamChannelReader()
214242
{
215-
return _dispatcher.DispatchMessageAsync(_connectionContext, new StreamInvocationMessage("123", "StreamChannelReader", Array.Empty<object>()));
243+
return _dispatcher.DispatchMessageAsync(_connectionContext, new StreamInvocationMessage("123", "StreamChannelReader", Array.Empty<object>()));
216244
}
217245

218246
[Benchmark]
219247
public Task StreamChannelReaderAsync()
220248
{
221-
return _dispatcher.DispatchMessageAsync(_connectionContext, new StreamInvocationMessage("123", "StreamChannelReaderAsync", Array.Empty<object>()));
249+
return _dispatcher.DispatchMessageAsync(_connectionContext, new StreamInvocationMessage("123", "StreamChannelReaderAsync", Array.Empty<object>()));
222250
}
223251

224252
[Benchmark]
225253
public Task StreamChannelReaderValueTaskAsync()
226254
{
227-
return _dispatcher.DispatchMessageAsync(_connectionContext, new StreamInvocationMessage("123", "StreamChannelReaderValueTaskAsync", Array.Empty<object>()));
255+
return _dispatcher.DispatchMessageAsync(_connectionContext, new StreamInvocationMessage("123", "StreamChannelReaderValueTaskAsync", Array.Empty<object>()));
228256
}
229257

230258
[Benchmark]
231259
public async Task StreamChannelReaderCount_Zero()
232260
{
233-
await _dispatcher.DispatchMessageAsync(_connectionContext, new StreamInvocationMessage("123", "StreamChannelReaderCount", new object[] { 0 }));
261+
await _dispatcher.DispatchMessageAsync(_connectionContext, new StreamInvocationMessage("123", "StreamChannelReaderCount", new object[] { 0 }));
234262

235-
await (_connectionContext as NoErrorHubConnectionContext).ReceivedCompleted.Task;
236-
(_connectionContext as NoErrorHubConnectionContext).ReceivedCompleted = new TaskCompletionSource<object>();
263+
await (_connectionContext as NoErrorHubConnectionContext).ReceivedCompleted.Task;
264+
(_connectionContext as NoErrorHubConnectionContext).ReceivedCompleted = new TaskCompletionSource<object>();
265+
}
266+
267+
[Benchmark]
268+
public async Task StreamIAsyncEnumerableCount_Zero()
269+
{
270+
await _dispatcher.DispatchMessageAsync(_connectionContext, new StreamInvocationMessage("123", "StreamIAsyncEnumerableCount", new object[] { 0 }));
271+
272+
await (_connectionContext as NoErrorHubConnectionContext).ReceivedCompleted.Task;
273+
(_connectionContext as NoErrorHubConnectionContext).ReceivedCompleted = new TaskCompletionSource<object>();
274+
}
275+
276+
[Benchmark]
277+
public async Task StreamIAsyncEnumerableCompletedTaskCount_Zero()
278+
{
279+
await _dispatcher.DispatchMessageAsync(_connectionContext, new StreamInvocationMessage("123", "StreamIAsyncEnumerableCountCompletedTask", new object[] { 0 }));
280+
281+
await (_connectionContext as NoErrorHubConnectionContext).ReceivedCompleted.Task;
282+
(_connectionContext as NoErrorHubConnectionContext).ReceivedCompleted = new TaskCompletionSource<object>();
237283
}
238284

239285
[Benchmark]
@@ -242,7 +288,25 @@ public async Task StreamChannelReaderCount_One()
242288
await _dispatcher.DispatchMessageAsync(_connectionContext, new StreamInvocationMessage("123", "StreamChannelReaderCount", new object[] { 1 }));
243289

244290
await (_connectionContext as NoErrorHubConnectionContext).ReceivedCompleted.Task;
245-
(_connectionContext as NoErrorHubConnectionContext).ReceivedCompleted = new TaskCompletionSource<object>();
291+
(_connectionContext as NoErrorHubConnectionContext).ReceivedCompleted = new TaskCompletionSource<object>();
292+
}
293+
294+
[Benchmark]
295+
public async Task StreamIAsyncEnumerableCount_One()
296+
{
297+
await _dispatcher.DispatchMessageAsync(_connectionContext, new StreamInvocationMessage("123", "StreamIAsyncEnumerableCount", new object[] { 1 }));
298+
299+
await (_connectionContext as NoErrorHubConnectionContext).ReceivedCompleted.Task;
300+
(_connectionContext as NoErrorHubConnectionContext).ReceivedCompleted = new TaskCompletionSource<object>();
301+
}
302+
303+
[Benchmark]
304+
public async Task StreamIAsyncEnumerableCompletedTaskCount_One()
305+
{
306+
await _dispatcher.DispatchMessageAsync(_connectionContext, new StreamInvocationMessage("123", "StreamIAsyncEnumerableCountCompletedTask", new object[] { 1 }));
307+
308+
await (_connectionContext as NoErrorHubConnectionContext).ReceivedCompleted.Task;
309+
(_connectionContext as NoErrorHubConnectionContext).ReceivedCompleted = new TaskCompletionSource<object>();
246310
}
247311

248312
[Benchmark]
@@ -251,32 +315,75 @@ public async Task StreamChannelReaderCount_Thousand()
251315
await _dispatcher.DispatchMessageAsync(_connectionContext, new StreamInvocationMessage("123", "StreamChannelReaderCount", new object[] { 1000 }));
252316

253317
await (_connectionContext as NoErrorHubConnectionContext).ReceivedCompleted.Task;
254-
(_connectionContext as NoErrorHubConnectionContext).ReceivedCompleted = new TaskCompletionSource<object>();
318+
(_connectionContext as NoErrorHubConnectionContext).ReceivedCompleted = new TaskCompletionSource<object>();
319+
}
320+
321+
[Benchmark]
322+
public async Task StreamIAsyncEnumerableCount_Thousand()
323+
{
324+
await _dispatcher.DispatchMessageAsync(_connectionContext, new StreamInvocationMessage("123", "StreamIAsyncEnumerableCount", new object[] { 1000 }));
325+
326+
await (_connectionContext as NoErrorHubConnectionContext).ReceivedCompleted.Task;
327+
(_connectionContext as NoErrorHubConnectionContext).ReceivedCompleted = new TaskCompletionSource<object>();
328+
}
329+
330+
[Benchmark]
331+
public async Task StreamIAsyncEnumerableCompletedTaskCount_Thousand()
332+
{
333+
await _dispatcher.DispatchMessageAsync(_connectionContext, new StreamInvocationMessage("123", "StreamIAsyncEnumerableCountCompletedTask", new object[] { 1000 }));
334+
335+
await (_connectionContext as NoErrorHubConnectionContext).ReceivedCompleted.Task;
336+
(_connectionContext as NoErrorHubConnectionContext).ReceivedCompleted = new TaskCompletionSource<object>();
255337
}
256338

257339
[Benchmark]
258340
public async Task UploadStream_One()
259341
{
260-
await _dispatcher.DispatchMessageAsync(_connectionContext, new InvocationMessage("123", nameof(TestHub.UploadStream), Array.Empty<object>(), streamIds: new string[] { "1" }));
261-
await _dispatcher.DispatchMessageAsync(_connectionContext, new StreamItemMessage("1", "test"));
262-
await _dispatcher.DispatchMessageAsync(_connectionContext, CompletionMessage.Empty("1"));
342+
await _dispatcher.DispatchMessageAsync(_connectionContext, new InvocationMessage("123", nameof(TestHub.UploadStream), Array.Empty<object>(), streamIds: new string[] { "1" }));
343+
await _dispatcher.DispatchMessageAsync(_connectionContext, new StreamItemMessage("1", "test"));
344+
await _dispatcher.DispatchMessageAsync(_connectionContext, CompletionMessage.Empty("1"));
345+
346+
await (_connectionContext as NoErrorHubConnectionContext).ReceivedCompleted.Task;
347+
(_connectionContext as NoErrorHubConnectionContext).ReceivedCompleted = new TaskCompletionSource<object>();
348+
}
349+
350+
[Benchmark]
351+
public async Task UploadStreamIAsyncEnumerable_One()
352+
{
353+
await _dispatcher.DispatchMessageAsync(_connectionContext, new InvocationMessage("123", nameof(TestHub.UploadStreamIAsynEnumerable), Array.Empty<object>(), streamIds: new string[] { "1" }));
354+
await _dispatcher.DispatchMessageAsync(_connectionContext, new StreamItemMessage("1", "test"));
355+
await _dispatcher.DispatchMessageAsync(_connectionContext, CompletionMessage.Empty("1"));
263356

264-
await (_connectionContext as NoErrorHubConnectionContext).ReceivedCompleted.Task;
265-
(_connectionContext as NoErrorHubConnectionContext).ReceivedCompleted = new TaskCompletionSource<object>();
357+
await (_connectionContext as NoErrorHubConnectionContext).ReceivedCompleted.Task;
358+
(_connectionContext as NoErrorHubConnectionContext).ReceivedCompleted = new TaskCompletionSource<object>();
266359
}
267360

268361
[Benchmark]
269362
public async Task UploadStream_Thousand()
270363
{
271-
await _dispatcher.DispatchMessageAsync(_connectionContext, new InvocationMessage("123", nameof(TestHub.UploadStream), Array.Empty<object>(), streamIds: new string[] { "1" }));
272-
for (var i = 0; i < 1000; ++i)
273-
{
274-
await _dispatcher.DispatchMessageAsync(_connectionContext, new StreamItemMessage("1", "test"));
275-
}
276-
await _dispatcher.DispatchMessageAsync(_connectionContext, CompletionMessage.Empty("1"));
277-
278-
await (_connectionContext as NoErrorHubConnectionContext).ReceivedCompleted.Task;
279-
(_connectionContext as NoErrorHubConnectionContext).ReceivedCompleted = new TaskCompletionSource<object>();
364+
await _dispatcher.DispatchMessageAsync(_connectionContext, new InvocationMessage("123", nameof(TestHub.UploadStream), Array.Empty<object>(), streamIds: new string[] { "1" }));
365+
for (var i = 0; i < 1000; ++i)
366+
{
367+
await _dispatcher.DispatchMessageAsync(_connectionContext, new StreamItemMessage("1", "test"));
368+
}
369+
await _dispatcher.DispatchMessageAsync(_connectionContext, CompletionMessage.Empty("1"));
370+
371+
await (_connectionContext as NoErrorHubConnectionContext).ReceivedCompleted.Task;
372+
(_connectionContext as NoErrorHubConnectionContext).ReceivedCompleted = new TaskCompletionSource<object>();
373+
}
374+
375+
[Benchmark]
376+
public async Task UploadStreamIAsyncEnumerable_Thousand()
377+
{
378+
await _dispatcher.DispatchMessageAsync(_connectionContext, new InvocationMessage("123", nameof(TestHub.UploadStreamIAsynEnumerable), Array.Empty<object>(), streamIds: new string[] { "1" }));
379+
for (var i = 0; i < 1000; ++i)
380+
{
381+
await _dispatcher.DispatchMessageAsync(_connectionContext, new StreamItemMessage("1", "test"));
382+
}
383+
await _dispatcher.DispatchMessageAsync(_connectionContext, CompletionMessage.Empty("1"));
384+
385+
await (_connectionContext as NoErrorHubConnectionContext).ReceivedCompleted.Task;
386+
(_connectionContext as NoErrorHubConnectionContext).ReceivedCompleted = new TaskCompletionSource<object>();
280387
}
281388
}
282389
}

0 commit comments

Comments
 (0)