我正在对网络应用程序进行压力测试,并设置了一个Windows测试程序,该程序可以旋转多个线程,并在每个线程上发送一个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个线程,即使我这样创建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();
}
任何人都可以告诉我发生了什么事吗?
由于
编辑:如果按照建议我睡了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 +服务器),这就是我的预期。
感谢您的帮助
答案 0 :(得分:69)
您的所有(或大多数)请求是否都有机会进入同一主机?每个主机有一个内置限制。您可以在system.Net connectionManagement元素的app.config中更改此内容。
另一件事是线程池只逐渐增加其线程数 - 它每半秒启动一个新线程,IIRC。这可能是你所看到的吗?尝试从等式中删除HttpWebRequest
- 只需睡几秒钟......
我怀疑后一个问题是你最初遇到的问题,但第一个问题也会引起你的问题。
答案 1 :(得分:53)
同时传出HTTP连接的数量有限制。我认为你可以在创建HttpWebRequest
对象之前使用System.Net.ServicePointManager.DefaultConnectionLimit
静态属性来控制它。
答案 2 :(得分:6)
对于.NET Core,我没有听说过这个。 ServicePointManager未包含在.NET Core 1中,但在版本2中似乎又回来了。但是,对于HttpClient,您还可以设置最大连接数,如下所示:
new HttpClient(new HttpClientHandler
{
MaxConnectionsPerServer = 100
})
答案 3 :(得分:4)
如果我在windows config中写下tag,而不是每次执行下面的代码时都执行。因此,每次执行代码时,我都将被允许最大1000000没有并行请求/响应。
HttpWebRequest POSTRequest = (HttpWebRequest)WebRequest.Create("http://yahoo.com");
标签
<connectionManagement>
<clear/>
<add address="*" maxconnection="1000000" />
</connectionManagement>