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?

TommyN
  • 2,252
  • 22
  • 19
  • hi, I'm facing the exact same issue here. did you find a solution meanwhile? – DanielG Nov 16 '15 at 14:12
  • 1
    @DanielG: I'm afraid not, but I posted a bug report here: https://connect.microsoft.com/VisualStudio/feedback/details/1932717/asp-net-bug-issue. It would be good if you voted on it and also clicked the "I can too" link (next to Repros). – TommyN Nov 17 '15 at 07:58
  • Ok, will do that. One question: Why don't you use just ByteArrayContent if you have the data already in memory anyway? – DanielG Nov 17 '15 at 14:27
  • Or was that just to demonstrate the issue? – DanielG Nov 17 '15 at 14:45
  • It was just to demonstrate the issue. – TommyN Nov 18 '15 at 15:02
  • 1
    Try adjusting the StreamContent buffer size (https://msdn.microsoft.com/en-us/library/hh193591(v=vs.118).aspx). The default is only 4kb and that will result in many write operations. – Tratcher Dec 02 '15 at 15:18
  • @TommyN out of curiosity, and out of this context, whats the tool you used to monitor request and response timmings? – hiFI Sep 28 '16 at 03:30
  • 1
    @hiFI I use Fiddler. _...but the data takes a long time to arrive as shown by the Fiddler timing info_ – TommyN Sep 28 '16 at 06:09

3 Answers3

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.

Community
  • 1
  • 1
Luke Chang
  • 346
  • 3
  • 7
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.

DanielG
  • 1,217
  • 1
  • 16
  • 37
0

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

binki
  • 7,754
  • 5
  • 64
  • 110
Bala
  • 90
  • 7