MiniProfiler - Installation and Setup
The primary reason I started this web site was to share with you the things I come across in my day job, the first batch of articles I am going to write will be around the tools I find invaluable in my role.
So first and foremost, let’s take a look at MiniProfiler.
MiniProfiler is a tool created and used by the StackExchange group of websites and is used for profiling your .NET and Ruby applications.
Whilst in your development environment you can use it to get an overlay on your page detailing the code execution of your Application. For those of you that have ever tried to identify bottlenecks in your applications you can immediately see how useful this can be. Take a look at this screenshot from the Jambr blog page.
As you can see, the steps needed to render my page, and most importantly the SQL that was executed in the process are clearly detailed out here, you can even click on the query to view the detail, for example:
I’ll go into all of this a little more in later articles, but for now let’s look at setting up and configuring MiniProfiler against a .NET 4.5 MVC 4 Application which uses Entity Framework 5 for its database connections.
Installation
Installation is simple and is all done through the Package Manager Console integrated into Visual Studio.
- Open the Package Manager Console (Tools -> Library Package Manager -> Package Manager Console.
- Type “Install-Package MiniProfiler” and press enter:
- Package Manager will now download and install MiniProfiler and add the required references to your project.
- As I mentioned previously, we’re going to be configuring MiniProfiler to profile our Entity Framework database context, in order to do so, we’ll also need the MiniProfiler.EF package, which is installed in exactly the same way, with
Install-Package MiniProfiler.EF
- Now open your Global.asax.vb file, at the top of your Sub Application_Start, add
MiniProfilerEF.Initialize_EF42()
This ensures that any connections created and used by Entity Framework are captured and logged into MiniProfiler like in my screenshot above. - In the same file, add the following to the top of your Application_BeginRequest
MiniProfiler.Start()
- And at the top of your Application_EndRequest
MiniProfiler.Stop()
- Almost done, I promise. We now need to add a piece of code to the page you want to see the profiler on. Personally I have this on my Base.Master, the parent master page for all others, that way it’s available on every page. The code you need to add, in your tag is
<%:MiniProfiler.RenderIncludes %>
- You may need to add a reference to the namespace at the top of your page like so
<%@ Import Namespace="StackExchange.Profiling" %>
And that’s it. It’s worth noting that if MiniProfiler.Start()
isn’t called, the .RenderIncludes function is smart enough not to clutter your browser with the JavaScript files that are required. I personally wrap my .Start()
in a Debugger.IsAttached
statement, therefore I only show the profile in my local development environment.
Run your application and see what happens…
Oh wait, Karl, you lied – it doesn’t work and I’m getting the following error when running MVC4!!
Failed to load resource: the server responded with a status of 404 (Not Found) http://*/mini-profiler-resources/results
This article wouldn’t be interesting unless it solved at least one annoyance now, would it? To fix the above error you need to add the following to your Web.Config
<system.webServer>
<handlers>
<add name="MiniProfiler" path="mini-profiler-resources/*" verb="*" type="System.Web.Routing.UrlRoutingModule" resourceType="Unspecified" preCondition="integratedMode" />
This sorts out the routing issue which appeared in MVC4. Now try again, and you’ll be sorted!
Summary
MiniProfiler is a very powerful tool, I will go into the more advanced features of it in a later Article.