使用集线器对广播消息进行信号器测试

时间:2014-08-05 04:06:30

标签: c# .net testing signalr scale

我有一个非常简单的基于信号灯的Hub应用程序,它基本上会定期向大约1000个客户端发送1种单一类型的消息。

我希望能够测试1000个Web浏览器是否可以在可重复的负载测试环境中连接并从集线器接收消息。

我已经看到信号器文档显示了如何使用Crank工具加载测试。但是这具有无法测试Hub类型的部署的限制,这没有帮助。

有没有人看到如何创建集线器和客户端的实例并测试X个客户端是否在特定时间范围内收到了消息?

1 个答案:

答案 0 :(得分:0)

根据zaitsman的评论,我提出了以下代码,我希望得到一些反馈?

[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();

        });
    }
}