mvc-mini-profiler - An effective mini profiler for ASP.NET MVC

The mvc-mini-profiler is a lightweight, but effective mini profiler tool for ASP.NET apps specially designed for ASP.NET MVC 3 apps. The mvc-mini-profiler is an free and open source tool from Stack Overflow team. The tool is initially developed for StackOverflow family of websites and open sources the tool later. This is an another great open source tool from Stack Overflow team after Dapper object mapper for .Net. The mvc-mini-profiler is available from http://code.google.com/p/mvc-mini-profiler/ . The mvc-mini-profiler is also available as a NuGet package .

How to using mvc-mini-profile

Step 1

Add reference to MvcMiniProfiler. You can use NuGet to add reference to MvcMiniProfiler

Step 2

Write out the css and javascript includes in the layout page

  1. @MiniProfiler.RenderIncludes()

Step 3

Start the mini profiler when you want to start profiling

  1. @using MvcMiniProfiler;

The below code block of the Application_BeginRequest event will start profiling if the request is from local computer

  1. protected void Application_BeginRequest()
  2. {
  3.     MiniProfiler profiler = null;
  4.     if (Request.IsLocal)
  5.     {
  6.         profiler = MvcMiniProfiler.MiniProfiler.Start();
  7.     }
  8. }

Step 4

Do the profiling when you want to perform profiling

  1. public ActionResult Index()
  2. {
  3.     var profiler = MiniProfiler.Current;
  4.     using (profiler.Step("IndexPage"))
  5.     {
  6.         ViewBag.Title = "Home Page";               
  7.     }
  8.     using (profiler.Step("Doing complex stuff"))
  9.     {
  10.         using (profiler.Step("Step A"))
  11.         {
  12.             Thread.Sleep(100);
  13.         }
  14.         using (profiler.Step("Step B"))
  15.         {
  16.             Thread.Sleep(250);
  17.         }
  18.     }
  19.  
  20.     return View();
  21. }

 

Step 5

Stop the mini profiler when you want to stop it

  1. protected void Application_EndRequest()
  2. {
  3.     MvcMiniProfiler.MiniProfiler.Stop();
  4. }

 

The above code stops the mini profiler in the Application_EndRequest() event.

1 Comment

Comments have been disabled for this content.