title: ASP.NET Core MVC Slow response generation
body: <p>I have an ASP.NET Core MVC web application that has an endpoint which returns some data as a json. The onlly issue is that my data is around 5 MBs of raw (non-idented) JSON, and the response takes a really long time.</p>
  
  <p>I set up some metrics and found that, on average, the processing that my application does takes <strong>around 30ms</strong>. Yet the whole response is returned after <strong>over 250ms</strong>.</p>
  
  <p>I am testing with a local instance of the app running on my machine and sending a get request with postman, so networking delay is minimal.</p>
  
  <h2>Here is what my controller looks like</h2>
  
  <pre><code>    [HttpGet("getData")]
      public IActionResult GetData()
      {
          Stopwatch sw = Stopwatch.StartNew();
          long a, b, c;
  
          var data = this._cacheInternal.GetValidDataSet();
          a = sw.ElapsedMilliseconds;
  
          sw.Restart();
          var processedData = this.ProcessData(data, false);
          b = sw.ElapsedMilliseconds;
  
          sw.Restart();
          var serialized = JsonConvert.SerializeObject(processedData);
          c = sw.ElapsedMilliseconds;
  
          this._log.Info($"Data: {a} ms. Processing {b} ms. Serialization {c} ms.");
  
          return this.Ok(serialized);
      }
  </code></pre>
  
  <h2>Here is what I have discovered when examining the AspNetCore logs.</h2>
  
  <pre><code>info: Microsoft.AspNetCore.Hosting.Internal.WebHost[1]
        Request starting HTTP/1.1 GET http://localhost:5000/api/status/getData
  info: Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker[1]
        Route matched with {action = "GetData", controller = "Status"}. Executing action DemoApp.Controllers.StatusController.GetData (DemoApp)
  info: Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker[1]
        Executing action method DemoApp.Controllers.StatusController.GetData (DemoApp) - Validation state: Valid
  info: Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker[2]
        Executed action method DemoApp.Controllers.StatusController.GetData (DemoApp), returned result Microsoft.AspNetCore.Mvc.OkObjectResult in 31.4532ms.
  info: Microsoft.AspNetCore.Mvc.Infrastructure.ObjectResultExecutor[1]
        Executing ObjectResult, writing value of type 'System.String'.
  info: Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker[2]
        Executed action DemoApp.Controllers.StatusController.GetData (DemoApp) in 69.2546ms
  info: Microsoft.AspNetCore.Hosting.Internal.WebHost[2]
        Request finished in 175.2726ms 200 text/plain; charset=utf-8
  </code></pre>
  
  <p>The logging of the stopwatch for this request is the following : </p>
  
  <p><code>Data: 1 ms. Processing 19 ms. Serialization 10 ms.</code></p>
  
  <p>And as you can see from the logs for my controller: </p>
  
  <p><strong>... returned result Microsoft.AspNetCore.Mvc.OkObjectResult in 31.4532ms</strong>.<br>
  This matches what I have with my stopwatch metrics and is the amount of time that the app is actually executing my code.</p>
  
  <p>From there on there are two more points that ASP takes over:</p>
  
  <p><strong>Executed action DemoApp.Controllers.StatusController.GetData (DemoApp) in 69.2546ms</strong></p>
  
  <p><strong>Request finished in 175.2726ms 200 text/plain; charset=utf-8</strong></p>
  
  <p>So my question is how can I find out what is happening in those two time points after ASP takes the result I pass to <code>this.Ok()</code> that takes so much time (and fix it)</p>
  
  <p>Again, the response I return is quite large (for this example 1.2MB raw JSON). But I don't understand what is causing such a long delay (given that the serialization I do only takes 10ms)</p>
  
  <p>I am using ASP.NET Core MVC and targeting .NET Core 2.1. The app is hosted with Kestrel.</p>