Performance Counters Determine Application PerformanceThis article covers performance counters and how to use them to your advantage within your applications. It starts by explaining why providing performance instrumentation is important and follows with some examples of setting up and using performance counters. It primarily involves using classes in the System.Diagnostics namespace.
Instrumentation often refers to measuring the performance of an application: the application's ability to monitor and report on key performance indicators. These indicators help to determine the overall performance of an application and can also help diagnose those pesky "undocumented features" (a.k.a. errors or bugs). The following items typically comprise complete instrumentation:
- Debugging—This feature allows you to print out conditional debugging information that can be left in the code at all times and excluded automatically when compiling in release mode. This allows you to have complete debugging messages available to you during development and testing and not have to change code prior to production release. It is facilitated through the System.Diagnostics.Debug class.
- Tracing—This feature allows you to monitor the status of your application through conditional statements. Similar to debugging, it involves the use of output messages from the application to display state information. However, tracing statements are generally included in the production release of an application and are enabled when needed, where debug messages are generally excluded. It is facilitated through the System.Diagnostics.Trace class.
- Logging—This feature involves writing information on major events related to your application, such as errors. Common places to log information include the Windows Event Log, a file, or a database.
- Performance counters—These features can monitor physical system components such as processors, memory, and networks, and if used within your application, can capture and publish performance-related data about that application. The published counter information is captured and you can then compare it against acceptable performance criteria. Performance counters are available on modern versions of Microsoft operating systems, such as Windows 2000 and XP.
You can use any combination of the above items to complete instrumentation within your applications. However, this article focuses strictly on performance counters.
Basics of Performance Counters
Related performance counters are grouped into categories. Existing Microsoft Windows categories include Processor and Memory. The Processor category, for example, contains all of the counters related to measuring the performance of the system's processor.
An instance is an optional further grouping of counters. Instances track data on multiple occurrences of an object the category represents. For example, a system with multiple processors would have an instance for each processor and another instance representing the total performance of all processors. That would enable you to monitor the performance behavior of any individual processor or all of them combined. Individual counters can be defined to measure whatever criteria you desire.
Performance Counter Types
A number of different performance counter types are available. They range from items that contain counts, to those that perform calculations such as averages. The following list contains a few of the more common ones that this article explores in its examples:
- AverageTimer32—a counter that measures the average time required to complete a process or operation. It is calculated by a ratio of total time elapsed to the number of items completed during that time.
- AverageBase—a supporting base counter that contains the number of items completed during the sample interval.
- NumberOfItems32—an instance counter that tracks a count of items. You can use it to monitor the number of times an operation is performed.
- RateOfCountsPerSecond32—a counter that tracks the number of items or operations per second.
The System.Diagnostics.PerformanceCounterType enumeration is a complete list of all of the different performance counter types available. A handful of counters are considered base counters, as indicated by "Base" appended to the end of the class name. The base counters are required, as they support other counters that perform calculations.