Volume 26 Number 07
Toolbox - Tools and Techniques for .NET Code Profiling
By Terrence Dorsey | July 2011
The pithy epigram “premature optimization is the root of all evil” has been variously attributed over the years to Donald Knuth, William Wulf and C.A.R. Hoare. (It appears that Hoare originated the phrase, but Knuth gets credit for popularizing it. See bit.ly/fW1jWE for a summary of Knuth’s thoughts on the subject.)
So what does this mean to you, the intrepid .NET developer? Should you ignore performance and code away to your heart’s content? Is it best to follow the whims of IntelliSense and rely on ever-faster hardware to scale your apps?
An important part of testing is making sure your application not only executes without errors, but also executes efficiently and responsively. That’s where code-profiling tools and techniques come into play. These let you, as part of the build and testing process, evaluate your code for constructions—and outright errors—that are likely to cause problems. You get an automated heads-up that points you directly at the places in your app that need refactoring.
A good place to start learning about .NET profiling is the Code Project article by Paul Glavich, “Profiling the Performance of a .NET Application” (bit.ly/fpua6i). You’ll not only get some basics on .NET profiling, but Glavich also introduces you to the ANTS Profiler (which we’ll get to a bit later).
Another great starting point is Amirthalingam Prasanna’s blog post, “Profiling Your .NET Code” (bit.ly/dDXWsF). He starts out by listing 10 reasons why you should profile your code, then walks you through the process using the CLR Profiler (bit.ly/eSJyWd).
In Brian Long’s extensive walkthrough of the Microsoft .NET Framework profiling APIs, “.NET Internals: The Profiling API” (bit.ly/hNEDYP), you’ll learn about performance counters, the Performance Monitor and the relevant APIs. You’ll also see how to create your own simple profiling tools to illustrate how the APIs work.
The CLR now provides extensive profiling support, and David Broman shows you how to use it in his blog post, “Profilers, in-process side-by-side CLR instances, and a free test harness” (bit.ly/dYeRnQ). Make sure to read through the archives of Broman’s site—there’s a lot of great information there.
Profiling gets really important when you’re doing high-performance, highly scaled programming. If that’s up your alley, check out the “SC08: Windows HPC: Multi-Core Parallel Code Profiling in VS2010” webcast on Channel 9 (bit.ly/gyeKPi).
Don’t miss the Visual Studio Profiler Team Blog (blogs.msdn.com/b/profiler) for profiling tips, tricks and late-breaking announcements.
Writing More Efficient Code
Of course, one way to avoid the pain of testing, profiling and refactoring your code repeatedly is to write more efficient code in the first place. Here are three articles that discuss best practices for writing apps that will, hopefully, need a few less runs through the profiler to achieve the speed you hoped for:
- “Guide to Improving Code Performance in .NET: Part I” by Satesh Arveti on C# Corner (bit.ly/gyImk9)
- “Writing Efficient C and C Code Optimization” by Koushik Ghosh on Code Project (bit.ly/icnYEi)
- “Writing High Performance .NET Code” by Juan A Rodriguez and Simonijt Dutta from Intel (intel.ly/fvweaP)
This is the Toolbox column, so let’s talk tools. Software-profiling utilities perform dynamic analysis of your application code while it’s running. Here are a few free and commercial offerings you might want to take for a spin.
AMD CodeAnalyst Performance Analyzer (https://developer.amd.com/tools/heterogeneous-computing/amd-codeanalyst-performance-analyzer/) is a free profiling tool from—you guessed it—Advanced Micro Devices Inc. that lets you profile C/C++, Fortran, Java and .NET code. It’s particularly designed for optimizing your code for multithreaded executing on AMD chipsets. CodeAnalyst integrates with Visual Studio 2003 through 2010 and runs on systems from Windows XP through Windows 7 in both x86 and AMD64 architectures.
EQATEC Profiler is a code profiler designed specifically for making your .NET apps run faster. It supports the .NET Framework 2.0 and later, Windows Presentation Foundation (WPF), Silverlight and the .NET Compact Framework. It’s also the first profiler to offer Windows Phone 7 profiling features both in the emulator and on devices. Free and paid licenses are available. See the site for details.
JetBrains dotTrace (jetbrains.com/profiler) is actually a collection of two lightweight .NET profiling apps: dotTrace4 4 Performance and dotTrace 3.5 Memory. dotTrace lets you target apps using the .NET Framework 1.0 through 4, .NET Compact Framework 3.5 and Silverlight 4. You can run tracing, sampling or line-by-line analysis on both local and remote systems. dotTrace integrates with Visual Studio 2005 through 2010. JetBrains currently offers a time-limited free trial and a number of licensing options. See the site for details.
Red Gate ANTS Performance Profiler (bit.ly/g1yVEt) and ANTS Memory Profiler (bit.ly/h3JzAX) are another set of commercial profiling tools that analyze Windows Forms apps, ASP.NET Web apps, SharePoint and Silverlight 4. With the .NET Framework 4, ANTS Performance Profiler supports CLR profiling so you can attach to running processes. You can also get performance data for SQL queries triggered from your code. Both products support the .NET Framework 1.0 through 4 and Windows XP through Windows 7, and can be run directly from Visual Studio. See the Red Gate site for pricing.
SmartBear AQtime Pro (bit.ly/ePmENJ) is a performance profiling and memory and resource debugging program for targeting the .NET Framework, Java, JScript and VBScript code. You can use AQtime Pro to profile both 32- and 64-bit applications, and the profiler can be directly integrated into Visual Studio 2002 through 2010 and Embarcadero RAD Studio development environments. AQtime Pro also provides a programmable debugger using a COM-based architecture so you can create custom profilers. Download a time-limited free trial, or contact Smart Bear for current pricing.
SmartBear AQtime Pro
SlimTune (code.google.com/p/slimtune) is a free, open source profiling and performance-analysis tool for .NET development. Development is currently in beta, and though you can target both x86 and x64 applications, only sample-based profiling is available. Source code is available if you want to hack in it yourself.
Electric Software GlowCode (glowcode.com) is another commercial performance and memory profiler targeted at 32- and 64-bit managed, native and mixed code written in C, C++ or any .NET Framework-compliant language. GlowCode integrates directly with Visual Studio 2010 so you can profile your app without leaving the IDE. A time-limited evaluation license is available along with single-user and floating licenses. Check the Web site for details.
Data Access Profiling
The data layer of your application needs love, too. Here are a few tools and resources for profiling data access.
If you’re using SQL Server 2008 R2, SQL Server Profiler is included as part of the installation; it can analyze queries, Transact-SQL statements and expressions, and so on. Other great resources for getting started with SQL Server Profiler include:
- “Using SQL Server Profiler” in the MSDN Library (msdn.microsoft.com/library/ms187929)
- “Introduction to SQL Server 2008 Profiler” by Pinal Dave on dotnetslackers.com (bit.ly/g5IDA0)
- “Step-By-Step: An introduction to SQL Server Profiler” by Susan Harkins at TechRepublic (tek.io/hTV6Kh)
If you’re using SQL Server 2005 or 2008 Express Edition, however, you won’t have access to the profiling tools included in the full version of SQL Server. In that case, you’ll want to take a look at the free AnjLab open source SQL Server Express Edition Profiler (bit.ly/eNg5oi).
Entity Framework Profiler (efprof.com) is designed for analyzing and real-time debugging data access in your code using the Entity Framework. It’s a great tool for uncovering what’s really happening behind the scenes. Entity Framework Profiler is a commercial product, but you can request a 30-day trial license to give it a whirl on your own projects.
Entity Framework Profiler
SQL Load Test (sqlloadtest.codeplex.com) is a CodePlex project created by the Visual Studio Team System (VSTS) Ranger team to generate unit tests from SQL Profiler traces and replay the database calls from the trace using Visual Studio Load Test. It’s a handy tool for testing data-centric apps that aren’t directly load-testable.
Finally, don’t forget LINQPad (linqpad.net) as a handy tool for evaluating LINQ queries as well as your C# and Visual Basic code. While not really a profiler, it’s a handy way to work through iterations of your code outside the context of your project.
Terrence Dorsey is the technical editor of MSDN Magazine*. You can read his blog at terrencedorsey.com or follow him on Twitter at twitter.com/tpdorsey*.