How to: Retrieve Performance Counter SamplesĀ
You use the CounterSample class to create samples and perform calculations on their contents. A sample class performs a "sampling" of a performance counter based on criteria you define. These criteria include the values from one or more counters and the frequency with which values should be provided. In addition, the class records the time at which samples were taken. You can collect all of this data in a single instantiated class and then use the Calculate method to perform a calculation. You can also use the Calculate method to perform a calculation that compares values in two different samples.
The calculation performed depends on the type of the counters; certain counter types have specific calculations associated with them. For example, counters of the type ElapsedTime compare the time stamp on two different samples and determine how much time has elapsed. Many counters perform an averaging calculation based on the data retrieved.
These are the steps you go through to define a sample:
Create one or more instances of the CounterSample class.
Retrieve a current sample for each instance.
Call the Calculate method, passing each sample to be included in the calculation as a parameter.
To retrieve performance counter samples and perform a calculation
Create a PerformanceCounter instance and configure it to interact with the desired category and counter. For more information, see How to: Create PerformanceCounter Component Instances or How to: Configure PerformanceCounter Component Instances.
Create an instance of the CounterSample class to hold the results of the sampling.
Call the NextSample method on the PerformanceCounter component instance to retrieve the calculated value, and assign the result to the CounterSample class.
Tip You must retrieve two samples to perform a calculation.
Create a variable to hold the results of the calculation, and give it a data type of Single.
For each of the two samples that you need for your calculation, assign a return value of NextSample to a variable of type CounterSample.
Call the Calculate method on the CounterSample class, and do one of two things:
If you have retrieved two samples, pass both of the samples (which are stored as CounterSample objects) to the Calculate method as parameters.
- or -
If you have retrieved only one sample, pass the first sample to the Calculate method, and then use the second parameter to retrieve another sample.
Set the result of the calculation equal to the variable you created to contain the result.
Note The two samples must be from counters of the same type or the method will throw an exception. The type of counter determines what kind of calculation performed. For more information, see Performance Counter Types.
The following code illustrates how to retrieve two samples and compare them using the Calculate method:
' Dim variables of types CounterSample for each sample and a ' variable of type single for the results. Dim sample1 As CounterSample Dim sample2 As CounterSample Dim result As Single ' Retrieve a sample. sample1 = PerformanceCounter1.NextSample() ' Wait some interval of time here and retrieve ' a second sample. System.Threading.Thread.Sleep(1000) sample2 = PerformanceCounter1.NextSample() ' Pass both samples to the Calculate method. result = CounterSample.Calculate(sample1, sample2)
System.Diagnostics.CounterSample sample1; System.Diagnostics.CounterSample sample2; float result; // Retrieve a sample. sample1 = PerformanceCounter1.NextSample(); // Wait some interval of time here and retrieve // a second sample. System.Threading.Thread.Sleep(1000); sample2 = PerformanceCounter1.NextSample(); // Pass both samples to the Calculate method. result = System.Diagnostics.CounterSample.Calculate(sample1, sample2);
// Assumes a using statement for System.Diagnostics.dll. System.Diagnostics.CounterSample sample1; System.Diagnostics.CounterSample sample2; float result; // Retrieve a sample. sample1 = PerformanceCounter1.NextSample(); // Wait some interval of time here and retrieve // a second sample. System.Threading.Thread.Sleep(1000); sample2 = PerformanceCounter1.NextSample(); // Pass both samples to the Calculate method. result = System.Diagnostics.CounterSample.Calculate(sample1, sample2);
Alternatively, you can call the NextSample method to provide the values for the second sample. The following example illustrates using this approach:
Dim sample1 As CounterSample Dim result As Single ' Retrieve a single sample. sample1 = PerformanceCounter1.NextSample() ' Pass the retrieved sample to the calculate method ' and retrieve another sample in the second parameter. result = CounterSample.Calculate(sample1, PerformanceCounter1.NextSample())
System.Diagnostics.CounterSample sample1; float result; // Retrieve a single sample. sample1 = PerformanceCounter1.NextSample(); // Pass the retrieved sample to the calculate method // and retrieve another sample in the second parameter. result = System.Diagnostics.CounterSample.Calculate(sample1, PerformanceCounter1.NextSample());
System.Diagnostics.CounterSample sample1 = new System.Diagnostics.CounterSample(); float result; // Retrieve a single sample. sample1 = PerformanceCounter1.NextSample(); // Pass the retrieved sample to the calculate method // and retrieve another sample in the second parameter. result = System.Diagnostics.CounterSample.Calculate(sample1, PerformanceCounter1.NextSample());
See Also
Tasks
How to: Retrieve Raw Performance Counter Values
How to: Retrieve Calculated Performance Counter Values
How to: Retrieve Lists of Counters and Categories