I have a WCF service hosted locally exposing a basic method that returns the string value you pass to it.
I also have a unit test where I try to call the service 10000 times and I monitor the time it takes for the calls to complete.
The position of my using statement in my test is crucial and makes a big difference if placed incorrectly, however I don’t understand why this happens.
Example 1: (35 seconds)
[TestMethod]
public void TestGetDataOutsideUsing()
{
const int count = 10000;
var operations = new List<int>();
for (var i = 0; i < count; i++)
operations.Add(i);
using (var proxy = new BasicHttpServiceClient())
{
var timer = Stopwatch.StartNew();
System.Threading.Tasks.Parallel.ForEach(operations, x => { proxy.GetData(x.ToString(CultureInfo.InvariantCulture)); });
timer.Stop();
Console.WriteLine("{0:###0.0000000} ms", timer.ElapsedMilliseconds);
Console.WriteLine("{0:###0.0000000} per millisecond", (count / (decimal)timer.ElapsedMilliseconds));
Console.WriteLine("{0:###0.0000000} per second", (count / ((decimal)timer.ElapsedMilliseconds / 1000)));
Console.WriteLine("{0:###0.0000000} per minute", (count / ((decimal)timer.ElapsedMilliseconds / 1000 / 60)));
}
}
Example 2: (6.2 seconds)
[TestMethod]
public void TestGetDataInsideUsing()
{
const int count = 10000;
var operations = new List<int>();
for (var i = 0; i < count; i++)
operations.Add(i);
var timer = Stopwatch.StartNew();
System.Threading.Tasks.Parallel.ForEach(operations, x =>
{
using (var proxy = new BasicHttpServiceClient())
{
proxy.GetData(x.ToString(CultureInfo.InvariantCulture));
}
});
timer.Stop();
Console.WriteLine("{0:###0.0000000} ms", timer.ElapsedMilliseconds);
Console.WriteLine("{0:###0.0000000} per millisecond", (count / (decimal)timer.ElapsedMilliseconds));
Console.WriteLine("{0:###0.0000000} per second", (count / ((decimal)timer.ElapsedMilliseconds / 1000)));
Console.WriteLine("{0:###0.0000000} per minute", (count / ((decimal)timer.ElapsedMilliseconds / 1000 / 60)));
}
The only difference between the 1st and 2nd example is the position of the using statement. I would have thought that having the using statement inside the ForEach would take longer, but it actually proved otherwise.
Why is this, and which of the above examples is the accurate way to test this? Am I perhaps going about this test the wrong way?
All I want to do is make 10000 concurrent calls to my service and see how long it takes.
In the first example, there is a single
Proxyobject; in the second example, there are multipleProxyobjects.I think this is not directly linked with the
usingstatement, but how it is used. In the first example,Proxyobject becomes a bottleneck for the parallel operation.