For a recap of what is new in this release see my earlier post: What’s new in Windows Azure VS.NET Tools 1.4
Note: The tools can be downloaded here using the Web Platform Installer.
The profiler in Visual Studio is very powerful and extremely useful for tracking down inefficiencies and memory leaks in your application. With the 1.4.4 release of the Visual Studio Tools for Windows Azure profiling support has been added that allows you to easily profile your applications to track down these types of hard to find problems while they are running live in the cloud.
For this article I’ll use a simple but common performance problem: string concatenation.
I have three functions.
GenerateSomeData() just creates a new Guid and converts it to a string to return.
private string GenerateSomeData()
Guid g = Guid.NewGuid();
DoVeryInefficientStringCopying() calls GenerateSomeData() 1000 times and concatenates the result into a string. String concatenation is very intensive because strings in C# are immutable (you can’t change a string once it is created) so each concatenation is actually creating a new string with the previous/new combined.
private string DoVeryInefficientStringCopying()
String tmpString = String.Empty;
for (int i = 0; i < 1000; i++)
tmpString += GenerateSomeData();
return tmpString;<br />
DoMoreEfficientStringCopying() – accomplishes the same goal except it uses StringBuilder to append the string instead of using the string class’s += operator. The difference is StringBuilder is efficient and uses a buffer to grow the string instead of constantly creating new strings and copying memory.
private string DoMoreEfficientStringCopying()
System.Text.StringBuilder tmpSB = new System.Text.StringBuilder();
for (int i = 0; i < 1000; i++)
I’m adding this code to a worker role I will deploy out to Azure with profiling enabled.
Here is the worker role code:
public override void Run()
// This is a sample worker implementation. Replace with your logic.
Trace.WriteLine("WorkerRole1 entry point called", "Information");
// Use the profiler to see which method is more efficient..
For profiling this simple problem I’m going to choose CPU Sampling:
Once the application is deployed and the scenario you are profiling has been reproduced you can analyze the profiling data.
The first step is to configure your symbol paths. Within Visual Studio click tools -> options -> debugging. I have a debug build and I’m not calling any additional libraries so I don’t need to set anything here but if you have components with .pdb’s outside of this project you could reference them here as well as the Microsoft symbol servers.
To analyze the profiling report expand the worker role from within server manager and select “View Profiling Report”:
The initial screen shows my overall CPU usage which isn’t much considering my code is sleeping every 10 seconds. It also highlights a “hot path” which is the most expensive code path captured during the profiling session. Unsurprisingly, it is in the DoVeryInefficientStringCopying() method.
Clicking on the WorkerRole.Run() method drills in where I can actually compare and contrast the two methods:
It’s easy to see that 94.6% of our time was spent in the VeryIneffecient method compared to the Effecient version.
If I then click on the DoVeryInefficientStringCopying() method in the top window I can drill in further which gets us to the += operation causing all of the trouble.
You can also change the view to a table layout of all the different functions to compare and contrast how much time was spent in each function and various other views.
For more information on analyzing the report data using Visual Studio 2010 Profiler see the following link: http://msdn.microsoft.com/en-us/library/ms182389.aspx.