I am stress testing the server and thus creating the HttpWebRequest
inside the factory but when the response time for various number of threads is checked then for 1 thread the response time is high and when number of threads increases then response time decreases. what can be the reason?
code is as below:
for (int i = 0; i < tsk.Length; i++)
{
tsk[i] = Task.Factory.StartNew((object obj) =>
{
System.Diagnostics.Stopwatch watch = System.Diagnostics.Stopwatch.StartNew();
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
watch.Stop();
}, i);
}
The reason for such behavior is simple: the more tasks you have in your system, the smaller time each of them have to do it's job. Your computer has a limited set of the cores on the CPU, so after fixed value you'll get the thread starvation problem, which leads to higher response time.
You really should switch your measuring to the server itself, save the time on request start and on the response end - the all other time is not about your server performance but only about infrastructure speed.
Also I highly recommend you to use Parallel
class instead of TPL
for the stress testing as it fits better for simultaneous operations.
Another issue in your code is that you're using the counter variable i
in the closure, so you should copy it:
for (int i = 0; i < tsk.Length; i++)
{
int localI = i;
tsk[localI] = Task.Factory.StartNew((object obj) =>
{
System.Diagnostics.Stopwatch watch = System.Diagnostics.Stopwatch.StartNew();
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
watch.Stop();
}, localI);
}