3
3
4
4
using System ;
5
5
using System . Buffers ;
6
+ using System . Collections . Generic ;
6
7
using System . IO ;
7
8
using System . IO . Pipelines ;
8
9
using System . Reactive . Linq ;
@@ -168,6 +169,26 @@ public ChannelReader<int> StreamChannelReaderCount(int count)
168
169
return channel . Reader ;
169
170
}
170
171
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
+
171
192
public async Task UploadStream ( ChannelReader < string > channelReader )
172
193
{
173
194
while ( await channelReader . WaitToReadAsync ( ) )
@@ -177,63 +198,88 @@ public async Task UploadStream(ChannelReader<string> channelReader)
177
198
}
178
199
}
179
200
}
201
+
202
+ public async Task UploadStreamIAsynEnumerable ( IAsyncEnumerable < string > stream )
203
+ {
204
+ await foreach ( var item in stream )
205
+ {
206
+ }
207
+ }
180
208
}
181
209
182
210
[ Benchmark ]
183
211
public Task Invocation ( )
184
212
{
185
- return _dispatcher . DispatchMessageAsync ( _connectionContext , new InvocationMessage ( "123" , "Invocation" , Array . Empty < object > ( ) ) ) ;
213
+ return _dispatcher . DispatchMessageAsync ( _connectionContext , new InvocationMessage ( "123" , "Invocation" , Array . Empty < object > ( ) ) ) ;
186
214
}
187
215
188
216
[ Benchmark ]
189
217
public Task InvocationAsync ( )
190
218
{
191
- return _dispatcher . DispatchMessageAsync ( _connectionContext , new InvocationMessage ( "123" , "InvocationAsync" , Array . Empty < object > ( ) ) ) ;
219
+ return _dispatcher . DispatchMessageAsync ( _connectionContext , new InvocationMessage ( "123" , "InvocationAsync" , Array . Empty < object > ( ) ) ) ;
192
220
}
193
221
194
222
[ Benchmark ]
195
223
public Task InvocationReturnValue ( )
196
224
{
197
- return _dispatcher . DispatchMessageAsync ( _connectionContext , new InvocationMessage ( "123" , "InvocationReturnValue" , Array . Empty < object > ( ) ) ) ;
225
+ return _dispatcher . DispatchMessageAsync ( _connectionContext , new InvocationMessage ( "123" , "InvocationReturnValue" , Array . Empty < object > ( ) ) ) ;
198
226
}
199
227
200
228
[ Benchmark ]
201
229
public Task InvocationReturnAsync ( )
202
230
{
203
- return _dispatcher . DispatchMessageAsync ( _connectionContext , new InvocationMessage ( "123" , "InvocationReturnAsync" , Array . Empty < object > ( ) ) ) ;
231
+ return _dispatcher . DispatchMessageAsync ( _connectionContext , new InvocationMessage ( "123" , "InvocationReturnAsync" , Array . Empty < object > ( ) ) ) ;
204
232
}
205
233
206
234
[ Benchmark ]
207
235
public Task InvocationValueTaskAsync ( )
208
236
{
209
- return _dispatcher . DispatchMessageAsync ( _connectionContext , new InvocationMessage ( "123" , "InvocationValueTaskAsync" , Array . Empty < object > ( ) ) ) ;
237
+ return _dispatcher . DispatchMessageAsync ( _connectionContext , new InvocationMessage ( "123" , "InvocationValueTaskAsync" , Array . Empty < object > ( ) ) ) ;
210
238
}
211
239
212
240
[ Benchmark ]
213
241
public Task StreamChannelReader ( )
214
242
{
215
- return _dispatcher . DispatchMessageAsync ( _connectionContext , new StreamInvocationMessage ( "123" , "StreamChannelReader" , Array . Empty < object > ( ) ) ) ;
243
+ return _dispatcher . DispatchMessageAsync ( _connectionContext , new StreamInvocationMessage ( "123" , "StreamChannelReader" , Array . Empty < object > ( ) ) ) ;
216
244
}
217
245
218
246
[ Benchmark ]
219
247
public Task StreamChannelReaderAsync ( )
220
248
{
221
- return _dispatcher . DispatchMessageAsync ( _connectionContext , new StreamInvocationMessage ( "123" , "StreamChannelReaderAsync" , Array . Empty < object > ( ) ) ) ;
249
+ return _dispatcher . DispatchMessageAsync ( _connectionContext , new StreamInvocationMessage ( "123" , "StreamChannelReaderAsync" , Array . Empty < object > ( ) ) ) ;
222
250
}
223
251
224
252
[ Benchmark ]
225
253
public Task StreamChannelReaderValueTaskAsync ( )
226
254
{
227
- return _dispatcher . DispatchMessageAsync ( _connectionContext , new StreamInvocationMessage ( "123" , "StreamChannelReaderValueTaskAsync" , Array . Empty < object > ( ) ) ) ;
255
+ return _dispatcher . DispatchMessageAsync ( _connectionContext , new StreamInvocationMessage ( "123" , "StreamChannelReaderValueTaskAsync" , Array . Empty < object > ( ) ) ) ;
228
256
}
229
257
230
258
[ Benchmark ]
231
259
public async Task StreamChannelReaderCount_Zero ( )
232
260
{
233
- await _dispatcher . DispatchMessageAsync ( _connectionContext , new StreamInvocationMessage ( "123" , "StreamChannelReaderCount" , new object [ ] { 0 } ) ) ;
261
+ await _dispatcher . DispatchMessageAsync ( _connectionContext , new StreamInvocationMessage ( "123" , "StreamChannelReaderCount" , new object [ ] { 0 } ) ) ;
234
262
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 > ( ) ;
237
283
}
238
284
239
285
[ Benchmark ]
@@ -242,7 +288,25 @@ public async Task StreamChannelReaderCount_One()
242
288
await _dispatcher . DispatchMessageAsync ( _connectionContext , new StreamInvocationMessage ( "123" , "StreamChannelReaderCount" , new object [ ] { 1 } ) ) ;
243
289
244
290
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 > ( ) ;
246
310
}
247
311
248
312
[ Benchmark ]
@@ -251,32 +315,75 @@ public async Task StreamChannelReaderCount_Thousand()
251
315
await _dispatcher . DispatchMessageAsync ( _connectionContext , new StreamInvocationMessage ( "123" , "StreamChannelReaderCount" , new object [ ] { 1000 } ) ) ;
252
316
253
317
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 > ( ) ;
255
337
}
256
338
257
339
[ Benchmark ]
258
340
public async Task UploadStream_One ( )
259
341
{
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" ) ) ;
263
356
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 > ( ) ;
266
359
}
267
360
268
361
[ Benchmark ]
269
362
public async Task UploadStream_Thousand ( )
270
363
{
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 > ( ) ;
280
387
}
281
388
}
282
389
}
0 commit comments