Troubleshoot ASP.NET Performance Problems with StackExchange's MiniProfiler

StackExchange's MiniProfiler tool is great for troubleshooting your ASP.NET MVC performance issues.

Don Kiely

July 3, 2013

4 Min Read
Troubleshoot ASP.NET Performance Problems with StackExchange's MiniProfiler

Related: "ASP.NET Web Development: Get a Glimpse into Your Web Server"

I recently worked on a project that involved some HTTP handlers and modules in an ASP.NET MVC 4 application that included some performance and profiling work. During the course of research for that project, I stumbled on a gem that has evaded my knowledge until now, even though I know now that people such as Scott Hanselman were talking about the tool as far back as 2011. That gem is MiniProfiler. It's developed by the folks who brought us StackExchange, and it's a marvelous resource for getting technical questions answered.

MiniProfiler is a minimal but very effective tool for .NET and Ruby applications. It's targeted primarily for data access performance problems and supports ADO.NET, Entity Framework, LINQ-to-SQL, and other data access technologies. MiniProfiler also has the ability to instrument your code to provide custom performance data during execution. However, don't let the "mini" part of the name fool you. Although the tool has a relatively light impact on your application, this is a complex tool that has several different options and flexibility for helping you fine tune application performance. Best of all, it's free and licensed under the Apache 2.0 license with the source code available on GitHub.

At runtime, MiniProfiler puts an easily accessible but unobtrusive element in the upper-left corner of the page that displays the total loading time for the page, see Figure 1. This is in the default ASP.NET MVC 4 Internet application.

 logo in a gray background |

If you click the box, then you get more detailed information, see Figure 2.

 logo in a gray background |

And this default configuration is just the beginning. There are several different options for configuring how MiniProfiler works in your application, and you can instrument code to gather specific performance information about code that has performance problems. For example, you could change the About action method for the default ASP.NET MVC 3 Internet application's Home controller to the following, which uses some sample code from the MiniProfiler site:

public ActionResult About(){ViewBag.Message = "Your app description page.";var profiler = MiniProfiler.Current;using (profiler.Step("Doing complex stuff")){using (profiler.Step("Step A")){ // something more interesting hereThread.Sleep(100);}using (profiler.Step("Step B")){ // and hereThread.Sleep(250);}}return View();}

This will change the MiniProfiler report with your custom benchmarks, see Figure 3.

 logo in a gray background |

Implementing MiniProfiler in a project is pretty simple. Although I describe this process for an ASP.NET MVC 4 application, the steps are similar for different applications:

  1. Install the MiniProfiler package in the Package Manager Console using the Install-Package MiniProfiler.MVC3 command. This installs the core MiniProfiler package as a dependency and wires up the project for basic use. The MVC3 package is the version to use for both ASP.NET MVC 3 and 4.

  2. In your shared _Layout.cshtml view, add the following RenderIncludes statement immediately before the closing body tag: @StackExchange.Profiling.MiniProfiler.RenderIncludes(). This adds the CSS and JavaScript files needed by MiniProfiler to display profile information.

That's it. When you run your application, you'll have the basic functionality of MiniProfiler. There are plenty of different ways to integrate MiniProfiler into your application. And there are tools that let you integrate closely with ADO.NET code as well as Entity Framework that can give you deep insight into where your data access code is having problems.

One of the nice things about the work that MiniProfiler's lead developer Sam Saffron' has done for ASP.NET MVC applications is to implement the NuGet package so that it's almost brain-dead simple to use MiniProfiler. When you install MiniProfiler in an ASP.NET MVC 3 or 4 project, check out the MiniProfiler code file in the App_Start folder to understand the details.

MiniProfiler works with just about any ASP.NET application, including Web Forms and Ruby applications. It's a very nice and effective way to explore the performance characteristics of your application with relatively little impact on the application. Its delivery as a NuGet package makes it trivial to add to a project, at least for the default usage. And it's easily usable in production applications. Check it out!

Sign up for the ITPro Today newsletter
Stay on top of the IT universe with commentary, news analysis, how-to's, and tips delivered to your inbox daily.

You May Also Like