Skip to main content

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. 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.


Post a Comment

Popular posts from this blog

Alibaba Cloud MVP Global Summit

On the 24-27th of September, Alibaba Cloud MVP Global Summit and Apsara Conference was held at Hangzhou, it was an honor to be invited to participate in this event. On the first day itself, once I touch down the Hangzhou Xiaoshan Airport, I find there are free shuttle buses available to bring you from airport to hotels and conference venue.

The MVP Global Summit was held at The Cloud Town on the 24th September. It is a gathering for the domestic and international MVPs to get to know each other, and there are also sharing of Alibaba Cloud new company direction, new product milestone, and the experience of Alibaba Cloud product application.

During the event, I had made the acquaintance of the MVPs from China, Singapore, Japan, Spain and Netherlands. Since every MVP has his own area of expertise, it is interesting when everyone share their story, problem and insights about technology.

Also, I got a surprise during the event for receiving a most active MVP award. Thanks Alibaba Cloud for…

How To Convert A Console Application To A Windows Service?

Problem Today I want to share how to easily convert an existing console application to a windows service. There are a few reasons why we would want to do that:
Console application must be running all time with an active user session in a server. A long running service application should be run by a non interactive (user logon) service account.When the console application is crashed due to unhandled exception, stack overflow or whatever reason, it will not be able to start back automatically unless someone manually go and restart the application.It is possible that someone login to the server with the same current active user session and accidentally close the console application.When the server is rebooted unintentionally such as power trip, the console application cannot start automatically. (Unless configured using startup link). Solution I came across a MSDN magazine, and found out that people are talking about Topshelf. It is quite an interesting small framework that allow us writ…

How To Limit The Thread Count Use By The Task Library?

Today I want to cover the topic about how to limit the thread use by the Task library which is available in .NET 4.5. This post is related to my previous async and await syntax post.

First of all, do you know that when one Task start to run, a new thread will be spawn for that task? Task is supposed to be used for asynchronous operation, however, with its thread spawning behavior, it also can be used for multi-threading operation. More detail info, check out this Task Parallelism article.

There are 3 ways to create a Task:

Option 1:

Using Task.Factory.

Task t1 = Task.Factory.StartNew(() => { Console.WriteLine(DateTime.Now.ToString() + " - Task fired."); Thread.Sleep(5000);

Option 2:

Using Task.Start() method.

Task t2 = newTask(() => { Console.WriteLine(DateTime.Now.ToString() + " - Task fired."); Thread.Sleep(5000); });


Option 3:

Using Task.Run() method.

Task t3 = Task.Run(() => { Console.WriteLine(DateTime.Now.ToString() + " - Task fired."); Thre…