VS2008: Profiling a Web Application Using Sampling
27 September 2009
In Visual Studio 2008, you can profile managed code using the Performance Tools that exists under the Analyze Menu. This allows you determine performance bottlenecks in code. There are two ways of doing this:
- Sampling - measures program state by sampling certain metrics such as clock cycles on a periodic basis.
- Instrumentation - inserts additional code into your code
This article discusses sampling. It is important to note that sampling can only be performed on a physical environment (i.e. not a VM).
To start profiling code, you must first enable collection of data using VSPerfCLREnv.cmd, typically located at C:\Program Files\Microsoft Visual Studio 9.0\Team Tools\Performance Tools. To enable CPU profiling, I run the above batch file with the option /globalsampleon. This enables sampling (attaching) to managed services. You can also enabled /globalsamplegc to profile memory. SImply run VSPerfCLREnv.cmd with no parameters to get the full list of options. Once this has been set, you need to perform a reboot, or restart all process that use dot net (at least i think - w3wp on its own was not sufficient).
Open the project you want to profile in Visual Studio and from the analyze menu select "Launch Performance Wizard". Select the relevant project to profile from your current solution e.g
Select sampling as the profiling method
Click finish on the next screen
The performance explorer will now appear (note that you can add additional target projects to this). To start gathering performance data, simply right click the performance node and select "attach/detach". Locate and attach to the W3WP (you may need to check the "Show Processes in All Sessions" and "Show Processes for all users checkboxes".

Once attached, begin hitting your web application through the browser (suggestion - set up a load test to repeatedly hit multiple times). Click stop in the performance explorer window when complete. The results will then process to give you summary data.
Change the current view drop down to drill into the results. E.g. change to modules and scoll down to find modules appropriate to your code
Double click on relevant functions to drill into the details...
Note that you can order by "Inclusive Samples %" to get the most expensive function calls from your current function. Double click these functions to drill into these.
Related Links
(currently 0 comments)
Office 2007 Tip – Lorem Function
23 September 2009
In Office 2007, type =lorem() to generate some lorem ipsum text. To control the number of paragraphs and sentences per paragraph simple type =lorem(3,5) This will generate 3 paragraphs of lorem ipsum text with 5 sentences per paragraph.
(currently 0 comments)
Visual Studio 2008: Auto Formatting
10 September 2009
Simply press Control + K then Control + D. and the code will be auto-formatted. Nice!
(currently 0 comments)
SharePoint / IE 8: Loopback Security Check
10 September 2009
Just a quick note – we recently had an issue on a project which had a host on a dns server pointing to a sharepoint webapp. When on the box hosting the webapp, I kept getting login prompts, even though i was logged in as the correct user.
It turns out that this is due to IE8 performing a loopback security check when accessing sites on a local machine via host header.
Workarounds are detailed here…
http://support.microsoft.com/default.aspx/kb/896861
http://blog.brianfarnhill.com/2008/11/19/ie8-beta-2-authentication-issues-with-host-header-configured-sites/
(currently 0 comments)