Question

I have problem with my app. Loading time varies from 500 ms up to 5 seconds and in extreme situations 10 seconds which look little strange for me as I am new in web development. Loading my page look like this:

  1. User click
  2. Before entering controller check if user has permission to do so
  3. Ask Active directory for credentials and ask my database for rights
  4. Call method from controller Load page or show error

Can AD server cause such a delay?

Even if I turn off authentication pages are loading in 3 seconds which can by boring when you see "waiting for localhost" sign :)

below sample which take average 3 seconds to load without authentication. Controller method:

public ActionResult Index(string name, string surname ,string deviceName, string deviceManufacturer)
    {
        var deviceusages = db.DeviceUsages.Include(d => d.DeviceInstance).Include(d => d.Storage).Include(d => d.User).Where(w=>w.UserId!=6);
        if(name!="" && name!=null)
        {
            deviceusages = deviceusages.Where(w => w.User.Name.Contains(name));
        }
        if (surname != "" && surname != null)
        {
            deviceusages = deviceusages.Where(w => w.User.Surname.Contains(surname));
        }
        if (deviceName != "" && deviceName != null)
        {
            deviceusages = deviceusages.Where(w => w.DeviceInstance.Device.Name.Contains(deviceName));
        }
        if (deviceManufacturer!= "" && deviceManufacturer != null)
        {
            deviceusages = deviceusages.Where(w => w.DeviceInstance.Device.Manufacturer.Contains(deviceManufacturer));
        }
        return View(deviceusages.ToList());
    }

And cshtml page:

<table class="table">
<tr>
    <th>
        @Html.DisplayNameFor(model => model.DeviceInstance.Device.Name)
    </th>
    <th>
        @Html.DisplayNameFor(model => model.DeviceInstance.Device.Manufacturer)
    </th>
    <th>
        @Html.DisplayNameFor(model => model.Storage.Name)
    </th>
    <th>
        @Html.DisplayNameFor(model => model.User.Name)
    </th>
    <th>
        @Html.DisplayNameFor(model => model.User.Surname)
    </th><th></th>
</tr>
@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.DeviceInstance.Device.Name)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.DeviceInstance.Device.Manufacturer)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Storage.Name)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.User.Name)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.User.Surname)
        </td>
        <td>
            @Html.ActionLink("Details", "Details", new { id=item.DeviceInstanceId }) 
            @if (item.UserId == 1)
            {
                @Html.Raw("| ")
                @Html.ActionLink("Claim", "ClaimDevice", new { id = item.DeviceInstanceId })
            }           
        </td>
    </tr>
}
</table>

And now my question I've done something wrong or my loading times are ok and I'm just newbie

Was it helpful?

Solution

Initially, this started out as a comment, but I think I can form it into an answer for you. First, you are not really doing anything wrong, but I can point out places that your code speed could be improved.

  1. You are joining a decent amount of tables. This will cause SQL to be slower than a single table/a few joined tables.

  2. The .Contains() method corresponds to LIKE in SQL syntax. This is slower than an actual value match (=). You could entertain the possibility of providing drop down lists to your user for searching so that they a) don't need to know the names and b) they can quickly see the possible values.

  3. "Can AD cause the slowdown" - possibly. It depends on how well your infrastructure is maintained and laid out and more on your network guys. If your AD/LDAP server is under a heavy load (because its running Exchange and SQL and DNS, etc), it can be slow in responsiveness, but this is not a code issue.

  4. Most likely, your debugger is what is actually causing your slow down. I have noticed that EF 6 runs much slower with the debugger attached than previous versions. As noted earlier, our production app runs at around 2s in localhost and 150-200ms in release mode on a not stellar web server hardware wise.

  5. Instead of asking AD each time, perhaps you ask once and then cache the AD results in a cookie or something similar. This would cut down on the requests outside of your app for each request.

So, in summary, no - you do not appear to be doing anything wrong. You do have room for improvement in my opinion, but I would not worry too much about it until you actually can get it on a server and out of localhost and test your application.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top