Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET Web API 2 - StreamContent is extremely slow

Tags:

We've ported a project from WCF to Web API (SelfHost) and during the process we noticed a huge slowdown when serving out a web application. Now 40-50 seconds vs 3 seconds previously.

I've reproduce the issue in a simple console application by adding the various Nuget pacakges for AspNet.WebApi and OwinSelfHost with the following controller:

var stream = new MemoryStream();
using (var file = File.OpenRead(filename))
{
    file.CopyTo(stream);
}
stream.Position = 0;

var response = Request.CreateResponse(System.Net.HttpStatusCode.OK);

/// THIS IS FAST
response.Content = new ByteArrayContent(stream.ToArray());
/// THIS IS SLOW
response.Content = new StreamContent(stream);

response.Content.Headers.ContentType = new MediaTypeHeaderValue(System.Web.MimeMapping.GetMimeMapping(filename));            
response.Content.Headers.ContentLength = stream.Length;

As you can see from the code the only difference is the usage of StreamContent (slooooow) vs ByteArrayContent.

The application is hosted on a Win10 machine and accessed from my laptop. Fiddler shows that it takes 14 seconds to get a single 1MB file from the server to my laptop using StreamContent while ByteArrayContent is less than 1s.

Also note that the complete file is read into memory to show that the only difference is the Content class used.

The strange thing is that it seems that its the transfer itself that is slow. The server responds with the headers quickly/immediately, but the data takes a long time to arrive as shown by the Fiddler timing info:

GotResponseHeaders: 07:50:52.800
ServerDoneResponse: 07:51:08.471

Complete Timing Info:

== TIMING INFO ============
ClientConnected:    07:50:52.238
ClientBeginRequest: 07:50:52.238
GotRequestHeaders:  07:50:52.238
ClientDoneRequest:  07:50:52.238
Determine Gateway:  0ms
DNS Lookup:         0ms
TCP/IP Connect:     15ms
HTTPS Handshake:    0ms
ServerConnected:    07:50:52.253
FiddlerBeginRequest:07:50:52.253
ServerGotRequest:   07:50:52.253
ServerBeginResponse:07:50:52.800
GotResponseHeaders: 07:50:52.800
ServerDoneResponse: 07:51:08.471
ClientBeginResponse:07:51:08.471
ClientDoneResponse: 07:51:08.471

Overall Elapsed:    0:00:16.233

Does anyone know what's going on under the hood that could explain the difference in behavior?

like image 916
TommyN Avatar asked Oct 07 '15 12:10

TommyN


2 Answers

The solution to my problem for OWIN self hosting was the StreamContent buffer size. The default constructor of StreamContent uses a default value of 0x1000, 4Kb. On a gigabit network, transfer of 26Mb file takes ~7 minutes to complete at rate of ~60Kb/s.

 const int BufferSize = 1024 * 1024;
 responseMessage = new HttpResponseMessage();
 responseMessage.Content = new StreamContent(fileStream, BufferSize);

Modifying the bufferSize to 1Mb now take only seconds to complete the download.

[EDIT] In StreamContent SerializeToStreamAsync does a StreamToStreamCopy, according to this link the performance will differ. A suitable value is probably 80K.

like image 146
Luke Chang Avatar answered Sep 19 '22 19:09

Luke Chang


I'm facing the same issue here, and I think it is related to the Owin self hosting. I just created an Asp.net sample Application and hosted it on IIS. In that case it worked as expected.

The results on my Testsystem while downloading an 80 MB file:

  • with Streamcontent and SelfHosting: ~20 minutes
  • with ByteArrayContent and Selfhosting: < 30 seconds
  • with Streamcontent and IIS Hosting: < 30 seconds

Either there is a configuration setting in ASP.net which I'm missing in my self-hosted project, or there is a bug in owin self-hosting code I guess.

like image 34
DanielG Avatar answered Sep 22 '22 19:09

DanielG