20

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?

8
  • hi, I'm facing the exact same issue here. did you find a solution meanwhile? Commented Nov 16, 2015 at 14:12
  • 1
    @DanielG: I'm afraid not, but I posted a bug report here: connect.microsoft.com/VisualStudio/feedback/details/1932717/…. It would be good if you voted on it and also clicked the "I can too" link (next to Repros). Commented Nov 17, 2015 at 7:58
  • Ok, will do that. One question: Why don't you use just ByteArrayContent if you have the data already in memory anyway? Commented Nov 17, 2015 at 14:27
  • 1
    Try adjusting the StreamContent buffer size (msdn.microsoft.com/en-us/library/hh193591(v=vs.118).aspx). The default is only 4kb and that will result in many write operations. Commented Dec 2, 2015 at 15:18
  • 1
    @hiFI I use Fiddler. ...but the data takes a long time to arrive as shown by the Fiddler timing info Commented Sep 28, 2016 at 6:09

3 Answers 3

31

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.

Sign up to request clarification or add additional context in comments.

Comments

5

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.

Comments

0

When you compile the projects, try changing from debug to release. It's a loong shot, but would surely increase performance a bit.

1 Comment

No difference I'm afraid.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.