.net 最大并发 HttpWebRequests 数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1361771/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 13:15:12  来源:igfitidea点击:

Max number of concurrent HttpWebRequests

.nethttpwebrequestmultithreading

提问by Duncan

I'm stress testing a web app and have set up a windows test program that spins up a number of threads and sends out a web request on each one.

我正在对一个 Web 应用程序进行压力测试,并设置了一个 Windows 测试程序,该程序可以启动多个线程并在每个线程上发出一个 Web 请求。

Problem is I get the following output:

问题是我得到以下输出:

01/09/09 11:34:04 Starting new HTTP request on 10
01/09/09 11:34:04 Starting new HTTP request on 11
01/09/09 11:34:04 Starting new HTTP request on 13
01/09/09 11:34:05 Starting new HTTP request on 14
01/09/09 11:34:05 Starting new HTTP request on 11
01/09/09 11:34:05 11 has finished!
01/09/09 11:34:05 Starting new HTTP request on 13
01/09/09 11:34:05 13 has finished!
01/09/09 11:34:05 Starting new HTTP request on 14
01/09/09 11:34:05 14 has finished!
01/09/09 11:34:05 Starting new HTTP request on 11
01/09/09 11:34:05 11 has finished!
01/09/09 11:34:05 Starting new HTTP request on 14
01/09/09 11:34:05 14 has finished!
01/09/09 11:34:05 Starting new HTTP request on 13
01/09/09 11:34:05 13 has finished!
01/09/09 11:34:05 Starting new HTTP request on 15
01/09/09 11:34:06 Starting new HTTP request on 11
01/09/09 11:34:06 11 has finished!
01/09/09 11:34:06 Starting new HTTP request on 14
01/09/09 11:34:06 14 has finished!

which sort of looks like there's a maximum of 5 threads, even if I create 100 as so:

哪种看起来最多有 5 个线程,即使我这样创建 100 个:

int numberOfThreads = Convert.ToInt32(txtConcurrentThreads.Text);

    List<BackgroundWorker> workers = new List<BackgroundWorker>();

    for (int N = 0; N < numberOfThreads; N++)
    {

        BackgroundWorker worker = new BackgroundWorker();
        worker.DoWork += new DoWorkEventHandler(worker_DoWork);
        worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(worker_RunWorkerCompleted);
        workers.Add(worker);
    }


    foreach(BackgroundWorker worker in workers)
    {
        worker.RunWorkerAsync();
    }

Can anyone enlighten me as to what is going on?

任何人都可以启发我了解正在发生的事情吗?

Thanks

谢谢

EDIT: If as suggested I sleep for 5 seconds, instead of httpwebrequest, then I do get more threads firing but not as many as I would have expected:

编辑:如果按照建议我睡了 5 秒钟,而不是 httpwebrequest,那么我确实会触发更多线程,但没有我预期的那么多:

01/09/09 11:56:14 Starting new HTTP request on 7
01/09/09 11:56:14 Starting new HTTP request on 11
01/09/09 11:56:15 Starting new HTTP request on 12
01/09/09 11:56:15 Starting new HTTP request on 13
01/09/09 11:56:16 Starting new HTTP request on 14
01/09/09 11:56:16 Starting new HTTP request on 15
01/09/09 11:56:17 Starting new HTTP request on 16
01/09/09 11:56:17 Starting new HTTP request on 17
01/09/09 11:56:18 Starting new HTTP request on 18
01/09/09 11:56:19 Starting new HTTP request on 7
01/09/09 11:56:19 7 has finished!
01/09/09 11:56:19 Starting new HTTP request on 11
01/09/09 11:56:19 11 has finished!
01/09/09 11:56:19 Starting new HTTP request on 19
01/09/09 11:56:20 Starting new HTTP request on 20
01/09/09 11:56:20 Starting new HTTP request on 12
01/09/09 11:56:20 12 has finished!

It still looks like I'm only getting 2 threads starting every second, which seems mighty slow to me. I suppose the Console.WriteLine could be a problem?

看起来我每秒只启动 2 个线程,这对我来说似乎很慢。我想 Console.WriteLine 可能有问题?

EDIT: I set

编辑:我设置

ThreadPool.SetMinThreads(100, 4); 

and

