// Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System; using System.Collections.Generic; using System.Threading.Tasks; using BenchmarkDotNet.Attributes; using Microsoft.AspNetCore.SignalR.Protocol; using Microsoft.AspNetCore.SignalR.Microbenchmarks.Shared; using Microsoft.Extensions.Logging.Abstractions; namespace Microsoft.AspNetCore.SignalR.Microbenchmarks { public class DefaultHubLifetimeManagerBenchmark { private DefaultHubLifetimeManager _hubLifetimeManager; private List _connectionIds; private List _subsetConnectionIds; private List _groupNames; private List _userIdentifiers; [Params(true, false)] public bool ForceAsync { get; set; } [GlobalSetup] public void GlobalSetup() { _hubLifetimeManager = new DefaultHubLifetimeManager(NullLogger>.Instance); _connectionIds = new List(); _subsetConnectionIds = new List(); _groupNames = new List(); _userIdentifiers = new List(); var jsonHubProtocol = new JsonHubProtocol(); for (int i = 0; i < 100; i++) { string connectionId = "connection-" + i; string groupName = "group-" + i % 10; string userIdentifier = "user-" + i % 20; AddUnique(_connectionIds, connectionId); AddUnique(_groupNames, groupName); AddUnique(_userIdentifiers, userIdentifier); if (i % 3 == 0) { _subsetConnectionIds.Add(connectionId); } var connectionContext = new TestConnectionContext { ConnectionId = connectionId, Transport = new TestDuplexPipe(ForceAsync) }; var hubConnectionContext = new HubConnectionContext(connectionContext, TimeSpan.Zero, NullLoggerFactory.Instance); hubConnectionContext.UserIdentifier = userIdentifier; hubConnectionContext.Protocol = jsonHubProtocol; _hubLifetimeManager.OnConnectedAsync(hubConnectionContext).GetAwaiter().GetResult(); _hubLifetimeManager.AddToGroupAsync(connectionId, groupName); } } private void AddUnique(List list, string connectionId) { if (!list.Contains(connectionId)) { list.Add(connectionId); } } [Benchmark] public Task SendAllAsync() { return _hubLifetimeManager.SendAllAsync("MethodName", Array.Empty()); } [Benchmark] public Task SendGroupAsync() { return _hubLifetimeManager.SendGroupAsync(_groupNames[0], "MethodName", Array.Empty()); } [Benchmark] public Task SendGroupsAsync() { return _hubLifetimeManager.SendGroupsAsync(_groupNames, "MethodName", Array.Empty()); } [Benchmark] public Task SendGroupExceptAsync() { return _hubLifetimeManager.SendGroupExceptAsync(_groupNames[0], "MethodName", Array.Empty(), _subsetConnectionIds); } [Benchmark] public Task SendAllExceptAsync() { return _hubLifetimeManager.SendAllExceptAsync("MethodName", Array.Empty(), _subsetConnectionIds); } [Benchmark] public Task SendConnectionAsync() { return _hubLifetimeManager.SendConnectionAsync(_connectionIds[0], "MethodName", Array.Empty()); } [Benchmark] public Task SendConnectionsAsync() { return _hubLifetimeManager.SendConnectionsAsync(_subsetConnectionIds, "MethodName", Array.Empty()); } [Benchmark] public Task SendUserAsync() { return _hubLifetimeManager.SendUserAsync(_userIdentifiers[0], "MethodName", Array.Empty()); } [Benchmark] public Task SendUsersAsync() { return _hubLifetimeManager.SendUsersAsync(_userIdentifiers, "MethodName", Array.Empty()); } } }