PerformanceHelper.cs Update: Perfmon, Console, and Debug Output options

I’ve updated my PerformanceHelper class that I’ll throw around certain code blocks to help trend or monitor performance.  Before it was merely a PerformanceCounter wrapper with a few metrics, but now you can specify Console, Debug and/or Perfmon.  You do this by using an XOR ( ‘|’ ) in a new Constructor.  Here’s an example which outputs to Debug.WriteLine and PerfMon:

for (int index = 0; index < 10; index++)
    using (new PerformanceHelper("ConsoleSample",
        "Perfmon Sample", OutputOptions.Debug | OutputOptions.Perfmon))

The method that does the Output to Console and Debug is marked as [Conditional(“DEBUG”)], because you really should have a better strategy for performance monitoring in production and you don’t want that hit for each block that you wrapped.

Note to Vista Developers:  You will have problems creating custom performance counters at runtime based on registry access.  To support this in a client based app, you will need to specify an application manifest and mark it with the following:  

<requestedExecutionLevel  level="requireAdministrator" />

For more information on application manifests, read this.

PerformanceHelper for easy PerformanceCounter integration

In the Speedy Rewards team here we use custom PerformanceCounters quite extensively to monitor and trend our host systems.  You may not know, but our rewards program implements real-time host integration for transactions that are Id’ed as a SpeedyRewards member such that systems like the in-store kiosk,, and any other store you may soon visit can accurately reflect balance updates immediately.  With 1600+ stores doing this and a sub-1 second response time, we need to keep a close eye on things.

Today I was trying to do some preliminary performance profiling on our new WCF based messaging infrastructure.  To make my life a little easier, I put together a PerformanceHelper class that I actually modeling off the Tracer class found in the Enterprise Library 1.1.  To use it, you wrap a section of code you wish to monitor performance for with the following:

using(new PerformanceHelper(CATEGORY, "<BlockInstanceName>") )

This will provide you with a RateOfCountsPerSecond32 counter and an AverageTimer32 counter called “Operations/sec\<BlockInstanceName>” and “Average Time/Operation\<BlockInstanceName>” respectively.  You can download the PerformanceHelper class here and a sample project here

Feel free to use and abuse it, but be aware that this has not fully tested and may well contain bugs.  I provide NO warranty or guarantees, so don’t come to me when your server blue-screens from a corrupt registry (not that I’ve ever done that).  Use at your own peril.