using System; using System.Threading; using System.Threading.Channels; using System.Threading.Tasks; using BenchmarkDotNet.Attributes; using Microsoft.AspNetCore.SignalR.Internal.Protocol; using Microsoft.AspNetCore.Sockets; using Microsoft.AspNetCore.Sockets.Internal; using Microsoft.Extensions.Logging.Abstractions; namespace Microsoft.AspNetCore.SignalR.Microbenchmarks { [ParameterizedJobConfig(typeof(CoreConfig))] public class BroadcastBenchmark { private DefaultHubLifetimeManager _hubLifetimeManager; private HubContext _hubContext; [Params(1, 10, 1000)] public int Connections; [GlobalSetup] public void GlobalSetup() { _hubLifetimeManager = new DefaultHubLifetimeManager(); var options = new UnboundedChannelOptions { AllowSynchronousContinuations = true }; for (var i = 0; i < Connections; ++i) { var transportToApplication = Channel.CreateUnbounded(options); var applicationToTransport = Channel.CreateUnbounded(options); var application = ChannelConnection.Create(input: applicationToTransport, output: transportToApplication); var transport = ChannelConnection.Create(input: transportToApplication, output: applicationToTransport); var connection = new DefaultConnectionContext(Guid.NewGuid().ToString(), transport, application); _hubLifetimeManager.OnConnectedAsync(new HubConnectionContext(connection, Timeout.InfiniteTimeSpan, NullLoggerFactory.Instance)).Wait(); } _hubContext = new HubContext(_hubLifetimeManager); } [Benchmark] public Task InvokeAsyncAll() { return _hubContext.All.InvokeAsync("Method"); } } }