views:

330

answers:

3

I am trying to get some detailed performance information from an application my company is developing. Examples of information I am trying to get would be how long a network transaction takes, how much CPU/memory the application is using, how long it takes for a given method to complete, etc.

I have had some failed attempts at this in the past (like trying to measure small time periods by using DateTime.Now). On top of that I don't know much of anything about getting CPU and memory statistics. Are there any good .Net classes, libraries, or frameworks out there that would help me collect this sort of information and/or log it to a text file?

+6  A: 

What you are looking for is Performance Counters. For .net you need this Performance Counter.

TheUnknownDotNet
A: 

Performance counters are one way to go, and the System.Diagnostics.Stopwatch class are good foundational places to look for doing this.

With performance counters (beyond those provided) you will need to manage both the infrastructure of tracking the events, as well as reporting the data. The performance counter base classes supply the connection details for hooking up to the event log, but you will need to provide other reporting infrastructure if you need to report the data in another way (such as to a log file, or database).

The stopwatch class is a wrapper around the high performance timer, giving you microsecond or nanosecond resolution depending on the processor or the platform. If you do not need that high of resolution you can use System.DateTime..Now.Ticks to get the current tick count for the processor clock and do differential math with that, giving you millisecond or bettter precision for most operations.

When tracking CPU statistics be aware that multiple processors and multiple cores will complicate any accurate statistics in some cases.

One last caution with performance counters, be aware that not all performance counters are on all machines. For instance ASP.NET counters are not present on a machine which does not have IIS installed, etc.

GrayWizardx
Naming failure: THe stopwatch is not a Performance Counter.
Henk Holterman
High performance timer. Thanks for the correction.
GrayWizardx
A: 

For cpu/memory use performance counters. For more specific information about specific methods (or lines of code) and specific objects, use a profiler. red-gate makes a great profiler.

http://www.red-gate.com/products/ants_performance_profiler

Sam