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:
[TestMethod]
public void Test1() {
var isValid = true;
for (var i = 0; i < 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);
}
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.
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 this thread. I managed come up with the following:
[TestMethod]
public void Test1() {
var isValid = true;
var threads = new Thread[100];
for (var i = 0; i < threads.Count(); i++) {
threads[i] = new Thread(() => {
for (var j = 0; j < 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);
}
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