title: Unit Test: Replicating Heavy Server Load With Local Server
body: <p>I'm trying to test my site under heavy load. I have found that the incorrect title appears on some occassions which indicates to me that i have a possible threading issue. To help solve this i've setup the following test:</p>
  
  <pre><code>[TestMethod]
  public void Test1() {
      var isValid = true;
  
      for (var i = 0; i &lt; 100; i++) {
          // Send the request (note Http.WebRequest is a utility method which returns the contents of the page)
          var request = Http.WebRequest("http://localhost/SomePage");
  
          var document = new HtmlDocument(); // Html Agility Pack
          document.LoadHtml(request.Data);
  
          // Get the required info
          var title = document.DocumentNode.SelectSingleNode("//title").InnerText.Trim();
  
          // Test if the info is valid
          if (title != "Some Page") {
              isValid = false;
              break;
          }
  
          Thread.Sleep(100);
      }
  
      Assert.IsTrue(isValid);
  }
  </code></pre>
  
  <p>Notice i am issuing this web request to the local server. In which case this test always passes as the test is sequential and therefore only 1 request happens at a time. If i change the url to target the live server then the request fails which indicates a problem exists under heavy load.</p>
  
  <p>I was wondering how i could modify this test so that i can replicate the heavy load by with my local server. This way i can experiment with various fixes without having to mess around with the live site. After reading <a href="https://stackoverflow.com/questions/41568/whats-the-best-way-to-unit-test-from-multiple-threads">this</a> thread. I managed come up with the following:</p>
  
  <pre><code>[TestMethod]
  public void Test1() {
      var isValid = true;
      var threads = new Thread[100];
  
      for (var i = 0; i &lt; threads.Count(); i++) {
          threads[i] = new Thread(() =&gt; {
              for (var j = 0; j &lt; 10; j++) {
                  // Send the request
                  var request = Http.WebRequest("http://localhost/SomePage");
  
                  var document = new HtmlDocument();
                  document.LoadHtml(request.Data);
  
                  // Get the required info
                  var title = document.DocumentNode.SelectSingleNode("//title").InnerText.Trim();
  
                  // Test if the info is valid
                  if (title != "Some Page") {
                      isValid = false;
                      break;
                  }
              }
          });
      }
  
      foreach (var thread in threads) {
          thread.Start();
      }
  
      foreach (var thread in threads) {
          thread.Join();
      }
  
      Assert.IsTrue(isValid);
  }
  </code></pre>
  
  <p>However when i ran this test it threw the error "The agent process was stopped while the test was running.". I'd appreciate it if someone could show me what i'm doing wrong or suggest an alternate solution. Thanks</p>