System.Net.ServicePointManager.DefaultConnectionLimit = 100;

and got the following results:

并得到以下结果:

01/09/09 14:00:07 Starting new HTTP request on 11
01/09/09 14:00:07 Starting new HTTP request on 81
01/09/09 14:00:07 Starting new HTTP request on 82
01/09/09 14:00:07 Starting new HTTP request on 79
01/09/09 14:00:07 Starting new HTTP request on 83
01/09/09 14:00:07 Starting new HTTP request on 84
01/09/09 14:00:07 Starting new HTTP request on 85
01/09/09 14:00:07 Starting new HTTP request on 87
01/09/09 14:00:07 Starting new HTTP request on 88
...
01/09/09 14:00:07 84 has finished! Took 323.0323 milliseconds
01/09/09 14:00:08 88 has finished! Took 808.0808 milliseconds
01/09/09 14:00:08 96 has finished! Took 806.0806 milliseconds
01/09/09 14:00:08 94 has finished! Took 806.0806 milliseconds
01/09/09 14:00:08 98 has finished! Took 801.0801 milliseconds
01/09/09 14:00:08 80 has finished! Took 799.0799 milliseconds
01/09/09 14:00:08 86 has finished! Took 799.0799 milliseconds
01/09/09 14:00:08 92 has finished! Took 799.0799 milliseconds
01/09/09 14:00:08 100 has finished! Took 812.0812 milliseconds
01/09/09 14:00:08 82 has finished! Took 1010.101 milliseconds

so was able to push out a whole lot of web requests concurrently. Which seemed to queue (calling out to an STA COM+ server) so that's what I expected.

所以能够同时推出大量的网络请求。这似乎在排队(调用 STA COM+ 服务器),这正是我所期望的。

Thanks for your help

谢谢你的帮助

回答by Jon Skeet

Are all (or most) of your requests going to the same host by any chance? There's a built-in limit on a per-host basis. You can change this in app.config in the system.Net connectionManagementelement.

您的所有(或大部分)请求是否都会发送到同一主机?每个主机都有一个内置限制。您可以在 system.Net connectionManagement元素中的 app.config 中更改此设置。

The other thing is that the thread pool only ramps up its number of threads gradually - it starts a new thread every half second, IIRC. Could that be what you're seeing? Try getting rid of HttpWebRequestfrom the equation - just sleep for a couple of seconds instead...

另一件事是线程池只会逐渐增加其线程数——它每半秒启动一个新线程,IIRC。那会是你看到的吗?尝试摆脱HttpWebRequest等式 - 只需睡几秒钟......

I suspect the latter problem is the one you're initially running into, but the first one is going to cause you problems as well.

我怀疑后一个问题是您最初遇到的问题,但第一个问题也会给您带来问题。

回答by Fredrik M?rk

There is a limit in the number of simultaneous outgoing HTTP connections. I think you can control this by using the System.Net.ServicePointManager.DefaultConnectionLimitstatic property before creating the HttpWebRequestobjects.

同时传出 HTTP 连接的数量有限制。我认为您可以System.Net.ServicePointManager.DefaultConnectionLimit在创建HttpWebRequest对象之前使用静态属性来控制它。

回答by cbp

I haven't heard much about this for .NET Core. ServicePointManager was not included in .NET Core 1, but appears to be back again in version 2. However, for the HttpClient, you can also set the maximum number of connections like this:

我对 .NET Core 的了解不多。ServicePointManager 未包含在 .NET Core 1 中,但似乎在版本 2 中再次出现。但是,对于 HttpClient,您还可以设置最大连接数,如下所示:

new HttpClient(new HttpClientHandler
                {
                    MaxConnectionsPerServer = 100
                })

回答by user220550

If i write below tag in windows config than will it be executed every time when below code is executed. So every time below code is executed i will be allowed to have max 1000000 no of parallel request/response.

如果我在 Windows 配置中写下面的标签,那么每次执行下面的代码时都会执行它。所以每次执行下面的代码时,我将被允许有最多 1000000 个并行请求/响应。

HttpWebRequest POSTRequest = (HttpWebRequest)WebRequest.Create("http://yahoo.com");

Tag

标签

<connectionManagement>
 <clear/>
 <add address="*" maxconnection="1000000" />
</connectionManagement>