Skip to content

Commit 00468fd

Browse files
committed
Added a simple stress test
1 parent 499b388 commit 00468fd

File tree

4 files changed

+84
-5
lines changed

4 files changed

+84
-5
lines changed

demo/DemoServer/ChatHub.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,14 @@
55

66
namespace DemoServer
77
{
8+
public class UserIdProvider : IUserIdProvider
9+
{
10+
public virtual string GetUserId(HubConnectionContext connection)
11+
{
12+
return "staticUserid";
13+
}
14+
}
15+
816
public class ChatHubA : Hub
917
{
1018
private readonly ILogger<ChatHubA> _logger;
@@ -53,7 +61,11 @@ public ChatHubB(ILogger<ChatHubB> logger)
5361
public async Task SendMessage(string name, string message)
5462
{
5563
// _logger.LogInformation($"{nameof(SendMessage)} called. ConnectionId:{Context.ConnectionId}, Name:{name}, Message:{message}");
56-
await Clients.Group(groupId).SendAsync("BroadcastMessage", name, message);
64+
await Clients.Group(groupId).SendAsync("BroadcastMessage", name, "group");
65+
await Clients.Caller.SendAsync("BroadcastMessage", name, "caller");
66+
await Clients.User(Context.UserIdentifier).SendAsync("BroadcastMessage", name, "user");
67+
await Clients.All.SendAsync("BroadcastMessage", name, "all");
68+
await Clients.AllExcept(Context.ConnectionId).SendAsync("BroadcastMessage", name, "allExcept");
5769
}
5870

5971
public override async Task OnConnectedAsync()

demo/DemoServer/Startup.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using Microsoft.AspNetCore.Builder;
33
using Microsoft.AspNetCore.Hosting;
44
using Microsoft.AspNetCore.HttpsPolicy;
5+
using Microsoft.AspNetCore.SignalR;
56
using Microsoft.Extensions.Configuration;
67
using Microsoft.Extensions.DependencyInjection;
78
using Microsoft.Extensions.Hosting;
@@ -24,6 +25,8 @@ public Startup(IConfiguration configuration)
2425
// This method gets called by the runtime. Use this method to add services to the container.
2526
public void ConfigureServices(IServiceCollection services)
2627
{
28+
services.AddSingleton<IUserIdProvider, UserIdProvider>();
29+
2730
services.AddRazorPages();
2831
services.AddSignalR()
2932
.AddSqlServer(o =>

src/IntelliTect.AspNetCore.SignalR.SqlServer/SqlServerHubLifetimeManager.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -403,9 +403,9 @@ private async Task EnsureSqlServerConnection()
403403

404404
_streams.Add(stream);
405405

406-
StartReceiving(streamIndex);
406+
StartReceiving();
407407

408-
void StartReceiving(int streamIndex)
408+
void StartReceiving()
409409
{
410410
stream.StartReceiving((id, message) => OnReceived(message))
411411
.ContinueWith(async t =>
@@ -416,7 +416,7 @@ void StartReceiving(int streamIndex)
416416

417417
// Try again in a little bit
418418
await Task.Delay(2000);
419-
StartReceiving(streamIndex);
419+
StartReceiving();
420420
}, TaskContinuationOptions.OnlyOnFaulted);
421421
}
422422

test/IntelliTect.AspNetCore.SignalR.SqlServer.Tests/SqlServerEndToEndTests.cs

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,71 @@ public async Task CanSendAndReceivePayloads_WithPolling()
4949
await RunCore(options, prefix);
5050
}
5151

52-
private static async Task RunCore(SqlServerOptions options, string prefix)
52+
53+
[SkippableFact]
54+
public async Task CanSendAndReceivePayloads_WithServiceBroker_UnderHeavyLoad()
55+
{
56+
await CreateDatabaseAsync();
57+
58+
var options = new SqlServerOptions
59+
{
60+
ConnectionString = connectionString,
61+
AutoEnableServiceBroker = true,
62+
Mode = SqlServerMessageMode.ServiceBroker
63+
};
64+
65+
var prefix = nameof(CanSendAndReceivePayloads_WithServiceBroker_UnderHeavyLoad);
66+
var installer = new SqlInstaller(options, NullLogger.Instance, prefix);
67+
var receiver = new SqlReceiver(options, NullLogger.Instance, prefix + "_0", "");
68+
69+
var receivedMessages = new ConcurrentBag<byte[]>();
70+
await installer.Install();
71+
var receiverTask = receiver.Start((_, message) =>
72+
{
73+
receivedMessages.Add(message);
74+
return Task.CompletedTask;
75+
});
76+
// Give the receiver time to reach a steady state (waiting).
77+
await Task.Delay(150);
78+
79+
var cts = new CancellationTokenSource();
80+
81+
// This is roughly analagous to number of connections, not number of servers.
82+
// The reasoning is that each connected client to the hub could be triggering
83+
// the hub to be sending messages.
84+
int numSenders = 100;
85+
int numSent = 0;
86+
var sender = new SqlSender(options, NullLogger.Instance, prefix + "_0");
87+
for (int i = 0; i < numSenders; i++)
88+
{
89+
_ = Task.Run(async () =>
90+
{
91+
var random = new Random();
92+
while (!cts.IsCancellationRequested)
93+
{
94+
var payload = new byte[255];
95+
random.NextBytes(payload);
96+
await sender.Send(payload);
97+
Interlocked.Increment(ref numSent);
98+
}
99+
}, cts.Token);
100+
}
101+
102+
var payload = new byte[255];
103+
new Random().NextBytes(payload);
104+
await Task.Delay(10000);
105+
cts.Cancel();
106+
107+
// Give the receiver time to reach a steady state (waiting).
108+
await Task.Delay(1000);
109+
110+
Assert.Equal(numSent, receivedMessages.Count);
111+
112+
receiver.Dispose();
113+
await receiverTask;
114+
}
115+
116+
private async Task RunCore(SqlServerOptions options, string prefix)
53117
{
54118
var installer = new SqlInstaller(options, NullLogger.Instance, prefix);
55119
var sender = new SqlSender(options, NullLogger.Instance, prefix + "_0");

0 commit comments

Comments
 (0)