2009-09-01 30 views
43

Sono stressato testare un'app Web e ho impostato un programma di test di Windows che fa girare un numero di thread e invia una richiesta web su ognuno.Numero massimo di HttpWebRequests simultanei

Il problema è che ottengo il seguente 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! 

che tipo di sembra come se ci fosse un massimo di 5 filetti, anche se creo 100 come così:

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(); 
    } 

Qualcuno può illuminarmi per quanto sta succedendo?

Grazie

EDIT: Se, come suggerito dormo per 5 secondi, invece di HttpWebRequest, allora ottengo più thread sparare, ma non così tanti come mi sarei aspettato:

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! 

'ancora sembra che sto ottenendo solo 2 thread che partono ogni secondo, il che sembra molto lento per me. Suppongo che Console.WriteLine potrebbe essere un problema?

EDIT: ho impostato

ThreadPool.SetMinThreads(100, 4); 

e

System.Net.ServicePointManager.DefaultConnectionLimit = 100; 

ed ha ottenuto i seguenti risultati:

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 

così è stato in grado di spingere fuori un sacco di richieste web contemporaneamente. Che sembrava fare la fila (chiamando un server STA COM +), ecco cosa mi aspettavo.

Grazie per il vostro aiuto

+1

Ho una domanda per il vostro soluzione qui. in base a msdn (https://msdn.microsoft.com/en-us/library/system.net.servicepointmanager.defaultconnectionlimit(v=vs.110).aspx), il valore predefinito è max int32, che è molto più grande di 100 hai impostato qui. Come fa la differenza anche quando non l'hai impostata a 100? Voglio dire, dubito che lo abbia risolto impostando il limite di connessione predefinito di ServicePointManager o piuttosto impostando l'elemento connectionManagement nella prima risposta. – foxwendy

+1

@foxwendy Sì, MSDN dice che lo è. int.MaxValue, quando ho provato in .NET 4.5 è solo ** 2 ** per impostazione predefinita. – xmedeko

risposta

56

sono tutti (o quasi) le richieste che vanno allo stesso host per caso? Esiste un limite predefinito per host. È possibile modificare questo in app.config nell'elemento system.Net connectionManagement.

L'altra cosa è che il pool di thread aumenta solo gradualmente il numero di thread - inizia un nuovo thread ogni mezzo secondo, IIRC. Potrebbe essere quello che stai vedendo? Prova sbarazzarsi di HttpWebRequest dall'equazione - solo dormire per un paio di secondi, invece ...

ho il sospetto quest'ultimo problema è quello che si sta inizialmente correre in, ma la prima sta per causare problemi come bene.

+0

Sì, stanno andando tutti a localhost. – Duncan

+0

Non so se il limite si applica a localhost ... ma è sicuramente qualcosa di cui essere a conoscenza. Vedi la mia modifica per un collegamento all'elemento app.config appropraite. –

+0

Grazie, lo farà. – Duncan

48

C'è un limite nel numero di connessioni HTTP in uscita simultanee. Penso che tu possa controllarlo usando la proprietà statica System.Net.ServicePointManager.DefaultConnectionLimit prima di creare gli oggetti HttpWebRequest.

+0

Se qualcuno è curioso, il valore predefinito di questa impostazione è 2 –

1

Se scrivo sotto il tag in windows config che verrà eseguito ogni volta che viene eseguito il codice sottostante. Quindi, ogni volta che viene eseguito il codice sottostante, sarà consentito avere un massimo di 1000000 di richieste/risposte parallele.

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

Tag

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

non ho sentito parlare molto di questo per .NET Nucleo.ServicePointManager non era inclusa nel .NET core 1, ma sembra essere di nuovo nella versione 2. Tuttavia, per il HttpClient, è anche possibile impostare il numero massimo di connessioni in questo modo:

new HttpClient(new HttpClientHandler 
       { 
        MaxConnectionsPerServer = 100 
       })