I have a very simple signalr Hub based application that basically sends out 1 single type of message periodically to around 1000 clients at a time.
I would like to be able to test that 1000 web browsers can connect and receive a message from the hub in a repeatable load testing environment.
I have seen that the signalr documentation shows how to load test using the Crank
tool. But this has the limitation of not being able to test the Hub types of deployments, which is not helpful.
Has anyone seen how one might create instances of a hub and clients and test that X number of clients all received a message within a certain time frame?
Based on the comment from zaitsman, I have come up with the following code which I would appreciate some feedback on?
[TestClass]
public class SignalRConnectionTest
{
static AutoResetEvent _autoEvent = new AutoResetEvent(false);
private const string HUB_URL = "http://mysite/signalr";
private const string HUB_NAME = "chatHub";
private static Dictionary<int, bool> _clientResponsesRecieved = new Dictionary<int, bool>();
private static object lockObject = new object();
[TestMethod]
public void TestJoinHub()
{
const int numberOfClients = 10;
var manualEvents = new ManualResetEvent[numberOfClients];
for (int i = 0; i < numberOfClients; i++)
{
manualEvents[i] = new ManualResetEvent(false);
var stateInfo = new State(manualEvents[i], i);
ThreadPool.QueueUserWorkItem(ConnectToSignalRAndWaitForMessage, stateInfo);
}
foreach (var manualResetEvent in manualEvents)
manualResetEvent.WaitOne();
Assert.AreEqual(_clientResponsesRecieved.Count, numberOfClients);
foreach (var key in _clientResponsesRecieved.Keys)
{
Assert.IsTrue(_clientResponsesRecieved[key]);
}
}
class State
{
public int threadId = 0;
public ManualResetEvent manualEvent;
public State(ManualResetEvent manualEvent, int threadId)
{
this.manualEvent = manualEvent;
this.threadId = threadId;
}
}
private void ConnectToSignalRAndWaitForMessage(object state)
{
var stateInfo = (State) state;
var hubConnection = new HubConnection(HUB_URL);
var chatHub = hubConnection.CreateHubProxy(HUB_NAME);
Console.WriteLine("Starting connection");
hubConnection.Start().Wait();
chatHub.On<string, string>("broadcastMessage", (username, message) =>
{
Console.WriteLine("Message Recieved: {0}", message);
lock (lockObject)
{
_clientResponsesRecieved.Add(stateInfo.threadId, true);
}
stateInfo.manualEvent.Set();
});
}
}