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.
- We installed a few a few nuget packages : MiniProfiler, MiniProfiler.EF6 and MiniProfiler.MVC4.
- 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"); }
- 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" />
- To make sure the MiniProfiler shows up on every page, we add this line to _Layout.cshtml
@StackExchange.Profiling.MiniProfiler.RenderIncludes()
- 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)); }
- To profile the MVC controllers, we added the following code to FilterConfig
filters.Add(new ProfilingActionFilter());
- 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.


Leave a reply to Maxime Jobin Cancel reply