4

I have an asp.net view/page (dynamic) that is built up of nested Html.RenderPartials. The view is sent an appropriate viewmodel to render.

Firebug says the GET response for the HTML is 9.89 secs for 9.5KB. To compare the FAQ page (static html) of the same site is 1.3 secs for 17K.

At first I thought it was the SQL Entity back end slowing things down due to the complexity of the viewmodel but it seems to construct the viewmodel in less than 1 sec according to my logs.

Any ideas why the MVC view is taking so long to render please and how I can speed this up? I'm considering partial loading via ajax.

(btw I Gzip and use CDN's etc - I have yslowed the whole site to death)

Edit:

Added timers (Stopwatch) to OnActionExecuting/OnActionExecuted and OnResultExecuting/OnResultExecuted.

09/12/2010 18:39:20: Controller: Profile Action: Index Elapsed time: 680.6431 - Action

09/12/2010 18:39:29: Controller: Profile Action: Index Elapsed time: 9202.063 - Result

9 seconds for the framework to render the view.

Anthony
  • 61
  • 1
  • 5
  • Is there any lazy loading going on with objects being passed to the view model? – Castrohenge Sep 12 '10 at 15:16
  • No lazy loading on any objects. – Anthony Sep 12 '10 at 15:18
  • Did you at Stopwatchs anywhere and time anything? You question has little information that could help us diagnose the problem. – John Farrell Sep 12 '10 at 15:58
  • Let me know where you want timers, and I will add them. I have timed the DB operations to construct the ViewModel and its sub 1 sec, so I'm thinking the 9.89 response time is the MVC view engine building the view. Where would be the best place to time this please ? – Anthony Sep 12 '10 at 16:14
  • 1
    Just found this http://stackoverflow.com/questions/1588010/asp-net-mvc-view-engine-performance which I'm going through. – Anthony Sep 12 '10 at 16:36
  • Some more info about your "nested Html.RenderPartials" - how deep is your nesting, is there recursion, how many calls to `RenderPartial`, etc. I've had lousy performance experience with recursive partial views. – bzlm Sep 13 '10 at 16:36
  • Are you letting MVC 'find' your partials using the standard search mechanism? Having experienced similar issues, I have found that supplying full path and extension on your RenderPartials speeds up view rendering sugnificantly. – Clicktricity Sep 13 '10 at 20:10
  • @Clicktricity Yes I have tried supplying the full path but it didn't make a huge difference in release mode. Apparently release mode does this for you anyway. Debug mode is a different matter however. Believe what you will :) – Anthony Sep 14 '10 at 14:39
  • @bzlm the RenderPartials are only 2 deep. I think the issue was extracting the object from the VM to send to the partial as it was also wanting to maintain entity management for relationships, hence me being able to turn it off and solving my issue. – Anthony Sep 14 '10 at 14:44
  • @Anthony you should split out your 'question' from your answer and actually create an answer for it. – George Stocker Sep 14 '10 at 14:52
  • @Anthony So, this had nothing to do with partial views, and could easily have been solved by a little profiling. :) – bzlm Sep 14 '10 at 15:09
  • Yes too true; must learn how to use this tool in more detail to become more efficient. There is very little on the net about it I find. – Anthony Sep 14 '10 at 15:31
  • @Anthony Very *little* about using profiling to find performance bottle necks on the net? Are we talking about the same net here? – bzlm Sep 14 '10 at 16:40
  • There is heaps on profiling in general yes I agree, and many good tools, but VS2010 built in tool I find hard to find many good tutorials. How about you, do you know of any please? – Anthony Sep 14 '10 at 18:05
  • Actually in saying that maybe I should use JetBrains dotTrace as a replacement; and a solution to my poor google techniques :) – Anthony Sep 14 '10 at 18:09

1 Answers1

2

Problem Solved

Firstly thanks to you all for your suggestions. I followed each suggestion again and again until I found the issue. This is what was wrong, and maybe someone could clarify for others.

VS2010 Performance Wizard was saying that each object being passed to the PartialViews was taking up huge CPU time and I presumed it to be the partial as I have read they may have issues.

foreach (ProfileComment item in Model)
{
    Html.RenderPartial("UserActivityComment", item);
}
...
Friends friend = Model.Friends.Where(e => e.ID == activity.ActionID).FirstOrDefault();
if (friend.FriendsProfile.UserName != Page.User.Identity.Name)
{
    Html.RenderPartial("UserActivityFriend.ascx", friend);
}

The ProfileComment and Friends object (plus others) are part of a ViewModel I generate and pass to the page. Now the VM is generated via the Entity Framework in less than 0.3 secs So I presumed all was fine with the VM.

The huge delay came when I wanted the view to Process it. The Model in the 'for loop' was flagged, and so was FirstOrDefault by the Performace Wizard.

Hmm strange, the model is constructed rapidly but not processed rapidly. Solution:

_entities.Friends.MergeOption = MergeOption.NoTracking;
_entities.ProfileComment.MergeOption = MergeOption.NoTracking;

I think the issue was extracting the object from the VM to send to the partial as it was also wanting to maintain entity management for relationships.

More info at Aia Research

and blogs.microsoft.co.il/blogs/gilf/archive/2009/02/20/disabling-change-tracking-in-entity-framework.aspx

Please feel free to expand on this in more detail. Btw the performance increase was huge!

Anthony
  • 61
  • 1
  • 5