The problem to solve
Sitecore has some tools for performance profiling that we can use out-of-the-box, like /sitecore/admin/stats.aspx or the Debug mode in the Page Editor. But, in my opinion, they’re useful when you already know you have a problem, and even then, they’re not really straight-forward to use. But how can we detect in real-time that we have a performance problem? I suggest using MiniProfiler.
The tool that will solve it
MiniProfiler is a light-weight plugin for ASP.NET applications that add live profiling capabilities to our application. Apart from the basic plugin (ASP.Net WebForms), there are also several extensions for MVC, EF, Ruby, Node.js, etc.
MiniProfiler will add an unobtrusive layer on the top left side of the website with the time taken to render it. Then we can open it to see the whole breakdown of steps.
This way we can easily see if a concrete page is taking more time than necessary, and then try to dig deeper in the components that conform that page, using Sitecore profiling or our own methods. Please refer to MiniProfiler documentation at http://miniprofiler.com/ or check the sample project at GitHub (https://github.com/MiniProfiler/dotnet/tree/master/Sample.WebForms) to see how you can wrap your own profiled sections in your code, and see them at the output layer.
How to install MiniProfiler in Sitecore
As MiniProfiler is deployed through Nuget, it seems that getting it up and running in Sitecore should be something easy. But there are a couple of things to do before this comes true.
Installing MiniProfiler plugin
The first step is to get the MiniProfiler package to our website. To do so, we will open the “Package Manager Console” in Visual Studio and select our Website project in the “Default project” dropdown.
We can use MiniProfiler capabilities in other projects that are part of the solution (data layer, services, etc.), where it will probably make more sense than in the top layer website. Then we will need to install the package in those projects as well.
In this case we’re installing the basic package for WebForms, as this has been tested against a Sitecore 6.6 installation. We can use the specific packages for MVC if needed.
We need to add three small changes to our website to have MiniProfiler running:
We will copy the configuration from the sample website: https://github.com/MiniProfiler/dotnet/blob/master/Sample.WebForms/Global.asax.cs
I suggest modifying the method void InitProfilerSettings() and including these lines:
// Sitecore specific URLs
So we avoid messing up with static files and Sitecore specific urls timing.
1. Make sure the <modules> section looks like this:
2. Edit the “IgnoreUrlPrefixes” setting, and add the following url, /mini-profiler-resources. It should look like:
<setting name="IgnoreUrlPrefixes" value="/mini-profiler-resources|/sitecore/default.aspx|/trace.axd|..." />
Finally, we must add the frontend resources (CSS/JS) that render the layer in the site. To do so, we have to add the following line to our main layout, at the end, just before the </body> tag:
<%= StackExchange.Profiling.MiniProfiler.RenderIncludes(useExistingjQuery:false) %>
This is the WebForms syntax. Please refer to the documentation if you’re using MVC.
MiniProfiler is a widely recommended way to include basic profiling in our website projects, and it’s really helpful to have your code under control.
The main trick to get it working with Sitecore was adding the MiniProfiler url to the ignored Urls by Sitecore, so Sitecore doesn’t think it’s an item path and return a 404 error.
This has been tested with Sitecore 6.6, but it will probably work as well with newer versions.
This post is an update from the original post in Spanish at my personal blog.