Tuesday, March 5, 2013

Performance Profiler in Visual Studio 2012

Today I want to share how to use the performance profiler tool which is available in Visual Studio 2012. I had been using 3rd party profiler tool such as Red Gate all these while, and you know that we have to pay some money for the license. Since Visual Studio 2012 comes with the performance profiler, we can save some money although I find the user friendliness of the tool is not as good as Red Gate's ANTS Performance Profiler.

Note: this tool only available in Visual Studio 2012 Premium and Ultimate edition.

A bit of intro about what is a performance profiler. It is a tool that monitor your running application and capture the information like the time of execution between individual line of codes. Besides, it also has the capability to capture objects in the memory, CPU utilization at that time or any resource contention (deadlock). With these information, it will be easy for us to identify which line of code is causing bottleneck, and the objects in the memory are really get collected by garbage collector or not, if not, it mean memory leak has occurred.



In your Visual Studio 2012, after clicking the Analyze menu, you can straight away start the performance profiler tool by clicking at the "Start Performance Analysis". It will run the profiler with default settings. After the profiler tool is started, it will fire up your application and begin profiling your application. Then, you can proceed to play around with your application. If you wish to track a certain area of code, you can just use your application in a way that you know how it would trigger that area of code, and then quit the application. The profiler tool will start its analysis job then provide the report later.

My experience is I have a web application to be profiled, but it is in layered architecture, means that I have multiple projects need to be profiled at the same time. The default profiler setting is not able to capture the information that I want. Therefore, I have to click the "Lauch Performance Wizard" menu to configure my profiler.


I have selected "Instrumentation" in order to capture the response time between lines of code.


Then, select the web project that I wish to profile. Here is the tricky part, by default it would select all the projects for you, and for the first time user like me, I would assume that it will profile everything for me even the selected class library projects. If your application architecture design is similar to mine, multiple class library projects are referenced by your web project, you can skip from selecting the class library projects in this window. Later, there is a place for you to select which assemblies (dll) to be instrumented together with your web project.



Next, "Enable Tier Interaction Profiling", the description look interesting, just enable it if your project is a web project.


Uncheck the checkbox, do not launch profiling yet because I have not add the assemblies to be instrumented together with my web project.

After clicking the Finish button, a new profiler session will be created in your Performance Explorer window. Right click the profiler session, then open the "Properties".


I would suggest to change the "Report Location" because when the first time I use this tool, all the reports are generated into my solution root folder, it look messy when getting more and more performance reports generated. Hence, I have a folder created call PerformanceReport in my solution folder to keep those performance reports. Then, click the Apply and OK button to close this window.




Now, back to Performance Explorer window, right click the Targets folder, then "Add Target Binary". Browse through your web project's bin folder, then select all the assemblies (dll) which are referenced by your project.


In order to start profiling, right click the Profile, then click the "Start Profiling" menu. You should expect to see your web project get built and default browser will be opened up for you automatically. Then, start to play around with your application. Once you are done with it, click the "Stop Profiling" or close the web browser.

Later, a performance report will be generated.


The graph is about the CPU utilization during the profiling, I personally do not find it useful.
The Hot Path is the suggested functions that you need to pay attention.
The Functions With Most Individual Work is the list of functions that take very long time to process in sorted order.

Play around with the views, there is a call tree view where display top to bottom of a function to another function call, the data is actually quite well self-explained. I personally find the "Avg Elapsed Exclusive Time" value is more useful which tell a particular function response time.


If you go to the modules view, you would find the screen like above. It tells you how times of your assemblies had been called, and I would normally do is to sort the Avg Elapsed Exclusive Time in descending order, and then double click the class.


Then, change the performance metric if you like. Click the function which show long elapsed time. At the bottom, you will see your code. It is the hint where you need to tune your code.

Enjoy your code tuning~

1 comment:

  1. Is it possible to change the default browser for ASP.NET profiling? I've been looking for a way to do this with no success, the Visual Studio Profiler Team blog has no contact information.

    ReplyDelete

Send Transactional SMS with API

This post cover how to send transactional SMS using the Alibaba Cloud Short Message Service API. Transactional SMS usually come with One Tim...