Being developers we always face a challenge, to debugand optimize the queries we have written for any request made to the server. Getting the exact time of the queries requested for each page load is next to impossible. Usually we count one two three.. and see the average.. :D. But is this the solution, how often can we do this!! This is not as developers 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 know which query is taking more time and which query reuires optimization.
As MiniProfiler suggests
It is capable of profiling calls on raw ADO.NET (SQL Server, Oracle, etc), LINQ-to-SQL, EF (including Code First), Lightspeed and a range of other data access scenarios.
Is’nt that great!! Yes indeed.
There are different versions available for .NET MVC projects based on the MVC versions. We can get the Install Package with versions from the below provided Links.
- Miniprofiler-The Core needed for profiling
- Miniprofiler for EF 6-The Core needed for profiling EntityFramework version 6+
- Miniprofiler for EF 5-The Core needed for profiling EntityFramework version 5
- Miniprofiler for MVC3-The Core needed for profiling MVC3 applications
- Miniprofiler for MVC4/5-The Core needed for profiling MVC4 & 5 applications
- Miniprofiler for WCF-The Core needed for profiling WCF applications
As we all know, for getting the package, we use the Package Managerconsole or Nuget Package. I would 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, there comes next the configuration which needs to be done. Check out the configurations below.
At first in an MVC application, as we know the heart of the MVC lies in the Global.asax, so we need to configure here first. There is no hard and first rule like, this but I follow this. 😀
protected void Application_BeginRequest()
//or any number of other checks, up to you, if for all request you need profiling then remove the if condition
protected void Application_EndRequest()
MiniProfiler.Stop(); //stop as early as you can, even earlier with Mvc MiniProfiler.MiniProfiler.Stop(discardResults: true);
This is all we need to do to let Miniprofiler get registered to the application. Now comes the question how will the Miniprofiler 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 qeries being called at the same time. Then we just need to place a simple peice of code snippet mentioned below, in the view page, as that needs to be rendered on the view only.
@using StackExchange.Profiling //namespace to be added
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. The below is how the profiler looks:
Now you would be wondering where did I mention the profiling to connect to the Entity Framework!! Hold on.. For that we need to install another package through the same PM console and that is Miniprofiler for the EF version based on used in the application.
this will add the package and all the dependencies if not present will be added. Then we need to add a simple code line to the Application_Start method in the Global.asax as below:
This as it explains itself, 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.
The highlighted part in the above image, show 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 peice helps developers beginning their careers. Any modifications, suggestions are mostly welcome.