How to use .NET profiler to troubleshoot ASP.NET page perfomance issues

How to use .NET profiler to troubleshoot slow ASP.NET page performance

 

I’m frequently tasked to figure out why certain pages or parts of websites are slow (assuming site is ASP.NET based). In this specific article I’ll show how to use free .NET profiler from Equatec http://www.eqatec.com/tools/profiler to troubleshoot this issues.

 

My common troubleshooting steps (before diving into ASP.NET website performance troubleshooting are below).

 



  1. Use Fiddler tool (http://www.fiddler2.com/fiddler2/) to actually measure response time of web page. This works with AJAX based pages as well and will actually provide you information which you usually would not be able to see in browser window (simple GET/POST requests are so 90s in modern technologies).


  2. Once you get statistics and confirmed that you do in fact have backend problem other then networking issues I usually see if there are any known dependencies for the page (like SQL backend). This is pretty easy to figure out by looking through web.config file. I start SQL profiler on SQL server afterwards and resubmit request from browser again watching profiler statements coming through. Sometimes troubleshooting ends here (if you see that Execution time of stored procedure or ad-hoc query corresponds to total page response time.



  3. At this point if you ruled out backend slowness you are probably looking at some issues within ASP.NET website itself. I usually enable ASP.NET tracing at this point by going to web.config and adding following statement there
     

    ). This will output tracing information (most importanly times to takes to execute specific methods in pipeline). Sometimes it’s enough to to see which part of the page pipeline fails.

     



  4. Please note that page execution times listed trace output are not including times which are spends outside of the page (like authenticating request or processing events in global.asax). So you might see fast execution times in trace output but page will still be slow to render and hence we come to real purpose of this article, i.e. to use .NET profiler to see what is causing page to stall.

 

 

I wrote a sample website where I simulated different stalling conditions. Stalling in pre-page execution pipeline (in global.asax) and stalling inside page itself (SQL call or Web Services call or something of that nature). To simulate long running method I inserted following statement in relevant methods (System.Threading.Thread.Sleep(5000);). This makes current thread sleep for specified amount of time. I put Tracing statements through the code so you can actually see in screenshot below where latency are being introduced. Lines in red indicate Trace statements in the code. (First one in global.asax Application_BeginRequest event and second one inside Page_Load() statement of default.aspx page). In real life scenario you most likely would not have any Tracing statements in code you’ll be troubleshooting though.

 

 

I compiled my test website and at this point all I have is what you probably would have in your case. You can download it here (ZIP). Once you installed it, navigate to default.aspx page and see how much it takes to load a page (shall be around 7s or so).

 

Next step is to install Equatec .NET profiler from here (Here).

 



  • On first run it’ll display default screen below


 



  • Click browse button and navigate to installation directory of your website (BIN folder) and open precompiled binary for your application (profiler.dll in this case)



 



  • Click “Build” button on bottom. Profiler will inject profiling information into your assembly and output results to the “Bin-Profiled” folder under the same root where your original “Bin” folder is. You’ll see that you original DLL file grew up in size because of that.


  • Copy all files from “Bin-Profiled” folder to “Bin” folder (replacing files)


  • Switch to “Run” tab and navigate to your problem page. You’ll notice that Control area will be updated with information “Profiled app Profiler started”




  • Click on “Take snapshot” button. It’ll generate report about execution time and put report under “View Snapshot reports” area.




  • Click “View” button now with latest report selected. Result below is exactly what you’d expect to see from test file. Which is first 5 seconds were spent in BeginRequest event and next 2 in Page_Load calling “DoSQLQuery” method.



 



  • Now since you know which specifics methods take that much time you need to take a look at what is being done inside those methods. In this case (and probably yours) you’ll just have assembly (profile.dll). You need to use tool like .NET Reflector (Download here) to see contents in readable form.

 

Now we can see exactly why it took so long to execute this specific method. In your case it might be SQL call or web services or call or something of that nature.