Why use performance counters?
I have good logging you say, excellent traceability in my database, if need be I can attach Ants Profiler, I don't need performance counters. Not true. Every application that runs on a server and serves multiple users should be load tested. A few definitions first:Load testing is not using the Performance Analyser in Visual Studio or Ants Profiler. This is merely looking for memory leaks and bad performance for a single user. Still good to do, but doesn't replace load testing.
To be able to load test and understand the results you need to be able to measure throughput and the most likely individual performance of specific tasks while the rest of the system is under load.
Performance counters are the easiest way to monitor throughput and performance of a running system in real time. It could be achieved with logging to files or database, but why bother? Most load testing tools capture performance monitor data and later analysis, and previous test data doesn't pollute later tests.
What else are they good for?
Even for single user applications performance counters can be a convenient way to measure individual intensive tasks within the application. Its also a good way to measure if running tasks in parallel is better than not.Why not, they're easy to use and there's some free stuff
There's a huge array of standard performance counters that come with the OS and with .NET, that are very useful. Counters to measure processor utilisation, number of logical threads created, memory usage, GC stats, and ASP.Net requests per second, and % of WCF throttles used. Basically only code that you have written inside your app won't be measured. Writing your own performance counters is dead easy, and there isn't much code that invades into your source to make it happen.var perfCounter = new PerformanceCounter("My App Name", "Counter 1", false); perfCounter.Increment();
Watching Performance Counters
To view performance counters and watch live data coming through use PerfMon. This is part of Windows OS.Gotchas:
- You may need to reboot after installing a performance counter with the OS. It may appear in the PerfMon list, but no data comes through. A reboot will likely fix this, it did for me.
- Admin priviledges will be required to install custom performance counters into the OS and delete them. This works in code, but isn't a great idea to run / require production apps to use admin credentials. To update performance counter data the app only needs to be a member of the Performance Log Users Group.
- There are some random gaps sometimes in the counters, but in my experience never more than a few minutes.
Download my sample code