title: Improving performance of multithreaded HttpWebRequests in .NET
body: <p>I am trying to measure the throughput of a webservice.</p>
  
  <p>In order to do that, I have written a small tool that continuously sends requests and reads responses from a number of threads.</p>
  
  <p>The contents of the inner loop of each thread looks like this:</p>
  
  <pre><code>public void PerformRequest()
  {
    WebRequest webRequest = WebRequest.Create(_uri);
  
    webRequest.ContentType = "application/ocsp-request";
    webRequest.Method = "POST";
    webRequest.Credentials = _credentials;
    webRequest.ContentLength = _request.Length;
    ((HttpWebRequest)webRequest).KeepAlive = false;
  
    using (Stream st = webRequest.GetRequestStream())
      st.Write(_request, 0, _request.Length);
  
    using (HttpWebResponse httpWebResponse = (HttpWebResponse)webRequest.GetResponse())
    using (Stream responseStream = httpWebResponse.GetResponseStream())
    using (BufferedStream bufferedStream = new BufferedStream(responseStream))
    using (BinaryReader reader = new BinaryReader(bufferedStream))
    {
      if (httpWebResponse.StatusCode != HttpStatusCode.OK)
        throw new WebException("Got response status code: " + httpWebResponse.StatusCode);
  
      byte[] response = reader.ReadBytes((int)httpWebResponse.ContentLength);
      httpWebResponse.Close();
    }      
  }
  </code></pre>
  
  <p>It seems to work okay, except that something seems to be limiting the tool. If I run two instances of the tool with each 40 threads, I get significantly more throughput than one instance with 80 threads.</p>
  
  <p>I found the ServicePointManager.DefaultConnectionLimit property, which I set to 10000 (and it makes no difference if I set it through app.config <a href="https://stackoverflow.com/a/436477/5542">as suggested by Jader Dias</a>).</p>
  
  <p>Are there any other settings in .NET or on my machine that can influence the performance? (I am running Vista, but I see the same problem on Windows Server 2003).</p>
  
  <p>Perhaps some restrictions on how many connections a single process can make?</p>