0

I have a controller that has a controller method looking like:

[Route("GetDashboard")]
    [HttpPost("GetDashboard")]
    public async Task<IActionResult> PostDashboard([FromBody]Period period)
    {
        Debug.WriteLine("PostDasboard in");  
        var eventTimer = new EventTimer();

        var forDate = period.FromDate;
        var q = GetMySales(new ReportFilterModel { FromDate = forDate.AddDays(-31), UntilDate = forDate });

        eventTimer.NextEvent("PostDashboard");

        var r = await (from i in q
                group i by i.Order.Shift.Site into s
                select new
                {
                    id = s.Key.Key,
                    name = s.Key.Name,
                    days = from i in s
                           group i by i.Order.Shift.AltTrade into d
                           select new
                           {
                               date = d.Key.GetEpochTicks(),
                               total = d.Sum(i => i.Sell),
                               qty = d.Sum(i => i.Quantity)
                           }

                }).ToListAsync();


        eventTimer.NextEvent($"Got List: {r.Count()}");

        var result = Json(r);



        if (_timing || (eventTimer.Elapsed >= 5000)) // More than  sec..
            await _eventLogger.DebugAsync("Timing", eventTimer.Final, null);


        Debug.WriteLine("PostDasboard Out");
        return result;
    }

I've added some bits to track the overall time. The method itself takes about 3 sec. After that it can take an additional 12 to 30 sec before the response finishes: Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker:Information: Executed action OnlineUsers.Controllers.CloudTradeController.PostDashboard (OnlineUsers) in 35352.5891ms Microsoft.AspNetCore.Hosting.Internal.WebHost:Information: Request finished in 35399.931ms 200 application/json; charset=utf-8};

About the only thing that is different for the delayed methods is nested Json. Does anybody have seen similar delays? are the any other tools available to get closer to where the delay is.

Andre'
  • 1

2 Answers2

0

The delay is just the slow serialization of the object. The delay can be moved to inside the method as below. So the question is more: why does it take 30+ sec to serialization 310 objects in a nested list (10 x 31) ?? Am I using wrong settings somewhere?

 var result = Content(JsonConvert.SerializeObject(r));
Andre'
  • 1
0

The problem is around the fact that the 'days' part, is still only IEnumerable. So, only when serializing, it is fetched from the db. By adding ToList() they can be materialized sooner. However this just moves the (same) time from serializing back to the query. Looks like linq not so good for nested totaling. Or is there a better pattern?

Andre'
  • 1