Silverlight is now into its fourth version (woo!) and with each iteration the platform becomes more solid, more mature and easier to develop for. One of the newer features that is now supported "out of the box" with Silverlight 4 is the ability to profile your Silverlight application, but you wouldn't know it from just digging around the menus of Visual Studio. In this post we'll explore the process of collecting a profile from your XAP (both in and out of browser) and we'll touch on common troubleshooting techniques for failed profiles.
What You'll Need
Ready, Set, Profile!
Here's the quick and easy way to profile - open an elevated Visual Studio 2010 Command Prompt (pre configured with useful Visual Studio commands) and run the following:
VSPerfClrEnv /sampleonVSPerfCmd -start:sample -output:somefile.vspVSPerfCmd -globalonVSPerfCmd -launch:"c:\Program Files (x86)\Internet Explorer\iexplore.exe" -args:""VSPerfCmd -shutdownVSPerfClrEnv /off
This will create a VSP (Visual Studio Profile) file in the current directory, which you can then open in Visual Studio.
Note: If you plan on profiling Internet Explorer 8 (and up) you must disable its per-tab process feature (otherwise you'll end up profiling the iexplore container process instead of the process that is hosting Silverlight). You can find more information here.
Tips & Tricks / FAQ (Recommended Reading)
Take a couple of minutes and run through the following, since you are most likely going to run into one of these issues on your first go...
If the missing symbols are in your own DLLs (like from your own XAP) then make sure to either launch the VSP from the directory that has your PDB files (see the above suggestion regarding associating VSP files with Visual Studio), or add the directories with your PDB files to your symbol path by adding them under the same tool window as above.
VSPerfClrEnv /sampleonVSPerfCmd -start:sample -output:somefile.vspVSPerfCmd -launch:"c:\Program Files (x86)\Internet Explorer\iexplore.exe" -args:""VSPerfCmd -globalonVSPerfCmd -shutdownVSPerfClrEnv /off
VSPerfClrEnv /sampleonVSPerfCmd -start:sample -output:somefile.vspVSPerfCmd -globalonVSPerfCmd -launch:"c:\Program Files (x86)\Internet Explorer\iexplore.exe" -args:""VSPferfCmd -detachVSPerfClrEnv /off
Loaded symbols for C:\Windows\SYSTEM32\ntdll.dll.Loaded symbols for c:\Program Files\Microsoft Silverlight\4.0.50401.0\npctrl.dll.Loaded symbols for c:\Program Files\Microsoft Silverlight\4.0.50401.0\agcore.dll.Loaded symbols for C:\Windows\system32\WINMM.dll....
You may also see one of the two following possible warnings:
Failed to load symbols for C:\Windows\System32\nlaapi.dll.
The most common problem - Visual Studio has found the DLL that it profiled, but it can't find the symbols for it. You'll need to adjust your symbol path to find the PDB files for your project and then reload the VSP.
Warning VSP2701: Kit3D.dll could not be found when looking for symbol information.
This is what you'll see if you're profiling a XAP from the internet - Visual Studio can't find the DLL that it profiled (because it was in the XAP that was downloaded) and thus can't load it to find corresponding PDBs. Load the VSP from the directory that has your DLLs in them (or add that to your global PATH), or download the XAP and extract the DLLs and this warning will go away. Note that if you don't have the PDBs then the warning will just morph into the above "Failed to load symbols".
This controls how often we sample by specifying the number of cycles before sampling (default is 10,000,000). You can also play around with -pf (sample of every n page faults) and -sys (sample on every n system calls). See -? for more info...
For a wide range of options, a lot of which I haven't touched on here.
26/4/2010 Edit: Maxim has a great post which walks you through profiling an actual app using similar steps to those described here - worth checking out!
28/4/2010 Edit: Updated FAQ with some questions from Maxim's blog
29/4/2010 Edit: Added note about IE8
Originally published here.