加载测试SignalR集线器应用程序的最佳方法是什么?

ElH*_*aix 24 asp.net-mvc asp.net-4.0 signalr

我想知道一些用于测试基于SignalR集线器的应用程序的不同方法.

ElH*_*aix 10

简而言之,如果使用集线器,使用.Net客户端就足够了.

在我的情况下,我有一个新闻源中心,根据用户的个人资料ID列出客户特定的数据.在我的测试用例中,我加载了一堆配置文件ID(在这种情况下为6000),调用名为JoinNewsfeed()的hub方法以及特定于客户端的连接ID和配置文件ID.每100ms建立一个新连接.

    [TestMethod]
    public void TestJoinNewsfeedHub()
    {
        int successfulConnections = 0;

        // get profile ID's
        memberService = new MemberServiceClient();
        List<int> profileIDs = memberService.GetProfileIDs(6000).ToList<int>();

        HubConnection hubConnection = new HubConnection(HUB_URL);
        IHubProxy newsfeedHub = hubConnection.CreateProxy("NewsfeedHub");


        foreach (int profileID in profileIDs)
        {
            hubConnection.Start().Wait();
            //hubConnection = EstablishHubConnection();
            newsfeedHub.Invoke<string>("JoinNewsfeed", hubConnection.ConnectionId, profileID).ContinueWith(task2 =>
            {
                if (task2.IsFaulted)
                {
                    System.Diagnostics.Debug.Write(String.Format("An error occurred during the method call {0}", task2.Exception.GetBaseException()));
                }
                else
                {
                    successfulConnections++;
                    System.Diagnostics.Debug.Write(String.Format("Successfully called MethodOnServer: {0}", successfulConnections));

                }
            });

            Thread.Sleep(100);

        }

        Assert.IsNotNull(newsfeedHub);
    }
Run Code Online (Sandbox Code Playgroud)

此处列出的性能指标可以在服务器上执行.为了确保客户端实际连接并且在服务器上填充客户端对象的过程已成功完成,我有一个服务器端方法调用客户端函数,其中包含从连接客户端派生的已连接客户端的数量和列表采集.


cod*_*ain 8

@ElHaix从我在自己的测试中看到的,您的方法不是创建新连接,而是重用现有连接.当您遍历profileID集合时,您应该看到hubConnection.ConnectionID保持不变.要创建新连接,您需要在foreach循环中创建HubConnection实例.

        int successfulConnections = 0;
        const int loopId = 10;

        Console.WriteLine("Starting...");
        for (int i = 1; i <= loopId; i++)
        {
            Console.WriteLine("loop " + i);

            var hubConnection = new HubConnection(HUB_URL);
            IHubProxy chatHub = hubConnection.CreateProxy(HUB_NAME);

            Console.WriteLine("Starting connection");
            hubConnection.Start().Wait();
            Console.WriteLine("Connection started: " + hubConnection.ConnectionId);

            chatHub.Invoke("Register", "testroom").ContinueWith(task2 =>
            {
                if (task2.IsFaulted)
                {
                    Console.WriteLine(String.Format("An error occurred during the method call {0}", task2.Exception.GetBaseException()));
                }
                else
                {
                    Console.WriteLine("Connected: " + hubConnection.ConnectionId);
                    successfulConnections++;
                }
            });

            Thread.Sleep(1000);
        }
Run Code Online (Sandbox Code Playgroud)