UploadValuesAsync response time

孤人 提交于 2019-12-08 06:41:05

问题


I am writing test harness to test a HTTP Post. Test case would send 8 http request using UploadValuesAsync in webclient class in 10 seconds interval. It sleeps 10 seconds after every 8 request. I am recording start time and end time of each request. When I compute the average response time. I am getting around 800 ms. But when I run this test case synchronously using UploadValues method in web client I am getting average response time 250 milliseconds. Can you tell me why is difference between these two methods? I was expecting the less response time in Aync but I did not get that.

Here is code that sends 8 requests async

                       var count = 0;
        foreach (var nameValueCollection in requestCollections)
        {
            count++;
            NameValueCollection collection = nameValueCollection;
            PostToURL(collection,uri);
            if (count % 8 == 0)
            {
                Thread.Sleep(TimeSpan.FromSeconds(10));
                count = 0;
            }
        }

UPDATED Here is code that sends 8 requests SYNC

public void PostToURLSync(NameValueCollection collection,Uri uri)
    {
        var response = new ServiceResponse
        {
            Response = "Not Started",
            Request = string.Join(";", collection.Cast<string>()
                                        .Select(col => String.Concat(col, "=", collection[col])).ToArray()),
            ApplicationId = collection["ApplicationId"]

        };

        try
        {
            using (var transportType2 = new DerivedWebClient())
            {
                transportType2.Expect100Continue = false;
                transportType2.Timeout = TimeSpan.FromMilliseconds(2000);
                response.StartTime = DateTime.Now;
                var responeByte = transportType2.UploadValues(uri, "POST", collection);
                response.EndTime = DateTime.Now;
                response.Response = Encoding.Default.GetString(responeByte);
            }

        }
        catch (Exception exception)
        {
            Console.WriteLine(exception.ToString());
        }
        response.ResponseInMs = (int)response.EndTime.Subtract(response.StartTime).TotalMilliseconds;
        responses.Add(response);
        Console.WriteLine(response.ResponseInMs);
    }

Here is the code that post to the HTTP URI

public void PostToURL(NameValueCollection collection,Uri uri)
    {
        var response = new ServiceResponse
                        {
                            Response = "Not Started",
                            Request = string.Join(";", collection.Cast<string>()
                                                        .Select(col => String.Concat(col, "=", collection[col])).ToArray()),
                            ApplicationId = collection["ApplicationId"]

                        };

        try
        {
            using (var transportType2 = new DerivedWebClient())
            {
                transportType2.Expect100Continue = false;
                transportType2.Timeout = TimeSpan.FromMilliseconds(2000);
                response.StartTime = DateTime.Now;
                transportType2.UploadValuesCompleted += new UploadValuesCompletedEventHandler(transportType2_UploadValuesCompleted);
                transportType2.UploadValuesAsync(uri, "POST", collection,response);
            }
        }
        catch (Exception exception)
        {
            Console.WriteLine(exception.ToString());
        }
    }

Here is the upload completed event

    private void transportType2_UploadValuesCompleted(object sender, UploadValuesCompletedEventArgs e)
    {
        var now = DateTime.Now;
        var response = (ServiceResponse)e.UserState;
        response.EndTime = now;
        response.ResponseInMs = (int) response.EndTime.Subtract(response.StartTime).TotalMilliseconds;
        Console.WriteLine(response.ResponseInMs);

        if (e.Error != null)
        {
            response.Response = e.Error.ToString();
        }
        else
        if (e.Result != null && e.Result.Length > 0)
        {
            string downloadedData = Encoding.Default.GetString(e.Result);
            response.Response = downloadedData;
        }
        //Recording response in Global variable
        responses.Add(response);
    }

回答1:


One problem you're probably running into is that .NET, by default, will throttle outgoing HTTP connections to the limit (2 concurrent connections per remote host) that are mandated by the relevant RFC. Assuming 2 concurrent connections and 250ms per request, that means the response time for your first 2 requests will be 250ms, the second 2 will be 500ms, the third 750ms, and the last 1000ms. This would yield a 625ms average response time, which is not far from the 800ms you're seeing.

To remove the throttling, increase ServicePointManager.DefaultConnectionLimit to the maximum number of concurrent connections you want to support, and you should see your average response time go down alot.

A secondary problem may be that the server itself is slower handling multiple concurrent connections than handing one request at a time. Even once you unblock the throttling problem above, I'd expect each of the async requests to, on average, execute somewhat slower than if the server was only executing one request at a time. How much slower depends on how well the server is optimized for concurrent requests.

A final problem may be caused by test methodology. For example, if your test client is simulating a browser session by storing cookies and re-sending cookies with each request, that may run into problems with some servers that will serialize requests from a single user. This is often a simplification for server apps so they won't have to deal with locking cross-requests state like session state. If you're running into this problem, make sure that each WebClient sends different cookies to simulate different users.

I'm not saying that you're running into all three of these problems-- you might be only running into 1 or 2-- but these are the most likley culprits for the problem you're seeing.




回答2:


As Justin said, I tried ServicePointManager.DefaultConnectionLimit but that did not fix the issue. I could not able reproduce other problems suggested by Justin. I am not sure how to reproduce them in first place.

What I did, I ran the same piece of code in peer machine that runs perfectly response time that I expected. The difference between the two machines is operating systems. Mine is running on Windows Server 2003 and other machine is running on Windows Server 2008.

As it worked on the other machines, I suspect that it might be one of the problem specified by Justin or could be server settings on 2003 or something else. I did not spend much time after that to dig this issue. As this is a test harness that we had low priority on this issue. We left off with no time further.

As I have no glue on what exactly fixed it, I am not accepting any answer other than this. Becuase at very least I know that switching to server 2008 fixed this issue.



来源:https://stackoverflow.com/questions/8000107/uploadvaluesasync-response-time

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!