并发HttpWebRequest的最大数目

我正在testing一个web应用程序的压力,并build立了一个windowstesting程序,它可以激活多个线程并发送一个web请求。

问题是我得到以下输出:

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! 

看起来最多有5个线程,即使我创build了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(); } 

任何人都可以启发我到底是怎么回事?

谢谢

编辑:如果build议我睡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! 

它看起来像我只是每秒开始2个线程,这似乎对我来说很慢。 我想Console.WriteLine可能是一个问题?

编辑:我设置

 ThreadPool.SetMinThreads(100, 4); 

 System.Net.ServicePointManager.DefaultConnectionLimit = 100; 

并得到以下结果:

 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 

所以能够同时推出大量的Web请求。 这似乎排队(呼叫到一个STA COM +服务器),这就是我所期望的。

谢谢你的帮助

您的请求中的所有(或大部分)请求都是由同一个主机进行的吗? 每个主机都有一个内置的限制。 您可以在system.Net connectionManagement元素中的app.config中更改此值。

另一件事是线程池只是逐渐增加线程的数量 – 它每半秒启动一个新线程,IIRC。 这可能是你所看到的吗? 尝试摆脱HttpWebRequest从公式 – 只是睡了几秒钟,而不是…

我怀疑后面的问题是你最初遇到的问题,但是第一个问题也会导致你的问题。

同时传出的HTTP连接数量有限制。 我想你可以通过在创buildHttpWebRequest对象之前使用System.Net.ServicePointManager.DefaultConnectionLimit静态属性来控制这个。

如果我在windowsconfiguration下面写标签,那么每次下面的代码执行的时候都会执行它。 所以每次下面的代码执行,我将被允许有最大1000000没有并行请求/响应。

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

标签

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

我还没有听说过这个.NET核心。 在.NET Core 1中没有包含ServicePointManager,但在版本2中似乎又回来了。但是,对于HttpClient,您还可以设置最大连接数,如下所示:

 new HttpClient(new HttpClientHandler { MaxConnectionsPerServer = 100 })