I’m going to switch gears here for a post or two and talk about software profiling. For those of you who have never used a profiler before, or have no idea what I’m about to talk about, here’s a quick run-down:
A profiler is an add-on program that will measure the performance of your code. Typically, this software allows the developer to drill-down into slow sections of code to see what is hogging all the CPU cycles. One package I’ve used in the past, and is an excellent profiler, is Redgate’s Ants profiler. It’s worth every penny. However, if you’re like me, you have a limited number of pennies to throw around (yes, Ants is a bit on the pricey side). So I’m always on the lookout for something cheap but easy to use. This is where MiniProfiler comes in.
Miniprofiler is easy to install. It took me about 15 minutes to install it into a demonstration MVC4 application. If you go to the MiniProfiler website: http://miniprofiler.com/, you can follow the directions step-by-step. Use the NuGet package manager to get the versions of MiniProfiler that you need.
For a generic MVC4 program, you’ll need the following setup steps:
1. Install the NuGet packages for MiniProfiler (PM> Install-Package MiniProfiler), the MVC package (PM> Install-Package MiniProfiler.Mvc4), and if you are using Entity Framework 6 (PM> Install-Package MiniProfiler.EF6 -Pre). There is also a package for NHibernate (which I will be testing in a future post). I discovered the NHibernate version when I typed “miniprofiler” in the search box of the NuGet package install window.
2. Next, just like the instructions show you on the MiniProfiler.com website:
@using StackExchange.Profiling;
...
@MiniProfiler.RenderIncludes()
Obviously, in an MVC application, the head and body tags are not shown. Just declare
the using statement at the top of the view and put the RenderIncludes() method at
the bottom.
3. Add this to the global.asax file:
using StackExchange.Profiling;
protected void Application_BeginRequest()
{
if (Request.IsLocal)
{
MiniProfiler.Start();
}
}
I also added this:
protected void Application_EndRequest()
{
MiniProfiler.Stop();
}
4. One last thing for the setup, open your Web.config and add this:
<system.webServer>
<validation validateIntegratedModeConfiguration="false" />
<handlers>
<add name="MiniProfiler" path="mini-profiler-resources/*" verb="*"
type="System.Web.Routing.UrlRoutingModule" resourceType="Unspecified"
preCondition="integratedMode" />
Install the MiniProfiler using NuGet
(PM> Install-Package MiniProfiler.EF6 -Pre)
Now open your Global.asax file and add the following using statement to the top:
using StackExchange.Profiling.EntityFramework6;
var query = (from p in db.people
select p.first).ToList();
and the second query looks like this:
var query = (from p in db.people
select p).ToList();
The profiler spits out the actual SQL that was generated by my LINQ queries, which can be convenient to determine what is going on at the SQL server level. You can also copy the SQL text and execute it in the MS SQL server console window to verify what is going on.
Next time, I’m planning to try out the NHibernate version. I’ll be setting up a demo NHibernate project, populate with a large list of data (maybe I’ll copy one of my previous NHibernate projects) and I’ll show how the MiniProfiler behaves.