Here’s the scoop. I wanted to profile my application to show someone how we could get the best “bang for our buck” on a single hardware upgrade. Did I need more memory or CPU? I expected, in the course of collecting metrics, to find some slower code snippets to improve software side. It can be sped up a little bit, but the fact of the matter is that the sheer volume of data has increased to a tipping point the hardware simply could not handle in the same time frame. Also, I wanted like to start doing this on a more regular cadence, so I would need any help I can get to speed up the process. You would think that Visual Studio 2008 would have something built in for C#. Well, it does if you shell out the extra money for the Team Edition. I have Professional. I am thusly screwed, right?
Wrong. For some reason they offer the Performance Tools from the Team Suite as a free download online. Check it out! How nice, Microsoft! The only draw back is that you will be doing this through the command line. (Even then, for some that is a pro and not a con.) My program is a command line utility, so it did not really change my method, but it is something to note as a draw back for the less command line literate. So I looked for a tutorial to get me on my way and I found everything I ever needed. Sort of. A little bit.
I spent way too much time staring at a blue screen. It was horrendous to behold. All I was trying to do is profile my C# application to gather some metrics for something I already know. The computer needed a memory upgrade. Before you even begin, see if you need this patch: http://blogs.msdn.com/b/chrissc/archive/2009/02/24/patch-released-to-fix-sampling-on-intel-core-i7-processors.aspx. Otherwise you will blue screen constantly and think you are doing something wrong. Trust me, it is a huge waste of time if you have an i7 processor and don’t download this patch.
To get us started, here is the article of reference I used: http://msdn.microsoft.com/en-us/library/aa985628.aspx. This was probably the best source of information. I will be duplicating some of the information here in walking through how to profile a C# application.
Let’s begin. Build your binaries in Release! Building in Debug is a waste of time as it does not perform quite the same. Add the location of the VSPerf tools to your PATH. Again, build Release and then fire up the command line. For me, this was located in “C:\Program Files (x86)\Microsoft Visual Studio 9.0\Team Tools” and if you have Visual Studio 2008, that’s where it should be for you. Translate to the proper Visual Studio install and OS for your machine.
Now you have a choice to make: Sampling or Instrumentation. If sampling, you just need to turn sampling on globally with the following command:
VSPerfCLREnv /sampleon
If you are using instrumentation, you will have to do slightly more. Turn on instrumentation globally:
VSPerfCLREnv /traceon
For all of the dll and exe files that you will be profiling, (hint: do this for dependencies) you will need to run the following commands to allow instrumentation.
VSInstr ProgramOrDll.exe
Now you’re ready to collect some data. I hope you have a simple, repeatable test planned. This is critical to identifying performance issues, especially if you plan on fixing them. Always have a repeatable test and run it multiple times to ensure you get correct data. Run your program from the command line with Performance Monitor by using the following command:
VsPerfCmd /start:sample /output:Report.vsp /launch:YourProgram.exe
When you’re done with sampling, you must turn it off with the following command. (Instrumentation will end when your program ends.)
VSPerfCmd /shutdown
Now you’ve got a beautiful vsp sitting on your hard-drive! This is the raw performance data. Now you need to change it into a consumable format because of our lowly Visual Studio version. Team Edition opens these bad boys. We are going to use more primitive tools. Microsoft, and I, recommend creating a folder at this point to help stay organized. Run this command to create a bunch of csv files:
VSPerfReport PeopleTraxReport.vsp /output:path\filename /summary:all
Now, don’t forget to restore your environment!
VSPerfCLREnv /sampleoff
These csv files are all different types of profiling information. You can tell what they are by the extension. There are functions, threads, modules, etc; My recommendations? If you’re looking for hotspots, use the _functions file. Application Exclusive Time is a great column to sort. If you’re trying to find a dead-lock or places of I/O wait try the _threads file. Otherwise, enjoy!

