-1

我创建了一个 WCF 服务器和一个 WCF 客户端来使用该服务。服务器的目的是将传入的 2 个数字相加,并在返回总和之前等待 X 毫秒。

客户端创建 Y 个任务并启动它们。每个任务都是服务器添加数字并等待 X 毫秒的请求。

当 x = 0 和 y = 1000 时,完成所有任务平均需要 6.2 秒。当 X = 0 和 Y = 10000 时,完成所有任务平均需要 61 秒。

为什么它这么慢,或者这可能是正常的?

谢谢达摩

客户端 C# 方法

private void radButtonTaskWithStatus_Click(object sender, EventArgs e)
        {
            try
            {
                var dateTime1 = DateTime.UtcNow;
                radProgressBarStatus.Maximum = int.Parse(radTextBoxFloodRequests.Text);
                radProgressBarStatus.Value1 = 0;

                Random rnd = new Random();


                Task<int>[] tasks = new Task<int>[int.Parse(radTextBoxFloodRequests.Text)];

                for (int i = 0; i < int.Parse(radTextBoxFloodRequests.Text); i++)
                {
                    int x = i;
                    tasks[i] = new Task<int>(() =>
                    {    

                        int FirstRandomNumber = rnd.Next(1, 20);
                        int SecondRandomNumber = rnd.Next(1, 20);


                        int result = TaskRequestWithResult(FirstRandomNumber, SecondRandomNumber, int.Parse(radTextBoxFloodDelay.Text), x);    

                        return result;
                    });
                }

                var continuation = Task.Factory.ContinueWhenAll(
                            tasks,
                            (antecedents) =>
                            {
                                var dateTime2 = DateTime.UtcNow;
                                var diffInSeconds = (dateTime2 - dateTime1).TotalSeconds;
                                this.radListView1.BeginInvoke((MethodInvoker)(() => this.radListView1.Items.Add((dateTime2 - dateTime1).TotalSeconds)));
                                //MessageBox.Show(diffInSeconds.ToString());


                                int total = 0;
                                for (int i = 0; i < int.Parse(radTextBoxFloodRequests.Text); i++)
                                    total = total + tasks[i].Result;
                                Debug.Print("Finished - Sum of all results is: " + total);
                                //RadMessageBox.Show("Finished - Sum of all results is: " + total);                     
                            });


                for (int i = 0; i < int.Parse(radTextBoxFloodRequests.Text); i++)
                {
                    tasks[i].Start();
                  //  System.Threading.Thread.Sleep(10); // Wait
                }

                TaskProgress(tasks, count => Invoke(new MethodInvoker(() => radProgressBarStatus.Value1 = count)));

                // Use next line if you want to block the main thread until all the tasks are complete
                //continuation.Wait();


            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message.ToString());

            }
        }

        public static void TaskProgress(IEnumerable<Task> tasks, Action<int> callback)
        {
            int count = 0;
            foreach (var task in tasks)
                task.ContinueWith(t => callback(Interlocked.Increment(ref count)));
        }

        private int TaskRequestWithResult(int number1, int number2, int delay, int count)
        {

            try
            {

                ServiceReference1.WCFJobsLibraryClient Client = new ServiceReference1.WCFJobsLibraryClient();
                Client.Endpoint.Address = new System.ServiceModel.EndpointAddress(radTextBoxbaseAddress.Text);
                WCFClient.ServiceReference1.ReturnClass AddNumbers_Result;

                AddNumbers_Result = Client.AddNumbers(number1, number2, delay);


                if (AddNumbers_Result.ErrorCode < 0)
                {
                    // If exception happens, it will be returned here
                    MessageBox.Show(AddNumbers_Result.ErrorCode.ToString() + " " + AddNumbers_Result.ErrorMessage + " " + AddNumbers_Result.ExMessage);
                }

                else
                {
                    Debug.Print("Task Executing now: " + count.ToString());                   

                }

                return AddNumbers_Result.Result;

            }

            catch (Exception ex)
            {                
                MessageBox.Show(ex.Message.ToString());
                return -1;

            }
        }


    }

客户端 App.config

