Performance Counter Value Retrieval
There are several ways you can read performance counter values:
You can retrieve a raw value from a counter using theproperty on the class.
You can retrieve the current calculated value for a counter using themethod on the PerformanceCounter class.
You can retrieve a set of samples using the NextValue method on the PerformanceCounter class, and compare their values using themethod.
There are security restrictions that affect your ability to use performance counters. For more information, see.
Raw, Calculated, and Sampled Data
The interaction between raw values, next (or calculated) values, and samples is fairly straightforward once you understand that raw and calculated values shift constantly, whereas samples allow you to retrieve a static snapshot of the counter at a particular point in time. The following diagram illustrates the relationship between raw value, next value, and samples.
This picture shows a representation of the data contained in a counter called Orders Per Second. The raw values for this counter are individual data points that vary by second, where the calculated average is represented by the line showing an increasing order receipt over time. In this chart, the following data points have been taken:
The user has used the NextValue method to retrieve the calculated value at three different times, represented by NV1, NV2, and NV3. Because the next value is constantly changing, a different value is retrieved each time without specifying any additional parameters.
The user has used themethod to take two samples, indicated by S1 and S2. Samples freeze a value in time, so the user can then compare the two sample values and perform calculations on them.
Comparing Retrieval Methods
Retrieving a raw value with the RawValue property is very quick, because no calculations or comparisons are performed. For example, if you are using a counter simply to count the number of orders processed in a system, you can retrieve the counter's raw value. For more information on retrieving raw values, see.
Retrieving a calculated value with the NextValue method is often more useful than retrieving the raw value, but this value may also give you an unrealistic view of the data because it can reflect unusual fluctuations in the data at the moment when the value is calculated. For example, if you have a counter that calculates the orders processed per second, an unusually high or low amount of orders processed at one particular moment will result in an average that is not realistic over time. This may provide a distorted view of the actual performance of your system. For more information on retrieving a calculated value, see.
Samples provide the most realistic views of the data in your system by allowing you to retrieve, retain, and compare various values over time. You would retrieve a sample, using the NextSample method, if you needed to compare values in different counters or calculate a value based on raw data. This may be slightly more resource-intensive, however, than a NextValue call. For more information on retrieving a sample, see.
The NextSample method returns an object of type. When you retrieve a sample, you have access to properties on the CounterSample class such as , , , and . These properties let you get a very detailed look at the data that makes up the sample data.