I'm developing a web service using ASP.NET MVC3. One of the methods of the HTTP API receives an image from the body of a POST request and needs to store it on the disk for further processing. The beginning of the method is like this:
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
Interlocked.Increment(ref _generateCount);
byte[] fileBuffer;
using (Stream inputStream = Request.InputStream)
{
if (inputStream.Length == 0)
{
Trace.WriteLine("Submitted file is empty", "Warning");
inputStream.Close();
Interlocked.Decrement(ref _generateCount);
return new HttpStatusCodeResult(400, "Content is empty");
}
fileBuffer = new byte[inputStream.Length];
inputStream.Read(fileBuffer, 0, (int)inputStream.Length);
inputStream.Close();
}
stopwatch.Stop()
... (storing the fileBuffer on disk & quite CPU intensive processing on the file)
I'm hosting the service in a small Azure instance.
Now the strange behavior I have is when I issue parallel requests to the service. Let's say I issue a first request, and a second one 5 seconds later. As you see I'm using a Stopwatch to monitor the performance. For the first request, the ElapsedTime will be very small (less than one second) but for the second one, it usually reads approx. 14 seconds!
Note that the average processing time for a single request is approx. 25 seconds (and gets to 40+ when multiple requests are being processed), so I get my input stream read 14 seconds later, although the first request hasn't finished.
How can I explain this delay ?
Thanks