UploadValues​​Async响应时间 [英] UploadValuesAsync response time

查看:165
本文介绍了UploadValues​​Async响应时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写的测试工具来测试HTTP POST。测试用例会在10秒的时间间隔在WebClient类使用UploadValues​​Async 8发送HTTP请求。它每隔8请求后休眠10秒。我的记录开始时间和每个请求的结束时间。当我计算的平均响应时间。我得到大约800毫秒。但是,当我使用同步方法UploadValues​​在Web客户端运行这个测试情况下,我得到的平均响应时间250毫秒。你能告诉我这是为什么这两种方法之间的区别?我期待在Aync少的响应时间,但我不明白了。

下面是code发送请求8异步

  VAR计数= 0;
        的foreach(在requestCollections VAR的NameValueCollection)
        {
            算上++;
            NameValueCollection中收集= NameValueCollection中;
            PostToURL(收集,URI);
            如果(计数%8 == 0)
            {
                Thread.sleep代码(TimeSpan.FromSeconds(10));
                计数= 0;
            }
        }

更新时间:
这里是code发送请求8 SYNC

 公共无效PostToURLSync(NameValueCollection中收集,开放的URI)
    {
        VAR响应=新ServiceResponse
        {
            响应=未启动
            请求=的string.join(;,collection.Cast<串GT;()
                                        。选择。(COL => String.Concat(COL,=,集合[COL))ToArray的()),
            的applicationID =收集[的applicationID]        };        尝试
        {
            使用(VAR transportType2 =新DerivedWebClient())
            {
                transportType2.Expect100Continue = FALSE;
                transportType2.Timeout = TimeSpan.FromMilliseconds(2000);
                response.StartTime = DateTime.Now;
                VAR responeByte = transportType2.UploadValues​​(URI,POST,集合);
                response.EndTime = DateTime.Now;
                response.Response = Encoding.Default.GetString(responeByte);
            }        }
        赶上(例外的例外)
        {
            Console.WriteLine(exception.ToString());
        }
        response.ResponseInMs =(INT)response.EndTime.Subtract(response.StartTime).TotalMilliseconds;
        responses.Add(响应);
        Console.WriteLine(response.ResponseInMs);
    }

下面是code,它发布到HTTP URI

 公共无效PostToURL(NameValueCollection中收集,开放的URI)
    {
        VAR响应=新ServiceResponse
                        {
                            响应=未启动
                            请求=的string.join(;,collection.Cast<串GT;()
                                                        。选择。(COL => String.Concat(COL,=,集合[COL))ToArray的()),
                            的applicationID =收集[的applicationID]                        };        尝试
        {
            使用(VAR transportType2 =新DerivedWebClient())
            {
                transportType2.Expect100Continue = FALSE;
                transportType2.Timeout = TimeSpan.FromMilliseconds(2000);
                response.StartTime = DateTime.Now;
                transportType2.UploadValues​​Completed + =新UploadValues​​CompletedEventHandler(transportType2_UploadValues​​Completed);
                transportType2.UploadValues​​Async(URI,POST,收集,响应);
            }
        }
        赶上(例外的例外)
        {
            Console.WriteLine(exception.ToString());
        }
    }

下面是上传完成事件

 私人无效transportType2_UploadValues​​Completed(对象发件人,UploadValues​​CompletedEventArgs E)
    {
        VAR现在= DateTime.Now;
        VAR响应=(ServiceResponse)e.UserState;
        response.EndTime =现在;
        response.ResponseInMs =(INT)response.EndTime.Subtract(response.StartTime).TotalMilliseconds;
        Console.WriteLine(response.ResponseInMs);        如果(e.Error!= NULL)
        {
            response.Response = e.Error.ToString();
        }
        其他
        如果(e.Result =空&放大器;!&放大器; e.Result.Length大于0)
        {
            字符串downloadedData = Encoding.Default.GetString(e.Result);
            response.Response = downloadedData;
        }
        //在全局变量记录响应
        responses.Add(响应);
    }


解决方案

至于贾斯汀说,我试着ServicePointManager.DefaultConnectionLimit,但没有解决问题。我能不能再现贾斯汀提出的其他问题。我不知道如何重现他们在首位。

我做什么,我跑在同行的机器在同一块code的运行完全响应时间我的预期。两机之间的差操作系统。我的是Windows Server 2003上运行和其他机器的Windows Server 2008。

上运行

由于它的工作的其他机器,我怀疑这可能是由Justin规定的问题之一,或者可能在2003年或别的东西是服务器设置。我并没有花多少时间后挖这个问题。由于这是一个测试工具,我们在这个问题上有低优先级。我们离开了,没有时间进一步。

正如我究竟是什么固定它,我不接受这个比其他任何回答没有胶水。在非常至少监守我知道,切换到2008年的服务器修复了这个问题。

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

解决方案

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.

这篇关于UploadValues​​Async响应时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