I have been asked to load test an external webservice hosted by our client.
Basically to send multiple web requests to the service and time how long it takes for the service to process them all. To try and simulate multiple different users hitting the web service from different machines. The test will be to check that the webservice can handle this an to basically give the client some metrics and examples….e.g how long its takes 50 requests to respond.
I am aware that this could be achieved by using a tool like SoapUI/LoadUI.
What I want to know is there anyway I can write a load test using C#?
Currently I have something along the lines of the following:
[Test]
public void TestService50Requests()
{
List<WebRequest> requests = CreateMultipleRequests(50); //Creates web request objects
string timeSpan = TimeMultipleRequests(requests);
Console.WriteLine("50 Requests runtime: " + timeSpan);
}
private void TimeMultipleRequests(List<WebRequest> requests)
{
var stopWatch = new Stopwatch();
stopWatch.Start();
foreach (var request in requests)
{
request.GetResponse();
}
stopWatch.Stop();
var timeSpan = stopWatch.Elapsed;
return String.Format("{0:00} hours, {1:00} minutes, {2:00} seconds, {3:00} milliseconds", timeSpan.Hours, timeSpan.Minutes, timeSpan.Seconds, timeSpan.Milliseconds / 10);
}
I assume there is a better way to do this? Any suggestions for improvements or is this completely wrong?
It really depends on what you are trying to test. Load testing can be used to stress (how many concurrent users it takes to break the system) or soak (medium load for an extended time period) the system. Your code does not send any concurrent requests to the system so it cannot be considered a good test that would mimic the real behavior of users.
There are good tools to accomplish these, but they can be costly. If you have Visual Studio Ultimate at your disposal, the load testing tools included are pretty good. You can also look at WAPT, which is a tool that will do a similar thing.
So, I would go back to the person that gave you the requirement and ask what specifically needs to be tested. Is it number of concurrent users the system can handle, no memory leaks, system availability, or performance?