Using the ASP.NET MVC Mini-Profiler with LightSpeed

The ASP.NET MVC Mini-Profiler, created by the Stack Overflow team, describes itself as “a simple but effective mini-profiler for ASP.NET MVC and ASP.NET.” It’s a handy little tool which, when you enable it, shows a discreet little popup at the top of the page which you can expand to see the timings of various steps of your code. What’s particularly nice about the MVC Mini-Profiler is that it can also profile your SQL, alerting you to duplicate queries which are the classic sign of an N+1 problem.

I’m pleased to announce that in the latest nightly build of LightSpeed we’ve provided a new hook that enables you to use the MVC Mini-Profiler on your LightSpeed queries. LightSpeed sports a bunch of options that you can use to optimise your querying, but it’s always possible that one of your buffoonish colleagues (not you, of course) has missed somewhere that they need to be used. The profiler can warn you about this…

…and show you the duplicate queries…

…and with LightSpeed’s spiffy eager load and named aggregate facilities the fix is just a click away.

So how do you get the profiler working with LightSpeed? The profiler works by wrapping all commands and connections with its own command and connection classes that track timing, SQL text, and so on. The new LightSpeedContext.Interceptor property allows you to intercept LightSpeed’s internal object factories and do the wrapping that the profiler needs. Here’s the code for the interceptor itself:

public class MiniProfilerInterceptor : Interceptor
  public override IDbConnection CreateConnection(Func<IDbConnection> baseCreator)
    var realConnection = baseCreator();
    return ProfiledDbConnection.Get((DbConnection)realConnection, MiniProfiler.Current);
    // or return new ProfiledDbConnection(...) depending on which version you are using
  public override IDbCommand CreateCommand(Func<IDbCommand> baseCreator)
    var realCommand = baseCreator();
    return new ProfiledDbCommand((DbCommand)realCommand, null, MiniProfiler.Current);

And you can attach the profiler through the web.config using the new interceptorClass attribute:

  <add name="Development"

(You can also attach it through code using the LightSpeedContext.Interceptor property, but you probably wouldn’t do this for the Mini-Profiler interceptor.)

And that’s it! Of course you can add additional profiling information via the Mini-Profiler API, as shown on the Mini-Profiler home page, but to get LightSpeed timing and SQL tracking, this is all you need!

Here’s a sample which demonstrates LightSpeed working with the Mini-Profiler. See the readme.txt file in the zip file for database setup instructions. The zip file includes the required build of the free LightSpeed Express so you can run the sample standalone, but for your own Web apps, remember that you’ll need the latest nightly to get the Interceptor property.


Have fun and let us know what you think!

UPDATE: We’ve been alerted to a breaking change in the Mini-Profiler where you should now use new ProfiledDbConnection instead of ProfiledDbConnection.Get. If you’re using the latest build of Mini-Profiler, you may need to update the sample code.

Tagged as LightSpeed

2 Responses to “Using the ASP.NET MVC Mini-Profiler with LightSpeed”

  • Thanks for adding this! I particularly like how you’ve implemented interceptor support in case we want to extend it to other pipeline support tools (like Glimpse, for example). Love it!

  • Very nice, still works great with the latest nightly of Lightspeed and the latest MiniProfiler

  • Leave a Reply


Join our mailer

You should join our newsletter! Sent monthly:

Back to Top