Performance analyzes your website with MiniProfiler

A profiler is a performance analysis tool that, most commonly, measures only the frequency and duration of function calls, but there are other specific types of profilers (e.g. memory profilers) in addition to more comprehensive profilers, capable of gathering extensive performance data.

What Is MiniProfiler?

A simple but effective mini-profiler for .NET and Ruby.

MiniProfiler was designed by the team at Stack Overflow. It is in production use there and on the Stack Exchange family of sites. Also used at topnguyen.net, acturent.com, plane9.com, GoDaddy.com.

MiniProfiler does not attach itself to every single method call; that would be too invasive and wouldn't focus on the biggest performance issues. Instead, it provides:

  • An ADO.NET profiler, capable of profiling calls on raw ADO.NET (SQL Server, Oracle, etc), LINQ-to-SQL, EF (including Code First) and a range of other data access scenarios
  • A pragmatic Step instrumentation that you can add to code you want to explicitly profile

Simple. Fast. Pragmatic. Useful.

How To Use

Firstly, you need to install MiniProfiler by NuGet

PM> Install-Package MiniProfiler

Secondly, add MiniProfile bundle to your master page (_layout.cshtml)

@using StackExchange.Profiling;
<head>
 ..
</head>
<body>
  ...
  @MiniProfiler.RenderIncludes()
</body>

Add to your web.config the code below to make sure MiniProfiler will show on your page.

<system.webServer>
  ...
  <handlers>
    <add name="MiniProfiler" path="mini-profiler-resources/*" verb="*" type="System.Web.Routing.UrlRoutingModule" resourceType="Unspecified" preCondition="integratedMode" />
  </handlers>
</system.webServer>

Finally, start and stop MiniProfiler for each request to monitor a request performance

using StackExchange.Profiling;
...    
protected void Application_BeginRequest()
{
        MiniProfiler.Settings.Results_Authorize = IsUserAllowedToSeeMiniProfilerUI;
        MiniProfiler.Settings.PopupRenderPosition = RenderPosition.BottomLeft;
        MiniProfiler.Settings.StackMaxLength = 256; // default is 120 characters
        MiniProfiler.Start(); 
}

protected void Application_EndRequest()
{
    MiniProfiler.Stop();
}

private bool IsUserAllowedToSeeMiniProfilerUI(HttpRequest httpRequest)
{
    // Implement your own logic for who 
    // should be able to access ~/mini-profiler-resources/results
    var principal = httpRequest.RequestContext.HttpContext.User;
    return principal.Identity.IsAuthenticated;
}

If you using Entity Framework in your project, you need more two steps to setup MiniProfiler for Entity Framework.

Step 1: Install MiniProfiler for EF by NuGet in your web project

PM> Install-Package MiniProfiler.EF6

Step 2:  Init MiniProfiler EF in Application_Start

protected void Application_Start()
{
    ...
    MiniProfilerEF6.Initialize();
}

Done, very simple :D and here is the result.

An Important NuGet Note

MiniProfiler

PM> Install-Package MiniProfiler

MiniProfiler for Entity Framework v6

PM> Install-Package MiniProfiler.EF6

Github link

Website link

Summary

Mini Profiler is a middleware that displays speed badge for every HTML page.

Designed to work both in production and in development.

Mini Profiler helps you avoid N + 1 problem and duplicate query detection.

The profiler is also able to log all ajax calls, you will get one profile timing per ajax call.

Happy Coding!