I had written this article on my blog and thought of posting it on CodeProject too.
Microsoft has released the first service pack for Visual Studio 2010. Although the service pack is marked as beta it comes with a “go live” license. Along with it, two other service packs are released - Team Foundation Server 2010 SP1 Beta and .NET 4.0 SP1 Beta. For Silverlight developers, they have packed two updates in the Visual Studio 2010 SP1 Beta.
- Silverlight 4 tools for Visual Studio 2010: “The Silverlight 4 tools are now included along with Silverlight 3 support”, all it means is that you will not have to install Silverlight 4 tools separately.
- Performance Wizard for Silverlight: This will enable the users to profile the Silverlight application performance directly through Visual Studio, user can create profiling reports and save them.
You can get the download links from Brian blog, he has nicely detailed all important updates in the service packs http://blogs.msdn.com/b/bharry/archive/2010/12/07/vs-tfs-2010-sp1-beta-has-released.aspx.
Once you have downloaded the web installer, be patient as the install will take some time to download the packages and install. For me, the process took around an hour from start to finish. After successful installation, you will be asked to restart your computer. Now your Visual Studio is all set to start profiling your Silverlight application using the wizard.
The Performance Wizard is very simple. Let us go step by step through it, open an existing Silverlight project or create a new project. To launch the ‘Performance Wizard’, go to the main menu find Analyze –> Launch Performance Wizard…
Performance wizard page 1 of 3 should be displayed, CPU Sampling must be selected by default if not the check mark the radio button before CPU Sampling.
Performance wizard page 2 of 3 - on this page you will find a list of projects depending on the structure of your solution. Typically for a Silverlight project, you will find the Silverlight application and the web application in the list, select your Silverlight application and click the ‘Next’ button.
Performance wizard page 2 of 3 is just a summary that says all the information needed has been collected. Make sure that on this page you have the check mark on the check box labelled as Launch profiling after the wizard finishes. When you click finish, the application should open in your default browser.
If you are not running as administrator, you might get an error saying “Unable to open profiler driver, would you like to upgrade credentials ..“ . Just click ‘yes’ and provide admin credentials if needed.
Once the above steps are done, the Performance Explorer with be populated with a structure similar to the image below. The performance wizard has created two folders, Reports and Targets.
Ok, now let's do a simple performance test. Let us create a simple application and see how the performance report looks like. Create a Silverlight application. In your main page, add a button and on the click of the button, we should have a function that does some kind of CPU intensive activity. I have simply created a nested loop to keep the CPU busy for a while, but you can write any code that will be CPU intensive.
private void Button_Click(object sender, RoutedEventArgs e)
for (int i = 0; i < 100000; i++)
for (int j = 0; j < 10000; j++)
Now follow the steps shown above to generate a profiling report using the wizard, run the project and click on the button a couple of times and then close the browser. Visual Studio will bring up the visual report based on the samples collected during the performance analysis. THe below figure shows the result of my test, one can clearly identify in the graph the spike in CPU during the first and second click. An interesting fact is that Studio will automatically show a call tree of the method that is expensive in terms of resources. For our case, the event handler
Button_Click() does an expensive
for loop and Studio was able identify the method and display the call hierarchy.
Points of Interest
- 22nd December, 2010: Initial post