MiniProfiler: Profiler For .NET Code

Introduction

As developers we often encounter challenges to debug and optimize queries we have written for requests made to the server. Getting the exact time of the queries requested for each page load is next to impossible. Usually we count, such as one, two and three and calculate the average. But is this the solution, how often can we do this? As developers this is not what we would want. The network tab in the developers tool gives us the information about the per request made for a page load, but that is not enough for us to understand which query is taking more time and which query requires optimization. As MiniProfiler suggests.

It is capable of profiling calls on raw ADO.NET (SQL Server, Oracle, and so on), LINQ-to-SQL, EF (including Code First), Lightspeed and a range of other data access scenarios.

Isn't that great? Yes, indeed it is.

Versions

There are different versions available for .NET MVC projects based on the MVC versions. We can get the Install Package with versions from the following provided Links.

Get Started

As we all know, for getting the package, we use the Package Managerconsole or Nuget Package. I will be using the PM console for this purpose. I will be installing Miniprofiler for MVC application. Check the image below. This will add the references to the project. Not ending with this, next is the configuration that needs to be done. Check out the configurations below.

configurations

Configurations

At first in an MVC application, as we know the heart of the MVC lies in the Global.asax, so we need to configure that here first. There is no hard and first rule for this but what I follow is this:

  1. protected void Application_BeginRequest()  
  2. {  
  3.    if(request.IsLocal)MiniProfiler.Start();   
  4.    //or any number of other checks, up to you, if for all request you need profiling then remove the if condition  
  5. }  
  6.   
  7. protected void Application_EndRequest()  
  8. {  
  9.    MiniProfiler.Stop(); //stop as early as you can, even earlier with Mvc MiniProfiler.MiniProfiler.Stop(discardResults: true);  
  10. }  
This is all we need to do to let Miniprofiler get registered to the application. Now for the question of how the Miniprofiler will be displayed to the user (developer). This is simple, we just need to check which page load takes more time and has the maximum number of queries being called at the same time. Then we just need to place a simple piece of code snippet as said below in the view page, since that needs to be rendered on the view only.
  1. @using StackExchange.Profiling //namespace to be added  
  2. <head>  
  3.     @MiniProfiler.RenderIncludes()  
  4. </head>  
Just note the namespace used : using StackExchange.Profiling Thus when we run the application, the view/layout wherever the RenderIncludes() is there the profiling is done and displays the entire list of queries executed and the time taken.

time taken

The following is how the profiler looks. Now you would be wondering where the profiling is to be connected to the Entity Framework. Hold on. For that we need to install another package using the same PM console and that is Miniprofiler for the EF version based on what used in the application. This will add the package and all the dependencies that are not present will be added.

Entity Framework

Then we need to add a simple code line to the Application_Start method in the Global.asax as in the following:
  1. MiniProfilerEF.Initialize();  
As it explains itself, this will initialize the profiling for the Entityframework operations or the db operations and show the user the entire SQL querybeing called/executed during the page load and also the time taken.
sql querybeing

The highlighted part in the preceding image shows the SQL query being called and how many times. The Red mark indicates that there are duplicate calls being made in the page load, based on which we will be optimizing the queries and reduce the number of db operations to make the page load faster, thus improving performance. I hope this small piece helps developers beginning their careers. Any modifications, suggestions are mostly welcome.

Up Next
    Ebook Download
    View all
    Pristine
    Read by 0 people
    Download Now!
    Learn
    View all