<configuration>
    <system.serviceModel>
        <bindings>
            <basicHttpBinding>
                <binding name="BasicHttpEndpoint" closeTimeout="00:01:00" openTimeout="00:01:00"
                    receiveTimeout="00:10:00" sendTimeout="00:10:00" allowCookies="false"
                    bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
                    maxBufferSize="65536" maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
                    messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered"
                    useDefaultWebProxy="true">
                    <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
                        maxBytesPerRead="4096" maxNameTableCharCount="16384" />
                    <security mode="None">
                        <transport clientCredentialType="None" proxyCredentialType="None"
                            realm="" />
                        <message clientCredentialType="UserName" algorithmSuite="Default" />
                    </security>
                </binding>
            </basicHttpBinding>
        </bindings>
        <client>
            <endpoint address="http://localhost:8732/Design_Time_Addresses/WCFService/WCFJobsLibrary/"
                binding="basicHttpBinding" bindingConfiguration="BasicHttpEndpoint"
                contract="ServiceReference1.IWCFJobsLibrary" name="BasicHttpEndpoint" />
        </client>
    </system.serviceModel>
</configuration>

服务器 C# 方法

[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession, ConcurrencyMode = ConcurrencyMode.Multiple)]
public class WCFJobsLibrary : IWCFJobsLibrary
{

    public ReturnClass AddNumbers(int FirstNumber, int SecondNumber, int Delay) //Add two numbers and wait a predefined interval
    {
        Logging.Write_To_Log_File("Entry", MethodBase.GetCurrentMethod().Name, "", "", "", 1);
        ReturnClass myReturnClass = new ReturnClass(-1, null, null, 0);
        try
        {             

            myReturnClass.ErrorCode = 1;

            myReturnClass.Result = FirstNumber + SecondNumber;

            System.Threading.Thread.Sleep(Delay); // Wait
            Logging.Write_To_Log_File("Exit", MethodBase.GetCurrentMethod().Name, "", "", "", 1);
            return myReturnClass;

        }
        catch (Exception ex)
        {
            Logging.Write_To_Log_File("Error", MethodBase.GetCurrentMethod().Name, "", "", ex.ToString(), 2);
            myReturnClass.ErrorCode = -1;
            myReturnClass.ErrorMessage = ex.ToString();
            return myReturnClass;
        }

    }

//Add two numbers and wait defined interval
[OperationContract]
ReturnClass AddNumbers(int FirstNumber, int SecondNumber, int Delay);

服务器应用程序.config

<configuration>
  <system.serviceModel>
  <services>
    <service name="WCFService.WCFJobsLibrary" behaviorConfiguration="Throttled" >
      <endpoint address="" binding="basicHttpBinding" bindingConfiguration=""
        name="BasicHttpEndpoint" contract="WCFService.IWCFJobsLibrary">
        <identity>
          <dns value="localhost" />
        </identity>
      </endpoint>
      <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
      <host>
        <baseAddresses>
          <add baseAddress="http://localhost:8732/Design_Time_Addresses/WCFService/WCFJobsLibrary/" />
        </baseAddresses>
      </host>
    </service>
  </services>
  <behaviors>
    <serviceBehaviors>
      <behavior name="Throttled">
        <!-- To avoid disclosing metadata information, 
          set the value below to false and remove the metadata endpoint above before deployment -->
        <serviceMetadata httpGetEnabled="true" />
        <!-- To receive exception details in faults for debugging purposes, 
          set the value below to true.  Set to false before deployment 
          to avoid disclosing exception information -->
        <serviceDebug includeExceptionDetailInFaults="true" />
        <serviceThrottling maxConcurrentCalls="100000" maxConcurrentInstances="100000" maxConcurrentSessions="100000" />
      </behavior>
    </serviceBehaviors>
  </behaviors>
  </system.serviceModel>
</configuration>
4

1 回答 1

6

不是 WCF,而是你的架构让你慢了下来。

例如,您正在创建 1k 个任务,这些任务将尝试从 ThreadPool 中获取线程,每个任务仅在它从您的 WCF 服务获得响应时完成(它基于每个任务同步调用您的 WCF 服务)。

现在,您可能认为您将立即启动 1k 或 10k 个作业,但实际上,.NET 的线程池将从少量线程开始,fe。4 并在需要时随着时间的推移增加它们的数量,这可能需要一些时间。

您可以通过增加线程池应分配的最小线程数来快速检查这是否是瓶颈:

int min, io;
ThreadPool.GetMinThreads(out min, out io);
ThreadPool.SetMinThreads(1000, io);

这只是如何规避你的问题,而不是解决它,你的架构应该不同,将你的工作分散在 2-4 个任务之间,而不是 10k !

此外,您似乎正在为每个请求创建一个新客户端,基本上,您正在做很多管道工作,分配大量资源(#task!= #threads,但这些仍然相关),对于非常简单的任务,因此您不是真正衡量 WCF 工作,而是您的工作。

于 2013-03-02T14:56:30.237 回答