HttpWebRequest的添加间隔时变慢 [英] HttpWebRequest gets slower when adding an Interval

查看:496
本文介绍了HttpWebRequest的添加间隔时变慢的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

测试不同的可能性,网页的源代码,我得到了以下结果下载(平均时间在毫秒到google.com,9gag.com):

Testing different possibilities to download the source of a webpage I got the following results (Average time in ms to google.com, 9gag.com):

  • 普通的HttpWebRequest:169,360
  • Gzip已HttpWebRequest的:143, 260
  • 在Web客户端GetStream: 132 295
  • 在Web客户端DownloadString:143,389
  • Plain HttpWebRequest: 169, 360
  • Gzip HttpWebRequest: 143, 260
  • WebClient GetStream : 132, 295
  • WebClient DownloadString: 143, 389

所以我9gag客户端,我决定采取GZIP HttpWebRequest的。问题是,实施在我的实际程序后,该请求需要的两倍以上的时间。
此问题也时只是增加了两个请求之间的Thread.Sleep发生。

So for my 9gag client I decided to take the gzip HttpWebRequest. The problem is, after implementing in my actual program, the request takes more than twice the time.
The Problem also occurs when just adding a Thread.Sleep between two requests.

编辑:
只是提高了codeA位,还是同样的问题:在一个循环的要求需要更长的时间运行时,我想补充的请求的延迟当


Just improved the code a bit, still the same problem: When running in a loop the requests takes longer when I add an Delay between to requests

for(int i = 0; i < 100; i++)
{
    getWebsite("http://9gag.com/");
}

注意到每次请求大约250毫秒。

Takes about 250ms per request.

for(int i = 0; i < 100; i++)
{
    getWebsite("http://9gag.com/");
    Thread.Sleep(1000);
}

注意到每次请求大约610ms。

Takes about 610ms per request.

    private string getWebsite(string Url)
    {
        Stopwatch stopwatch = Stopwatch.StartNew();

        HttpWebRequest http = (HttpWebRequest)WebRequest.Create(Url);
        http.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
        string html = string.Empty;

        using (HttpWebResponse webResponse = (HttpWebResponse)http.GetResponse())
        using (Stream responseStream = webResponse.GetResponseStream())
        using (StreamReader reader = new StreamReader(responseStream))
        {

            html = reader.ReadToEnd();
        }

        Debug.WriteLine(stopwatch.ElapsedMilliseconds);
        return html;
    }

任何想法来解决这个问题呢?

Any ideas to fix this problem?

推荐答案

也许试试这个,虽然它可能只帮助你的情况下,单个请求,实际上使事情变得更糟做多线程版本的时候。

Maybe give this a try, although it might only help your case of a single request and actually make things worse when doing a multithreaded version.

ServicePointManager.UseNagleAlgorithm = false;

下面是从MSDN文档报价为 HttpWebRequest类

Here's a quote from MSDN docs for the HttpWebRequest Class

这会对性能产生影响的另一个选择是使用   该UseNagleAlgorithm财产。当此属性设置为true,   TCP / IP将尝试使用TCP Nagle算法用于HTTP连接。   发送TCP数据包时Nag​​le算法汇总数据。它   积累小的消息序列插入之前更大的TCP数据包   数据被通过网络发送。使用Nagle算法可以   优化网络资源的使用,尽管在某些情况下   性能也可以降低。一般来说恒定大批量   吞吐量,性能改进是使用内格尔实现   算法。但对于较小的吞吐量的应用,退化   性能可以看出

Another option that can have an impact on performance is the use of the UseNagleAlgorithm property. When this property is set to true, TCP/IP will try to use the TCP Nagle algorithm for HTTP connections. The Nagle algorithm aggregates data when sending TCP packets. It accumulates sequences of small messages into larger TCP packets before the data is sent over the network. Using the Nagle algorithm can optimize the use of network resources, although in some situations performance can also be degraded. Generally for constant high-volume throughput, a performance improvement is realized using the Nagle algorithm. But for smaller throughput applications, degradation in performance may be seen.

应用程序无法正常需要更改默认值   在UseNagleAlgorithm属性被设置为true。但是,如果一个   采用低延迟连接的应用程序,它可以帮助设置这个   属性设置为false。

An application doesn't normally need to change the default value for the UseNagleAlgorithm property which is set to true. However, if an application is using low-latency connections, it may help to set this property to false.

这篇关于HttpWebRequest的添加间隔时变慢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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