Modelon Solutions

A fresh take on your ideas

MiniProfiler

Published by

on

We were doing a little performance related work on a ASP.NET MVC website. There were a few pages where we transmitted a lot of unnecessary data (binding a big model to KendoUI if you don’t use the data is bad bad bad) and a few SQL n+1 scenarios…

All in all, the performance was pretty good, and the site had only a few users who used it on an intranet. This time around though, we are exposing the site to the internet for hundreds of people to use from everywhere on earth, and that changed a few things.

Then we discovered MiniProfiler. which is available on Nuget. It’s an extendable system that allows you to track the length of time used by different part of your system, and shows the information in a non invasive way.

miniprofilerimg2

 This is how we got going:

  1. We installed a few a few nuget packages : MiniProfiler, MiniProfiler.EF6 and MiniProfiler.MVC4.
  2. We added the following code to Application_Start()
    MiniProfiler.Settings.Results_Authorize = IsUserAllowedToSeeMiniProfilerUI;
    MiniProfilerEF6.Initialize();
    private bool IsUserAllowedToSeeMiniProfilerUI(HttpRequest httpRequest)
    {
        var principal = httpRequest.RequestContext.HttpContext.User;
        return principal.IsInRole("admin");     
    }
  3. We had to modify our web.config to have the following in the handlers section of system.webservice section
    <add name="MiniProfiler" path="mini-profiler-resources/*" verb="*" type="System.Web.Routing.UrlRoutingModule" resourceType="Unspecified" preCondition="integratedMode" />
    
  4. To make sure the MiniProfiler shows up on every page, we add this line to _Layout.cshtml
    @StackExchange.Profiling.MiniProfiler.RenderIncludes()
    
  5. To profile the MVC views, we added the following code to Application_Start
    var copy = ViewEngines.Engines.ToList();
    ViewEngines.Engines.Clear();
    foreach (var item in copy)
    {
        ViewEngines.Engines.Add(new ProfilingViewEngine(item));
    }
  6. To profile the MVC controllers, we added the following code to FilterConfig
    filters.Add(new ProfilingActionFilter());
    
  7. Anywhere we had a lot of processing, we isolated each section like this
    using (MiniProfiler.Current.Step("Calculate sales"))
    { ... }

We were amazed at how easy all of this went, and also at the result. The EF6 plugin automatically detects n+1 or duplicate queries and so that gave us a big speed boost in our optimisation phase.

One response to “MiniProfiler”

  1. Maxime Jobin Avatar

    Great post about the profiler. I usually use an internal class I coded to do that and output the results in the Output console. It looks like you are getting a better breakdown of what is happening and where. Thank you for this.

    Like

Leave a comment

Previous Post
Next Post