Rich client applications focused on line of business and written on the Silverlight platform have become increasingly...
popular since the introduction of Silverlight 4 in March of 2010. Great performance is crucial for the success of many enterprise applications and this is even more important with Silverlight because it runs within the client browser and with the limitations of the security sandbox. Profiling Silverlight applications is a proactive step to ensure your applications are fine-tuned to deliver the best possible experience to the customer.
Everything you need to profile Silverlight 4 applications is available within Visual Studio 2010.
Know your Symbol Servers
Microsoft Symbol Servers provide advanced and detailed debugging information to the development environment. Symbols are important for profiling because they enable the profiler to take the code being analyzed and link it to the symbols that represent variable names, method calls, and other metadata information that is available in the source. Without the appropriate symbols, you won’t be able to decipher the profiler information. Fortunately, enabling and setting up symbol servers is fast and easy.
To enable your symbol server, open Visual Studio 2010 and go into Tools and Options. Expand the Debugging node and select Symbols:
The first thing you want to do is specify a directory to cache symbolic debug information. This cache is important because it prevents the debugger from having to fetch the debug symbols over the network every time you start debugging. You may choose any directory by typing in the full path or clicking on the ellipses to browse to the directory of your choice.
The first time you debug, the symbol server should pull down the modules that you specified “automatically load symbols” for (see the bottom radio buttons in the previous illustration). To see what modules have had the symbols loaded, run the program in debug mode and navigate to the Modules window:
This window will display all modules and will indicate whether or not the symbols have been loaded. It will also indicate the path to the symbols:
If an item does not have symbols loaded, you can request to load them from the Microsoft Public Symbol Server (a central repository for Microsoft-related symbols) or point to a local directory where the symbols are located. Simply right-click on the module, choose “Load Symbols From” then indicate a path or the Microsoft servers:
Where are my Local Symbols?
Your local symbols are located in files with the .PDB extension in the output directory for your application. To locate the local symbol directory for a project, right-click on the main project name and choose “Open Folder in Windows Explorer.” The symbol directory will be in the Bin/Debug folder unless otherwise specified in the project properties. For more on PDB files, read the MSDN article here:
Release the application
The next step may seem confusing but to profile your application, you must compile it in Release mode (you can still specify that debug symbols are generated when building in release mode). The symbol servers will provide the necessary symbol information as configured in Visual Studio, but the release mode is required for the Silverlight profiler to effectively parse and profile the running application. Switch the application to Release mode and hit CTRL+F5 to compile and run it. Take note of the URL generated by the debugger because we’ll use this in the profiling step:
Profile the usual suspects
The first step to begin profiling is to open a Visual Studio 2010 command prompt (this is separate from a regular command prompt and is available from the start menu under Visual Studio 2010). It is important that you run this as Administrator to have the appropriate access to profile. Navigate to the command prompt and right-click to run as administrator.
Now, find the release directory for your main XAP file. You can do this quickly from Visual Studio by right-clicking on the project and choosing “Open Folder in Windows Explorer” then navigating to Bin/Release. Copy the path and change to that directory, then type:
This will set up the environment for profiling. Read more about this command here:
Now we can launch the web browser and pass it the URL for the application that we noted earlier. In this example, I’ll use Internet Explorer and type the following on one line:
"c:\program files (x86)\internet explorer\iexplore.exe" http://localhost:42447/Default.aspx
Next, I need to get the process id for the Silverlight application. If I have no other web browsers open, I should get two process ids: one for the host web application, and one for the Silverlight application. With a little practice, you’ll be familiar with the signature of your application and be able to guess the right one. An easy way to “cheat” is to use the Visual Studio debugger and choose “Attach to process.” Don’t actually attach, but in the view you can see the names of the processes and the runtime with an indicator of the Silverlight process.
The following command attaches the profiler – after executing it, you navigate through the application and use it for a period of time to collect sample data. The output directive indicates what the filename should be.
VSPerfCmd /start:sample /output:mydata /attach:4956
When you are finished, you can execute a series of commands to detach the profile, dump the profile information, and reset the environment:
Now let’s take a look at what we found!
Just the facts, Ma’am
In the release directory of the application where you began the performance profiling session there should be a file with the name you specified and a .vsp extension.
Double-click the file to open it within Visual Studio 2010.
From Visual Studio 2010, use the file … open dialog to open the file. You’ll see a dialog as Visual Studio parsers the profiled data:
After it is loaded, the report will be analyzed. In the output window, you’ll see a status as the symbols are loaded for the data:
If all goes well, you’ll be greeted with a report that shows a summary of CPU usage:
From this page, you can easily drill down in other areas by changing the current view, sorting and filtering, and drill down to the area the application is spending the most time and/or consuming the most resources.
These steps work out of the box. For advanced profiling, however, it is recommended that you look into any number of third-party profiling tools. Most commercial packages support the Silverlight 4 runtime and can provide deeper insights into details such as object graphs, memory utilization, and call volume over time.