Estoy poniendo a prueba una aplicación web y he configurado un programa de prueba de Windows que genera varios hilos y envía una solicitud web para cada uno.Número máximo de HttpWebRequests concurrentes
El problema es que me da el siguiente resultado:
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!
qué tipo de parece que hay un máximo de 5 hilos, incluso si creo 100 como así:
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();
}
Puede alguien me ilumine en cuanto a lo que está pasando?
Gracias
EDIT: Si como se sugiere que dormir durante 5 segundos, en lugar de HttpWebRequest, entonces me pongo más hilos de disparar, pero no tantos como yo hubiera esperado:
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!
Todavía parece que solo estoy obteniendo 2 hilos comenzando cada segundo, lo cual me parece muy lento. ¿Supongo que la Console.WriteLine podría ser un problema?
EDIT: establece
ThreadPool.SetMinThreads(100, 4);
y
System.Net.ServicePointManager.DefaultConnectionLimit = 100;
y obtuvo los siguientes resultados:
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
así fue capaz de empujar a cabo una gran cantidad de peticiones web al mismo tiempo. Que parecía cola (llamar a un servidor STA COM +), así que eso es lo que esperaba.
Gracias por su ayuda
Tengo una pregunta para su solución aquí. de acuerdo con msdn (https://msdn.microsoft.com/en-us/library/system.net.servicepointmanager.defaultconnectionlimit(v=vs.110).aspx), el valor predeterminado es el máximo int32, que es mucho más grande de 100 que estableces aquí. ¿Cómo hace la diferencia incluso cuando no lo configuraste en 100? Quiero decir, dudo que haya resuelto el problema estableciendo el límite de conexión predeterminado de ServicePointManager o, más bien, estableciendo el elemento connectionManagement en la primera respuesta. – foxwendy
@foxwendy Sí, MSDN dice que sí. int.MaxValue, cuando lo he probado en .NET 4.5, es ** ** ** por defecto. – xmedeko