3

We have a MVC 4 Web application. I used miniprofiler to check how long it is taking and it shows that the render of a view with three partial views is taking a very long time. I tried moving the information in the partial views directly onto the page and that didn't make a difference in the times at all. What should I do to reduce the render time? Any tips on where I should start looking? As you can see, the render is taking 48 seconds.

                                         **| duration | from start(ms)**
|http://localhost:61380/CaseContacts     | 653.7    | +0.0 
|Controller: CaseController.Contacts     | 11.9     | +641.1 
|    Contacts view - not an ajax request | 0.3      | +651.7
|        populating view model           | 0.0      | +652.1
|    getting list of contacts            | 374.4    | +652.1
|    Find: Contacts                      | 499.8    | +1033.6
|    Render: Contacts                    | 48400.8  | +1535.2

**| client event           |duration(ms)| from start(ms)**
|Request start           |            | +2.0
|Response                |   1.0      | +52245.0
|Dom loading             |            | +52247.0
|scripts                 | 390.0      | +52315.0 
|Dom Content Loaded Event| 29.0       | +52707.0
|Dom Interactive         |            | + 52707.0
|Load Event              |            | + 52760.0
|Dom Complete            |            | + 52760.0

Update:

public IQueryable<T> FindQueryable(Expression<Func<T, bool>> predicate, Func<T, Object> orderBy)
    {
        return this.db.GetAll<T>().Where(predicate).OrderBy(orderBy).AsQueryable<T>();
    }
public class ContactRepository : Repository<USRContact>, IContactRepository<USRContact>
{
    public IList<ContactNameViewModel> GetAll(int fieldOfficeID, string searchText = "")
    {
        if (searchText.Length > 0)
            return Mapper.Map<IList<USRContact>, IList<ContactNameViewModel>>(this.FindQueryable(x => (x.FieldOfficeID == fieldOfficeID) &&
            (x.FormalName.Contains(searchText) || x.PreferredName.Contains(searchText) || x.Alias.Contains(searchText) || x.Pseudonym.Contains(searchText)), x => (x.STMGender.Gender)).ToList());
        else
            return Mapper.Map<IList<USRContact>, IList<ContactNameViewModel>>(this.FindQueryable(x => (x.FieldOfficeID == fieldOfficeID)).ToList());
    }

Then my controller is loading the results onto a viewmodel and then:

@Html.Partial("~/Views/Shared/Contacts/ContactNameList.cshtml", Model.Contacts)

The partial view contains the following code:

@model IList<Casenator.Web.Models.Contact.ContactNameViewModel>
<ul id="NameList" style="margin-left:0">
    @foreach (var item in Model)
    {
        @*<li>@Html.RouteLink(@item.FullName, "Contacts", new { id = item.ContactID })</li>*@
        <li>@Ajax.RouteLink(item.FullName, "Contacts", new { id = item.ContactID }, new AjaxOptions { UpdateTargetId = "BasicInfo", OnSuccess="InitializeComboBoxes", OnBegin="LoadContact(" + item.ContactID + ")" }) </li>
    }
</ul>

Any help will be appreciated! Thanks, Safris

safriss
  • 93
  • 2
  • 7

1 Answers1

2

What you are returning from query is important. If you are returning IEnumerable It would be better to reurn IQueryable. You may see this question it may help you in your situation

I don't think automapper like tools are designed to map many recoreds. you may try to map the object manually

Community
  • 1
  • 1
Tassadaque
  • 8,129
  • 13
  • 57
  • 89
  • To find the list of contacts, it is taking 374.4 ms . It seems like the render is taking longer (48400.8ms). I use automapper to translate from domain model to view model. So I am unable to translate from IQueryable to IQueryable. – safriss Mar 04 '13 at 22:54
  • Then you may try to map the it mannually ot see if automapper i scausing issue – Tassadaque Mar 05 '13 at 04:34
  • Generic repository may not be suitable in some situation – Tassadaque Mar 05 '13 at 05:00
  • Tassadaque is right, avoid using IEnumerables and use IQueryables instead when querying data from the db. ToList call is a massive performance hit as is Count() from List. You could try translating from dm to vm like: IQueryable c = db.Contacts.Select(p => new ContactViewModel(...)) and see how that turns out. – Tomi Lammi Mar 05 '13 at 10:10
  • I only have 6 rows returned back from the db. I removed the automapper and tried it without the automapper and directly using db.Contacts.Select(p => new ContactViewModel(...)) as suggested by @sormii and it still takes more than 36000ms. Any other things that I should look at? – safriss Mar 05 '13 at 14:33
  • @Tassadaque: I started a stop watch on ActionExecuting and stopped it at ActionExecuted and it is 451 ms. I started another stop watch on ActionExecuting and stopped it at Result Executed and the time elapsed is over 42000. Does that then rule out the automapper problems? Does it have to do with the MVC view engine? – safriss Mar 05 '13 at 14:58
  • you may try glimpse nuget package to see what is happening on server. a quick troubleshooter may be firegub net tab to point out the troubling spot. Although mini profiler gives a good idea but my try ohter things problem seem to be somewhare else – Tassadaque Mar 05 '13 at 17:50