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

サーバー App.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ではありませんが、あなたのアーキテクチャがあなたを遅くしているのです。

たとえば、ThreadPool からスレッドを取得しようとする 1k のタスクを作成しているとします。すべてのタスクは、WCF サービスから応答を取得した場合にのみ完了します (タスクごとに WCF サービスを同期的に呼び出します)。

さて、一度に 1k や 10k のジョブを開始すると思われるかもしれませんが、実際には、.NET のスレッドプールは少数のスレッド (fe) で開始されます。4 必要に応じて時間をかけて数を増やします。これには時間がかかる場合があります。

スレッドプールによって割り当てられる必要があるスレッドの最小数を増やすことで、これがボトルネックであるかどうかをすばやく確認できます。

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

これは、問題を解決するのではなく、問題を回避する方法です。アーキテクチャは異なる必要があり、作業を 10k ではなく 2 ~ 4 のタスクに分散する必要があります。

また、リクエストごとに新しいクライアントを作成しているようです。基本的に、多くの配管作業を行っており、多くのリソースを割り当てています (#task != #threads、しかしそれらはまだ相関しています)非常に単純なタスクであるため、実際には WCF の作業ではなく、自分の作業を測定しています。

于 2013-03-02T14:56:30.237 に答える