Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I prevent Socket/Port Exhaustion?
    text
    copied!<p>I am attempting to performance test a website by hitting it with requests across multiple threads. Each thread executes <em>n</em> times. (in a for loop)</p> <p>However, I am running into problems. Specifically the WebException ("Unable to connect to remote server") with the inner exception:</p> <blockquote> <p>An operation on a socket could not be performed because the system lacked sufficient buffer space or because a queue was full 127.0.0.1:52395</p> </blockquote> <p>I am attempting to run 100 threads at 500 iterations per thread. </p> <p>Initially I was using <code>HttpWebRequest</code> in System.Net to make the GET request to the server. Currently I am using <code>WebClient</code> as I assumed that each iteration was using a new socket (so 100 * 500 sockets in a short period of time). I assumed WebClient (which is instantiated once per thread) would only use one socket. </p> <p>I don't need 50 000 sockets open at once, as I would like to send the GET request, receive the response, and close the socket, freeing it for use in the next loop iteration. I understand that it would be a problem to </p> <p>However, even with WebClient, a bunch of sockets are being requested resulting in a bunch of sockets in <code>TIME_WAIT</code> mode (checked using netstat). This causes other applications (like internet browsers) to hang and stop functioning.</p> <p>I can operate my test with less iterations and/or less threads, as it appears the sockets do eventually exit this TIME_WAIT state. However, this is not a solution as it doesn't adequately test the abilities of the web server.</p> <p><strong>Question:</strong></p> <p>How do I explicitly close a socket (from the client side) after each thread iteration in order to prevent TIME_WAIT states and socket exhaustion?</p> <p><strong>Code:</strong></p> <p>Class that wraps the HttpRequest</p> <p><em>Edit:</em> Wrapped WebClient in a using, so a new one is instantiated,used and disposed for every iteration. The problem still persists.</p> <pre><code> public sealed class HttpGetTest : ITest { private readonly string m_url; public HttpGetTest( string url ) { m_url = url; } void ITest.Execute() { using (WebClient webClient = new WebClient()){ using( Stream stream = webClient.OpenRead( m_url ) ) { } } } } </code></pre> <p>The part of my ThreadWrapperClass that creates a new thread:</p> <pre><code>public void Execute() { Action Hammer = () =&gt; { for( int i = 1; i &lt;= m_iterations; i++ ) { //Where m_test is an ITest injected through constructor m_test.Execute(); } }; ThreadStart work = delegate { Hammer(); }; Thread thread = new Thread( work ); thread.Start(); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload