PerformanceCounterType Enumeration
TOC
Collapse the table of content
Expand the table of content

PerformanceCounterType Enumeration

 

Specifies the formula used to calculate the NextValue method for a PerformanceCounter instance.

Namespace:   System.Diagnostics
Assembly:  System (in System.dll)

public enum class PerformanceCounterType

Member nameDescription
AverageBase

A base counter that is used in the calculation of time or count averages, such as AverageTimer32 and AverageCount64. Stores the denominator for calculating a counter to present "time per operation" or "count per operation".

AverageCount64

An average counter that shows how many items are processed, on average, during an operation. Counters of this type display a ratio of the items processed to the number of operations completed. The ratio is calculated by comparing the number of items processed during the last interval to the number of operations completed during the last interval.

AverageTimer32

An average counter that measures the time it takes, on average, to complete a process or operation. Counters of this type display a ratio of the total elapsed time of the sample interval to the number of processes or operations completed during that time. This counter type measures time in ticks of the system clock.

CounterDelta32

A difference counter that shows the change in the measured attribute between the two most recent sample intervals.

CounterDelta64

A difference counter that shows the change in the measured attribute between the two most recent sample intervals. It is the same as the CounterDelta32 counter type except that is uses larger fields to accomodate larger values.

CounterMultiBase

A base counter that indicates the number of items sampled. It is used as the denominator in the calculations to get an average among the items sampled when taking timings of multiple, but similar items. Used with CounterMultiTimer, CounterMultiTimerInverse, CounterMultiTimer100Ns, and CounterMultiTimer100NsInverse.

CounterMultiTimer

A percentage counter that displays the active time of one or more components as a percentage of the total time of the sample interval. Because the numerator records the active time of components operating simultaneously, the resulting percentage can exceed 100 percent.

CounterMultiTimer100Ns

A percentage counter that shows the active time of one or more components as a percentage of the total time of the sample interval. It measures time in 100 nanosecond (ns) units.

CounterMultiTimer100NsInverse

A percentage counter that shows the active time of one or more components as a percentage of the total time of the sample interval. Counters of this type measure time in 100 nanosecond (ns) units. They derive the active time by measuring the time that the components were not active and subtracting the result from multiplying 100 percent by the number of objects monitored.

CounterMultiTimerInverse

A percentage counter that shows the active time of one or more components as a percentage of the total time of the sample interval. It derives the active time by measuring the time that the components were not active and subtracting the result from 100 percent by the number of objects monitored.

CounterTimer

A percentage counter that shows the average time that a component is active as a percentage of the total sample time.

CounterTimerInverse

A percentage counter that displays the average percentage of active time observed during sample interval. The value of these counters is calculated by monitoring the percentage of time that the service was inactive and then subtracting that value from 100 percent.

CountPerTimeInterval32

An average counter designed to monitor the average length of a queue to a resource over time. It shows the difference between the queue lengths observed during the last two sample intervals divided by the duration of the interval. This type of counter is typically used to track the number of items that are queued or waiting.

CountPerTimeInterval64

An average counter that monitors the average length of a queue to a resource over time. Counters of this type display the difference between the queue lengths observed during the last two sample intervals, divided by the duration of the interval. This counter type is the same as CountPerTimeInterval32 except that it uses larger fields to accommodate larger values. This type of counter is typically used to track a high-volume or very large number of items that are queued or waiting.

ElapsedTime

A difference timer that shows the total time between when the component or process started and the time when this value is calculated.

NumberOfItems32

An instantaneous counter that shows the most recently observed value. Used, for example, to maintain a simple count of items or operations.

NumberOfItems64

An instantaneous counter that shows the most recently observed value. Used, for example, to maintain a simple count of a very large number of items or operations. It is the same as NumberOfItems32 except that it uses larger fields to accommodate larger values.

NumberOfItemsHEX32

An instantaneous counter that shows the most recently observed value in hexadecimal format. Used, for example, to maintain a simple count of items or operations.

NumberOfItemsHEX64

An instantaneous counter that shows the most recently observed value. Used, for example, to maintain a simple count of a very large number of items or operations. It is the same as NumberOfItemsHEX32 except that it uses larger fields to accommodate larger values.

RateOfCountsPerSecond32

A difference counter that shows the average number of operations completed during each second of the sample interval. Counters of this type measure time in ticks of the system clock.

RateOfCountsPerSecond64

A difference counter that shows the average number of operations completed during each second of the sample interval. Counters of this type measure time in ticks of the system clock. This counter type is the same as the RateOfCountsPerSecond32 type, but it uses larger fields to accommodate larger values to track a high-volume number of items or operations per second, such as a byte-transmission rate.

RawBase

A base counter that stores the denominator of a counter that presents a general arithmetic fraction. Check that this value is greater than zero before using it as the denominator in a RawFraction value calculation.

RawFraction

An instantaneous percentage counter that shows the ratio of a subset to its set as a percentage. For example, it compares the number of bytes in use on a disk to the total number of bytes on the disk. Counters of this type display the current percentage only, not an average over time.

SampleBase

A base counter that stores the number of sampling interrupts taken and is used as a denominator in the sampling fraction. The sampling fraction is the number of samples that were 1 (or true) for a sample interrupt. Check that this value is greater than zero before using it as the denominator in a calculation of SampleFraction.

SampleCounter

An average counter that shows the average number of operations completed in one second. When a counter of this type samples the data, each sampling interrupt returns one or zero. The counter data is the number of ones that were sampled. It measures time in units of ticks of the system performance timer.

SampleFraction

A percentage counter that shows the average ratio of hits to all operations during the last two sample intervals.

Timer100Ns

A percentage counter that shows the active time of a component as a percentage of the total elapsed time of the sample interval. It measures time in units of 100 nanoseconds (ns). Counters of this type are designed to measure the activity of one component at a time.

Timer100NsInverse

A percentage counter that shows the average percentage of active time observed during the sample interval.

Some counter types represent raw data, while others represent calculated values that are based on one or more counter samples. The following categories classify the types of counters available.

  • Average: measure a value over time and display the average of the last two measurements. Associated with each average counter is a base counter that tracks the number of samples involved.

  • Difference: subtract the last measurement from the previous one and display the difference, if it is positive; if negative, they display a zero.

  • Instantaneous: display the most recent measurement.

  • Percentage: display calculated values as a percentage.

  • Rate: sample an increasing count of events over time and divide the change in count values by the change in time to display a rate of activity.

When sampling performance counter data, using a counter type that represents an average can make raw data values meaningful for your use. For example, the raw data counter NumberOfItems64 can expose data that is fairly random from sample to sample. The formula for an average calculation of the values that the counter returns would be (X 0 +X 1 +…+X n)/n, where each X i is a raw counter sample.

Rate counters are similar to average counters, but more useful for situations in which the rate increases as a resource is used. A formula that quickly calculates the average is ((X n -X 0)/(T n -T 0)) / frequency, where each X i is a counter sample and each T i is the time that the corresponding sample was taken. The result is the average usage per second.

System_CAPS_noteNote

Unless otherwise indicated, the time base is seconds.

When instrumenting applications (creating and writing custom performance counters), you might be working with performance counter types that rely on an accompanying base counter that is used in the calculations. The base counter must be immediately after its associated counter in the CounterCreationDataCollection collection your application uses. The following table lists the base counter types with their corresponding performance counter types.

Base counter type

Performance counter types

AverageBase

AverageTimer32

AverageCount64

CounterMultiBase

CounterMultiTimer

CounterMultiTimerInverse

CounterMultiTimer100Ns

CounterMultiTimer100NsInverse

RawBase

RawFraction

SampleBase

SampleFraction

The following examples demonstrate several of the counter types in the PerformanceCounterType enumeration.

AverageCount64

#using <System.dll>

using namespace System;
using namespace System::Collections;
using namespace System::Collections::Specialized;
using namespace System::Diagnostics;

// Output information about the counter sample.
void OutputSample( CounterSample s )
{
   Console::WriteLine( "\r\n+++++++++++" );
   Console::WriteLine( "Sample values - \r\n" );
   Console::WriteLine( "   BaseValue        = {0}", s.BaseValue );
   Console::WriteLine( "   CounterFrequency = {0}", s.CounterFrequency );
   Console::WriteLine( "   CounterTimeStamp = {0}", s.CounterTimeStamp );
   Console::WriteLine( "   CounterType      = {0}", s.CounterType );
   Console::WriteLine( "   RawValue         = {0}", s.RawValue );
   Console::WriteLine( "   SystemFrequency  = {0}", s.SystemFrequency );
   Console::WriteLine( "   TimeStamp        = {0}", s.TimeStamp );
   Console::WriteLine( "   TimeStamp100nSec = {0}", s.TimeStamp100nSec );
   Console::WriteLine( "++++++++++++++++++++++" );
}

//++++++++//++++++++//++++++++//++++++++//++++++++//++++++++//++++++++//++++++++
//    Description - This counter type shows how many items are processed, on average,
//        during an operation. Counters of this type display a ratio of the items 
//        processed (such as bytes sent) to the number of operations completed. The  
//        ratio is calculated by comparing the number of items processed during the 
//        last interval to the number of operations completed during the last interval. 
// Generic type - Average
//      Formula - (N1 - N0) / (D1 - D0), where the numerator (N) represents the number 
//        of items processed during the last sample interval and the denominator (D) 
//        represents the number of operations completed during the last two sample 
//        intervals. 
//    Average (Nx - N0) / (Dx - D0)  
//    Example PhysicalDisk\ Avg. Disk Bytes/Transfer 
//++++++++//++++++++//++++++++//++++++++//++++++++//++++++++//++++++++//++++++++
float MyComputeCounterValue( CounterSample s0, CounterSample s1 )
{
   float numerator = (float)s1.RawValue - (float)s0.RawValue;
   float denomenator = (float)s1.BaseValue - (float)s0.BaseValue;
   float counterValue = numerator / denomenator;
   return counterValue;
}

bool SetupCategory()
{
   if (  !PerformanceCounterCategory::Exists( "AverageCounter64SampleCategory" ) )
   {
      CounterCreationDataCollection^ CCDC = gcnew CounterCreationDataCollection;

      // Add the counter.
      CounterCreationData^ averageCount64 = gcnew CounterCreationData;
      averageCount64->CounterType = PerformanceCounterType::AverageCount64;
      averageCount64->CounterName = "AverageCounter64Sample";
      CCDC->Add( averageCount64 );

      // Add the base counter.
      CounterCreationData^ averageCount64Base = gcnew CounterCreationData;
      averageCount64Base->CounterType = PerformanceCounterType::AverageBase;
      averageCount64Base->CounterName = "AverageCounter64SampleBase";
      CCDC->Add( averageCount64Base );

      // Create the category.
      PerformanceCounterCategory::Create( "AverageCounter64SampleCategory", "Demonstrates usage of the AverageCounter64 performance counter type.", CCDC );
      return (true);
   }
   else
   {
      Console::WriteLine( "Category exists - AverageCounter64SampleCategory" );
      return (false);
   }
}

void CreateCounters( PerformanceCounter^% PC, PerformanceCounter^% BPC )
{

   // Create the counters.
   PC = gcnew PerformanceCounter( "AverageCounter64SampleCategory","AverageCounter64Sample",false );

   BPC = gcnew PerformanceCounter( "AverageCounter64SampleCategory","AverageCounter64SampleBase",false );
   PC->RawValue = 0;
   BPC->RawValue = 0;
}
void CollectSamples( ArrayList^ samplesList, PerformanceCounter^ PC, PerformanceCounter^ BPC )
{
   Random^ r = gcnew Random( DateTime::Now.Millisecond );

   // Loop for the samples.
   for ( int j = 0; j < 100; j++ )
   {
      int value = r->Next( 1, 10 );
      Console::Write( "{0} = {1}", j, value );
      PC->IncrementBy( value );
      BPC->Increment();
      if ( (j % 10) == 9 )
      {
         OutputSample( PC->NextSample() );
         samplesList->Add( PC->NextSample() );
      }
      else
            Console::WriteLine();
      System::Threading::Thread::Sleep( 50 );
   }
}

void CalculateResults( ArrayList^ samplesList )
{
   for ( int i = 0; i < (samplesList->Count - 1); i++ )
   {
      // Output the sample.
      OutputSample(  *safe_cast<CounterSample^>(samplesList[ i ]) );
      OutputSample(  *safe_cast<CounterSample^>(samplesList[ i + 1 ]) );

      // Use .NET to calculate the counter value.
      Console::WriteLine( ".NET computed counter value = {0}", CounterSampleCalculator::ComputeCounterValue(  *safe_cast<CounterSample^>(samplesList[ i ]),  *safe_cast<CounterSample^>(samplesList[ i + 1 ]) ) );

      // Calculate the counter value manually.
      Console::WriteLine( "My computed counter value = {0}", MyComputeCounterValue(  *safe_cast<CounterSample^>(samplesList[ i ]),  *safe_cast<CounterSample^>(samplesList[ i + 1 ]) ) );
   }
}

int main()
{
   ArrayList^ samplesList = gcnew ArrayList;
   PerformanceCounter^ PC;
   PerformanceCounter^ BPC;
   SetupCategory();
   CreateCounters( PC, BPC );
   CollectSamples( samplesList, PC, BPC );
   CalculateResults( samplesList );
}

AverageTimer32

#using <System.dll>

using namespace System;
using namespace System::Collections;
using namespace System::Collections::Specialized;
using namespace System::Diagnostics;
using namespace System::Runtime::InteropServices;

//++++++++//++++++++//++++++++//++++++++//++++++++//++++++++//+++++++
// PERF_AVERAGE_TIMER
//  Description - This counter type measures the time it takes, on 
//     average, to complete a process or operation. Counters of this
//     type display a ratio of the total elapsed time of the sample 
//     interval to the number of processes or operations completed
//     during that time. This counter type measures time in ticks 
//     of the system clock. The F variable represents the number of
//     ticks per second. The value of F is factored into the equation
//     so that the result can be displayed in seconds.
//    
//  Generic type - Average
//    
//  Formula - ((N1 - N0) / F) / (D1 - D0), where the numerator (N)
//     represents the number of ticks counted during the last 
//     sample interval, F represents the frequency of the ticks, 
//     and the denominator (D) represents the number of operations
//     completed during the last sample interval.
//    
//  Average - ((Nx - N0) / F) / (Dx - D0)
//    
//  Example - PhysicalDisk\ Avg. Disk sec/Transfer 
//++++++++//++++++++//++++++++//++++++++//++++++++//++++++++//+++++++
float MyComputeCounterValue( CounterSample s0, CounterSample s1 )
{
    __int64 n1 = s1.RawValue;
    __int64 n0 = s0.RawValue;
    unsigned __int64 f = s1.SystemFrequency;
    __int64 d1 = s1.BaseValue;
    __int64 d0 = s0.BaseValue;
    double numerator = (double)(n1 - n0);
    double denominator = (double)(d1 - d0);
    float counterValue = (float)((numerator / f) / denominator);
    return counterValue;
}

// Output information about the counter sample.
void OutputSample( CounterSample s )
{
    Console::WriteLine( "+++++++++++" );
    Console::WriteLine( "Sample values - \r\n" );
    Console::WriteLine( "   CounterType      = {0}", s.CounterType );
    Console::WriteLine( "   RawValue         = {0}", s.RawValue.ToString() );
    Console::WriteLine( "   BaseValue        = {0}", s.BaseValue.ToString() );
    Console::WriteLine( "   CounterFrequency = {0}", s.CounterFrequency.ToString() );
    Console::WriteLine( "   CounterTimeStamp = {0}", s.CounterTimeStamp.ToString() );
    Console::WriteLine( "   SystemFrequency  = {0}", s.SystemFrequency.ToString() );
    Console::WriteLine( "   TimeStamp        = {0}", s.TimeStamp.ToString() );
    Console::WriteLine( "   TimeStamp100nSec = {0}", s.TimeStamp100nSec.ToString() );
    Console::WriteLine( "++++++++++++++++++++++" );
}

bool SetupCategory()
{
    if (  !PerformanceCounterCategory::Exists( "AverageTimer32SampleCategory") )
       {
        CounterCreationDataCollection^ CCDC = gcnew CounterCreationDataCollection;

        // Add the counter.
        CounterCreationData^ averageTimer32 = gcnew CounterCreationData;
        averageTimer32->CounterType = PerformanceCounterType::AverageTimer32;
        averageTimer32->CounterName = "AverageTimer32Sample";
        CCDC->Add( averageTimer32 );

        // Add the base counter.
        CounterCreationData^ averageTimer32Base = gcnew CounterCreationData;
        averageTimer32Base->CounterType = PerformanceCounterType::AverageBase;
        averageTimer32Base->CounterName = "AverageTimer32SampleBase";
        CCDC->Add( averageTimer32Base );

        // Create the category.
        PerformanceCounterCategory::Create( "AverageTimer32SampleCategory", 
            "Demonstrates usage of the AverageTimer32 performance counter type", 
            PerformanceCounterCategoryType::SingleInstance, CCDC );
        Console::WriteLine( "Category created - AverageTimer32SampleCategory" );
        return (true);
        }

    Console::WriteLine( "Category exists - AverageTimer32SampleCategory" );
    return (false);
}

void CreateCounters( PerformanceCounter^% PC, PerformanceCounter^% BPC )
{
    // Create the counters.
    PC = gcnew PerformanceCounter( "AverageTimer32SampleCategory","AverageTimer32Sample",false );
    BPC = gcnew PerformanceCounter( "AverageTimer32SampleCategory","AverageTimer32SampleBase",false );
    PC->RawValue = 0;
    BPC->RawValue = 0;
}

void CollectSamples( ArrayList^ samplesList, PerformanceCounter^ PC, 
PerformanceCounter^ BPC )
{
    __int64 perfTime = 0;
    Random^ r = gcnew Random( DateTime::Now.Millisecond );

    // Loop for the samples.
    for ( int i = 0; i < 10; i++ )
        {
        PC->RawValue = Stopwatch::GetTimestamp();
        BPC->IncrementBy( 10 );
        System::Threading::Thread::Sleep( 1000 );
        Console::WriteLine( "Next value = {0}", PC->NextValue().ToString() );
        samplesList->Add( PC->NextSample() );
        }
}

void CalculateResults( ArrayList^ samplesList )
{
    for ( int i = 0; i < (samplesList->Count - 1); i++ )
        {
        // Output the sample.
        OutputSample(  *safe_cast<CounterSample^>(samplesList[ i ]) );
        OutputSample(  *safe_cast<CounterSample^>(samplesList[ i + 1 ]) );

        // Use .NET to calculate the counter value.
        Console::WriteLine( ".NET computed counter value = {0}",
           CounterSample::Calculate(  *safe_cast<CounterSample^>(samplesList[ i ]),
           *safe_cast<CounterSample^>(samplesList[ i + 1 ]) ) );

        // Calculate the counter value manually.
        Console::WriteLine( "My computed counter value = {0}", 
            MyComputeCounterValue(  *safe_cast<CounterSample^>(samplesList[ i ]),
           *safe_cast<CounterSample^>(samplesList[ i + 1 ]) ) );
        }
}

int main()
{
    ArrayList^ samplesList = gcnew ArrayList;
    PerformanceCounter^ PC;
    PerformanceCounter^ BPC;
    SetupCategory();
    CreateCounters( PC, BPC );
    CollectSamples( samplesList, PC, BPC );
    CalculateResults( samplesList );

    Console::WriteLine("\n\nHit ENTER to return");
    Console::ReadLine();
}

ElapsedTime

#using <System.dll>

using namespace System;
using namespace System::Collections;
using namespace System::Collections::Specialized;
using namespace System::Diagnostics;
using namespace System::Runtime::InteropServices;

void OutputSample( CounterSample s )
{
   Console::WriteLine( "\r\n+++++++++++" );
   Console::WriteLine( "Sample values - \r\n" );
   Console::WriteLine( "   BaseValue        = {0}", s.BaseValue );
   Console::WriteLine( "   CounterFrequency = {0}", s.CounterFrequency );
   Console::WriteLine( "   CounterTimeStamp = {0}", s.CounterTimeStamp );
   Console::WriteLine( "   CounterType      = {0}", s.CounterType );
   Console::WriteLine( "   RawValue         = {0}", s.RawValue );
   Console::WriteLine( "   SystemFrequency  = {0}", s.SystemFrequency );
   Console::WriteLine( "   TimeStamp        = {0}", s.TimeStamp );
   Console::WriteLine( "   TimeStamp100nSec = {0}", s.TimeStamp100nSec );
   Console::WriteLine( "++++++++++++++++++++++" );
}

void CollectSamples()
{
   String^ categoryName = "ElapsedTimeSampleCategory";
   String^ counterName = "ElapsedTimeSample";

   // Create the performance counter category.
   if (  !PerformanceCounterCategory::Exists( categoryName ) )
   {
      CounterCreationDataCollection^ CCDC = gcnew CounterCreationDataCollection;

      // Add the counter.
      CounterCreationData^ ETimeData = gcnew CounterCreationData;
      ETimeData->CounterType = PerformanceCounterType::ElapsedTime;
      ETimeData->CounterName = counterName;
      CCDC->Add( ETimeData );

      // Create the category.
      PerformanceCounterCategory::Create( categoryName,
         "Demonstrates ElapsedTime performance counter usage.",
         CCDC );
   }
   else
   {
      Console::WriteLine( "Category exists - {0}", categoryName );
   }


   // Create the performance counter.
   PerformanceCounter^ PC = gcnew PerformanceCounter( categoryName,
                                                      counterName,
                                                      false );
   // Initialize the counter.
   PC->RawValue = Stopwatch::GetTimestamp();

   DateTime Start = DateTime::Now;

   // Loop for the samples.
   for ( int j = 0; j < 100; j++ )
   {
      // Output the values.
      if ( (j % 10) == 9 )
      {
         Console::WriteLine( "NextValue() = {0}", PC->NextValue() );
         Console::WriteLine( "Actual elapsed time = {0}", DateTime::Now.Subtract( Start ) );
         OutputSample( PC->NextSample() );
      }

      // Reset the counter on every 20th iteration.
      if ( j % 20 == 0 )
      {
         PC->RawValue = Stopwatch::GetTimestamp();
         Start = DateTime::Now;
      }
      System::Threading::Thread::Sleep( 50 );
   }

   Console::WriteLine( "Elapsed time = {0}", DateTime::Now.Subtract( Start ) );
}

int main()
{
   CollectSamples();
}

NumberOfItems32

#using <System.dll>

using namespace System;
using namespace System::Collections;
using namespace System::Collections::Specialized;
using namespace System::Diagnostics;
float MyComputeCounterValue( CounterSample s0, CounterSample s1 )
{
   float counterValue = (float)s1.RawValue;
   return counterValue;
}

// Output information about the counter sample.
void OutputSample( CounterSample s )
{
   Console::WriteLine( "\r\n+++++++++++" );
   Console::WriteLine( "Sample values - \r\n" );
   Console::WriteLine( "   BaseValue        = {0}", s.BaseValue );
   Console::WriteLine( "   CounterFrequency = {0}", s.CounterFrequency );
   Console::WriteLine( "   CounterTimeStamp = {0}", s.CounterTimeStamp );
   Console::WriteLine( "   CounterType      = {0}", s.CounterType );
   Console::WriteLine( "   RawValue         = {0}", s.RawValue );
   Console::WriteLine( "   SystemFrequency  = {0}", s.SystemFrequency );
   Console::WriteLine( "   TimeStamp        = {0}", s.TimeStamp );
   Console::WriteLine( "   TimeStamp100nSec = {0}", s.TimeStamp100nSec );
   Console::WriteLine( "++++++++++++++++++++++" );
}

bool SetupCategory()
{
   if (  !PerformanceCounterCategory::Exists( "NumberOfItems32SampleCategory" ) )
   {
      CounterCreationDataCollection^ CCDC = gcnew CounterCreationDataCollection;

      // Add the counter.
      CounterCreationData^ NOI64 = gcnew CounterCreationData;
      NOI64->CounterType = PerformanceCounterType::NumberOfItems64;
      NOI64->CounterName = "NumberOfItems32Sample";
      CCDC->Add( NOI64 );

      // Create the category.
      PerformanceCounterCategory::Create( "NumberOfItems32SampleCategory", "Demonstrates usage of the NumberOfItems32 performance counter type.", CCDC );
      return true;
   }
   else
   {
      Console::WriteLine( "Category exists - NumberOfItems32SampleCategory" );
      return false;
   }
}

void CreateCounters( PerformanceCounter^% PC )
{
   // Create the counter.
   PC = gcnew PerformanceCounter( "NumberOfItems32SampleCategory","NumberOfItems32Sample",false );
   PC->RawValue = 0;
}

void CollectSamples( ArrayList^ samplesList, PerformanceCounter^ PC )
{
   Random^ r = gcnew Random( DateTime::Now.Millisecond );

   // Loop for the samples.
   for ( int j = 0; j < 100; j++ )
   {
      int value = r->Next( 1, 10 );
      Console::Write( "{0} = {1}", j, value );
      PC->IncrementBy( value );
      if ( (j % 10) == 9 )
      {
         OutputSample( PC->NextSample() );
         samplesList->Add( PC->NextSample() );
      }
      else
            Console::WriteLine();
      System::Threading::Thread::Sleep( 50 );

   }
}

void CalculateResults( ArrayList^ samplesList )
{
   for ( int i = 0; i < (samplesList->Count - 1); i++ )
   {
      // Output the sample.
      OutputSample(  *safe_cast<CounterSample^>(samplesList[ i ]) );
      OutputSample(  *safe_cast<CounterSample^>(samplesList[ i + 1 ]) );

      // Use .NET to calculate the counter value.
      Console::WriteLine( ".NET computed counter value = {0}", CounterSampleCalculator::ComputeCounterValue(  *safe_cast<CounterSample^>(samplesList[ i ]),  *safe_cast<CounterSample^>(samplesList[ i + 1 ]) ) );

      // Calculate the counter value manually.
      Console::WriteLine( "My computed counter value = {0}", MyComputeCounterValue(  *safe_cast<CounterSample^>(samplesList[ i ]),  *safe_cast<CounterSample^>(samplesList[ i + 1 ]) ) );
   }
}

void main()
{
   ArrayList^ samplesList = gcnew ArrayList;
   PerformanceCounter^ PC;
   SetupCategory();
   CreateCounters( PC );
   CollectSamples( samplesList, PC );
   CalculateResults( samplesList );
}

NumberOfItems64

#using <System.dll>

using namespace System;
using namespace System::Collections;
using namespace System::Collections::Specialized;
using namespace System::Diagnostics;
float MyComputeCounterValue( CounterSample s0, CounterSample s1 )
{
   float counterValue = (float)s1.RawValue;
   return counterValue;
}


// Output information about the counter sample.
void OutputSample( CounterSample s )
{
   Console::WriteLine( "\r\n+++++++++++" );
   Console::WriteLine( "Sample values - \r\n" );
   Console::WriteLine( "   BaseValue        = {0}", s.BaseValue );
   Console::WriteLine( "   CounterFrequency = {0}", s.CounterFrequency );
   Console::WriteLine( "   CounterTimeStamp = {0}", s.CounterTimeStamp );
   Console::WriteLine( "   CounterType      = {0}", s.CounterType );
   Console::WriteLine( "   RawValue         = {0}", s.RawValue );
   Console::WriteLine( "   SystemFrequency  = {0}", s.SystemFrequency );
   Console::WriteLine( "   TimeStamp        = {0}", s.TimeStamp );
   Console::WriteLine( "   TimeStamp100nSec = {0}", s.TimeStamp100nSec );
   Console::WriteLine( "++++++++++++++++++++++" );
}

bool SetupCategory()
{
   if (  !PerformanceCounterCategory::Exists( "NumberOfItems64SampleCategory" ) )
   {
      CounterCreationDataCollection^ CCDC = gcnew CounterCreationDataCollection;

      // Add the counter.
      CounterCreationData^ NOI64 = gcnew CounterCreationData;
      NOI64->CounterType = PerformanceCounterType::NumberOfItems64;
      NOI64->CounterName = "NumberOfItems64Sample";
      CCDC->Add( NOI64 );

      // Create the category.
      PerformanceCounterCategory::Create( "NumberOfItems64SampleCategory", "Demonstrates usage of the NumberOfItems64 performance counter type.", CCDC );
      return true;
   }
   else
   {
      Console::WriteLine( "Category exists - NumberOfItems64SampleCategory" );
      return false;
   }
}

void CreateCounters( PerformanceCounter^% PC )
{
   // Create the counters.
   PC = gcnew PerformanceCounter( "NumberOfItems64SampleCategory","NumberOfItems64Sample",false );
   PC->RawValue = 0;
}

void CollectSamples( ArrayList^ samplesList, PerformanceCounter^ PC )
{
   Random^ r = gcnew Random( DateTime::Now.Millisecond );

   // Loop for the samples.
   for ( int j = 0; j < 100; j++ )
   {
      int value = r->Next( 1, 10 );
      Console::Write( "{0} = {1}", j, value );
      PC->IncrementBy( value );
      if ( (j % 10) == 9 )
      {
         OutputSample( PC->NextSample() );
         samplesList->Add( PC->NextSample() );
      }
      else
            Console::WriteLine();
      System::Threading::Thread::Sleep( 50 );
   }
}

void CalculateResults( ArrayList^ samplesList )
{
   for ( int i = 0; i < (samplesList->Count - 1); i++ )
   {
      // Output the sample.
      OutputSample(  *safe_cast<CounterSample^>(samplesList[ i ]) );
      OutputSample(  *safe_cast<CounterSample^>(samplesList[ i + 1 ]) );

      // Use .NET to calculate the counter value.
      Console::WriteLine( ".NET computed counter value = {0}", CounterSampleCalculator::ComputeCounterValue(  *safe_cast<CounterSample^>(samplesList[ i ]),  *safe_cast<CounterSample^>(samplesList[ i + 1 ]) ) );

      // Calculate the counter value manually.
      Console::WriteLine( "My computed counter value = {0}", MyComputeCounterValue(  *safe_cast<CounterSample^>(samplesList[ i ]),  *safe_cast<CounterSample^>(samplesList[ i + 1 ]) ) );
   }
}

int main()
{
   ArrayList^ samplesList = gcnew ArrayList;
   PerformanceCounter^ PC;
   SetupCategory();
   CreateCounters( PC );
   CollectSamples( samplesList, PC );
   CalculateResults( samplesList );
}

SampleFraction

No code example is currently available or this language may not be supported.

RateOfCountsPerSecond32

#using <System.dll>

using namespace System;
using namespace System::Collections;
using namespace System::Collections::Specialized;
using namespace System::Diagnostics;

//++++++++//++++++++//++++++++//++++++++//++++++++//++++++++//++++++++//++++++++
//    PERF_COUNTER_COUNTER
//    Description     - This counter type shows the average number of operations completed
//        during each second of the sample interval. Counters of this type
//        measure time in ticks of the system clock. The F variable represents
//        the number of ticks per second. The value of F is factored into the
//        equation so that the result can be displayed in seconds.
//
//    Generic type - Difference
//
//    Formula - (N1 - N0) / ( (D1 - D0) / F), where the numerator (N) represents the number
//        of operations performed during the last sample interval, the denominator
//        (D) represents the number of ticks elapsed during the last sample
//        interval, and F is the frequency of the ticks.
//
//         Average - (Nx - N0) / ((Dx - D0) / F) 
//
//       Example - System\ File Read Operations/sec 
//++++++++//++++++++//++++++++//++++++++//++++++++//++++++++//++++++++//++++++++
float MyComputeCounterValue( CounterSample s0, CounterSample s1 )
{
   float numerator = (float)(s1.RawValue - s0.RawValue);
   float denomenator = (float)(s1.TimeStamp - s0.TimeStamp) / (float)s1.SystemFrequency;
   float counterValue = numerator / denomenator;
   return counterValue;
}


// Output information about the counter sample.
void OutputSample( CounterSample s )
{
   Console::WriteLine( "\r\n+++++++++++" );
   Console::WriteLine( "Sample values - \r\n" );
   Console::WriteLine( "   BaseValue        = {0}", s.BaseValue );
   Console::WriteLine( "   CounterFrequency = {0}", s.CounterFrequency );
   Console::WriteLine( "   CounterTimeStamp = {0}", s.CounterTimeStamp );
   Console::WriteLine( "   CounterType      = {0}", s.CounterType );
   Console::WriteLine( "   RawValue         = {0}", s.RawValue );
   Console::WriteLine( "   SystemFrequency  = {0}", s.SystemFrequency );
   Console::WriteLine( "   TimeStamp        = {0}", s.TimeStamp );
   Console::WriteLine( "   TimeStamp100nSec = {0}", s.TimeStamp100nSec );
   Console::WriteLine( "++++++++++++++++++++++" );
}

bool SetupCategory()
{
   if (  !PerformanceCounterCategory::Exists( "RateOfCountsPerSecond32SampleCategory" ) )
   {
      CounterCreationDataCollection^ CCDC = gcnew CounterCreationDataCollection;

      // Add the counter.
      CounterCreationData^ rateOfCounts32 = gcnew CounterCreationData;
      rateOfCounts32->CounterType = PerformanceCounterType::RateOfCountsPerSecond32;
      rateOfCounts32->CounterName = "RateOfCountsPerSecond32Sample";
      CCDC->Add( rateOfCounts32 );

      // Create the category.
      PerformanceCounterCategory::Create( "RateOfCountsPerSecond32SampleCategory", "Demonstrates usage of the RateOfCountsPerSecond32 performance counter type.", CCDC );
      return true;
   }
   else
   {
      Console::WriteLine( "Category exists - RateOfCountsPerSecond32SampleCategory" );
      return false;
   }
}

void CreateCounters( PerformanceCounter^% PC )
{
   // Create the counter.
   PC = gcnew PerformanceCounter( "RateOfCountsPerSecond32SampleCategory","RateOfCountsPerSecond32Sample",false );
   PC->RawValue = 0;
}

void CollectSamples( ArrayList^ samplesList, PerformanceCounter^ PC )
{
   Random^ r = gcnew Random( DateTime::Now.Millisecond );

   // Initialize the performance counter.
   PC->NextSample();

   // Loop for the samples.
   for ( int j = 0; j < 100; j++ )
   {
      int value = r->Next( 1, 10 );
      PC->IncrementBy( value );
      Console::Write( "{0} = {1}", j, value );
      if ( (j % 10) == 9 )
      {
         Console::WriteLine( ";       NextValue() = {0}", PC->NextValue() );
         OutputSample( PC->NextSample() );
         samplesList->Add( PC->NextSample() );
      }
      else
            Console::WriteLine();
      System::Threading::Thread::Sleep( 50 );
   }
}

void CalculateResults( ArrayList^ samplesList )
{
   for ( int i = 0; i < (samplesList->Count - 1); i++ )
   {
      // Output the sample.
      OutputSample(  *safe_cast<CounterSample^>(samplesList[ i ]) );
      OutputSample(  *safe_cast<CounterSample^>(samplesList[ i + 1 ]) );

      // Use .NET to calculate the counter value.
      Console::WriteLine( ".NET computed counter value = {0}", CounterSampleCalculator::ComputeCounterValue(  *safe_cast<CounterSample^>(samplesList[ i ]),  *safe_cast<CounterSample^>(samplesList[ i + 1 ]) ) );

      // Calculate the counter value manually.
      Console::WriteLine( "My computed counter value = {0}", MyComputeCounterValue(  *safe_cast<CounterSample^>(samplesList[ i ]),  *safe_cast<CounterSample^>(samplesList[ i + 1 ]) ) );
   }
}

int main()
{
   ArrayList^ samplesList = gcnew ArrayList;
   PerformanceCounter^ PC;
   SetupCategory();
   CreateCounters( PC );
   CollectSamples( samplesList, PC );
   CalculateResults( samplesList );
}

RateOfCountsPerSecond64

#using <System.dll>

using namespace System;
using namespace System::Collections;
using namespace System::Collections::Specialized;
using namespace System::Diagnostics;

//++++++++//++++++++//++++++++//++++++++//++++++++//++++++++//++++++++//++++++++
//    PERF_COUNTER_COUNTER
//    Description     - This counter type shows the average number of operations completed
//        during each second of the sample interval. Counters of this type
//        measure time in ticks of the system clock. The F variable represents
//        the number of ticks per second. The value of F is factored into the
//        equation so that the result can be displayed in seconds.
//
//    Generic type - Difference
//
//    Formula - (N1 - N0) / ( (D1 - D0) / F), where the numerator (N) represents the number
//        of operations performed during the last sample interval, the denominator
//        (D) represents the number of ticks elapsed during the last sample
//        interval, and F is the frequency of the ticks.
//
//    Average - (Nx - N0) / ((Dx - D0) / F) 
//
//  Example - System\ File Read Operations/sec 
//++++++++//++++++++//++++++++//++++++++//++++++++//++++++++//++++++++//++++++++
float MyComputeCounterValue( CounterSample s0, CounterSample s1 )
{
   float numerator = (float)(s1.RawValue - s0.RawValue);
   float denomenator = (float)(s1.TimeStamp - s0.TimeStamp) / (float)s1.SystemFrequency;
   float counterValue = numerator / denomenator;
   return counterValue;
}

void OutputSample( CounterSample s )
{
   Console::WriteLine( "\r\n+++++++++++" );
   Console::WriteLine( "Sample values - \r\n" );
   Console::WriteLine( "   BaseValue        = {0}", s.BaseValue );
   Console::WriteLine( "   CounterFrequency = {0}", s.CounterFrequency );
   Console::WriteLine( "   CounterTimeStamp = {0}", s.CounterTimeStamp );
   Console::WriteLine( "   CounterType      = {0}", s.CounterType );
   Console::WriteLine( "   RawValue         = {0}", s.RawValue );
   Console::WriteLine( "   SystemFrequency  = {0}", s.SystemFrequency );
   Console::WriteLine( "   TimeStamp        = {0}", s.TimeStamp );
   Console::WriteLine( "   TimeStamp100nSec = {0}", s.TimeStamp100nSec );
   Console::WriteLine( "++++++++++++++++++++++" );
}

bool SetupCategory()
{
   if (  !PerformanceCounterCategory::Exists( "RateOfCountsPerSecond64SampleCategory" ) )
   {
      CounterCreationDataCollection^ CCDC = gcnew CounterCreationDataCollection;

      // Add the counter.
      CounterCreationData^ rateOfCounts64 = gcnew CounterCreationData;
      rateOfCounts64->CounterType = PerformanceCounterType::RateOfCountsPerSecond64;
      rateOfCounts64->CounterName = "RateOfCountsPerSecond64Sample";
      CCDC->Add( rateOfCounts64 );

      // Create the category.
      PerformanceCounterCategory::Create( "RateOfCountsPerSecond64SampleCategory", "Demonstrates usage of the RateOfCountsPerSecond64 performance counter type.", CCDC );
      return true;
   }
   else
   {
      Console::WriteLine( "Category exists - RateOfCountsPerSecond64SampleCategory" );
      return false;
   }
}

void CreateCounters( PerformanceCounter^% PC )
{
   // Create the counter.
   PC = gcnew PerformanceCounter( "RateOfCountsPerSecond64SampleCategory","RateOfCountsPerSecond64Sample",false );
   PC->RawValue = 0;
}

void CollectSamples( ArrayList^ samplesList, PerformanceCounter^ PC )
{
   Random^ r = gcnew Random( DateTime::Now.Millisecond );

   // Initialize the performance counter.
   PC->NextSample();

   // Loop for the samples.
   for ( int j = 0; j < 100; j++ )
   {
      int value = r->Next( 1, 10 );
      PC->IncrementBy( value );
      Console::Write( "{0} = {1}", j, value );
      if ( (j % 10) == 9 )
      {
         Console::WriteLine( ";       NextValue() = {0}", PC->NextValue() );
         OutputSample( PC->NextSample() );
         samplesList->Add( PC->NextSample() );
      }
      else
            Console::WriteLine();
      System::Threading::Thread::Sleep( 50 );
   }
}

void CalculateResults( ArrayList^ samplesList )
{
   for ( int i = 0; i < (samplesList->Count - 1); i++ )
   {
      // Output the sample.
      OutputSample(  *safe_cast<CounterSample^>(samplesList[ i ]) );
      OutputSample(  *safe_cast<CounterSample^>(samplesList[ i + 1 ]) );

      // Use .NET to calculate the counter value.
      Console::WriteLine( ".NET computed counter value = {0}", CounterSampleCalculator::ComputeCounterValue(  *safe_cast<CounterSample^>(samplesList[ i ]),  *safe_cast<CounterSample^>(samplesList[ i + 1 ]) ) );

      // Calculate the counter value manually.
      Console::WriteLine( "My computed counter value = {0}", MyComputeCounterValue(  *safe_cast<CounterSample^>(samplesList[ i ]),  *safe_cast<CounterSample^>(samplesList[ i + 1 ]) ) );
   }
}

int main()
{
   ArrayList^ samplesList = gcnew ArrayList;
   PerformanceCounter^ PC;
   SetupCategory();
   CreateCounters( PC );
   CollectSamples( samplesList, PC );
   CalculateResults( samplesList );
}

RawFraction

#using <System.dll>

using namespace System;
using namespace System::Collections;
using namespace System::Collections::Specialized;
using namespace System::Diagnostics;

//++++++++//++++++++//++++++++//++++++++//++++++++//++++++++//++++++++//++++++++
// Formula from MSDN -
//      Description - This counter type shows the ratio of a subset to its set as a percentage.
//            For example, it compares the number of bytes in use on a disk to the
//            total number of bytes on the disk. Counters of this type display the 
//            current percentage only, not an average over time.
//
// Generic type - Instantaneous, Percentage 
//        Formula - (N0 / D0), where D represents a measured attribute and N represents one
//            component of that attribute.
//
//        Average - SUM (N / D) /x 
//        Example - Paging File\% Usage Peak
//++++++++//++++++++//++++++++//++++++++//++++++++//++++++++//++++++++//++++++++
float MyComputeCounterValue( CounterSample rfSample )
{
   float numerator = (float)rfSample.RawValue;
   float denomenator = (float)rfSample.BaseValue;
   float counterValue = (numerator / denomenator) * 100;
   return counterValue;
}


// Output information about the counter sample.
void OutputSample( CounterSample s )
{
   Console::WriteLine( "+++++++++++" );
   Console::WriteLine( "Sample values - \r\n" );
   Console::WriteLine( "   BaseValue        = {0}", s.BaseValue );
   Console::WriteLine( "   CounterFrequency = {0}", s.CounterFrequency );
   Console::WriteLine( "   CounterTimeStamp = {0}", s.CounterTimeStamp );
   Console::WriteLine( "   CounterType      = {0}", s.CounterType );
   Console::WriteLine( "   RawValue         = {0}", s.RawValue );
   Console::WriteLine( "   SystemFrequency  = {0}", s.SystemFrequency );
   Console::WriteLine( "   TimeStamp        = {0}", s.TimeStamp );
   Console::WriteLine( "   TimeStamp100nSec = {0}", s.TimeStamp100nSec );
   Console::WriteLine( "++++++++++++++++++++++" );
}

bool SetupCategory()
{
   if (  !PerformanceCounterCategory::Exists( "RawFractionSampleCategory" ) )
   {
      CounterCreationDataCollection^ CCDC = gcnew CounterCreationDataCollection;

      // Add the counter.
      CounterCreationData^ rf = gcnew CounterCreationData;
      rf->CounterType = PerformanceCounterType::RawFraction;
      rf->CounterName = "RawFractionSample";
      CCDC->Add( rf );

      // Add the base counter.
      CounterCreationData^ rfBase = gcnew CounterCreationData;
      rfBase->CounterType = PerformanceCounterType::RawBase;
      rfBase->CounterName = "RawFractionSampleBase";
      CCDC->Add( rfBase );

      // Create the category.
      PerformanceCounterCategory::Create( "RawFractionSampleCategory", "Demonstrates usage of the RawFraction performance counter type.", CCDC );
      return true;
   }
   else
   {
      Console::WriteLine( "Category exists - RawFractionSampleCategory" );
      return false;
   }
}

void CreateCounters( PerformanceCounter^% PC, PerformanceCounter^% BPC )
{

   // Create the counters.
   PC = gcnew PerformanceCounter( "RawFractionSampleCategory","RawFractionSample",false );
   BPC = gcnew PerformanceCounter( "RawFractionSampleCategory","RawFractionSampleBase",false );
   PC->RawValue = 0;
   BPC->RawValue = 0;
}

void CollectSamples( ArrayList^ samplesList, PerformanceCounter^ PC, PerformanceCounter^ BPC )
{
   Random^ r = gcnew Random( DateTime::Now.Millisecond );

   // Initialize the performance counter.
   PC->NextSample();

   // Loop for the samples.
   for ( int j = 0; j < 100; j++ )
   {
      int value = r->Next( 1, 10 );
      Console::Write( "{0} = {1}", j, value );

      // Increment the base every time, because the counter measures the number 
      // of high hits (raw fraction value) against all the hits (base value).
      BPC->Increment();

      // Get the % of samples that are 9 or 10 out of all the samples taken.
      if ( value >= 9 )
            PC->Increment();

      // Copy out the next value every ten times around the loop.
      if ( (j % 10) == 9 )
      {
         Console::WriteLine( ";       NextValue() = {0}", PC->NextValue() );
         OutputSample( PC->NextSample() );
         samplesList->Add( PC->NextSample() );
      }
      else
            Console::WriteLine();
      System::Threading::Thread::Sleep( 50 );

   }
}

void CalculateResults( ArrayList^ samplesList )
{
   for ( int i = 0; i < samplesList->Count; i++ )
   {

      // Output the sample.
      OutputSample(  *safe_cast<CounterSample^>(samplesList[ i ]) );

      // Use .NET to calculate the counter value.
      Console::WriteLine( ".NET computed counter value = {0}", CounterSampleCalculator::ComputeCounterValue(  *safe_cast<CounterSample^>(samplesList[ i ]) ) );

      // Calculate the counter value manually.
      Console::WriteLine( "My computed counter value = {0}", MyComputeCounterValue(  *safe_cast<CounterSample^>(samplesList[ i ]) ) );

   }
}

int main()
{
   ArrayList^ samplesList = gcnew ArrayList;
   PerformanceCounter^ PC;
   PerformanceCounter^ BPC;
   SetupCategory();
   CreateCounters( PC, BPC );
   CollectSamples( samplesList, PC, BPC );
   CalculateResults( samplesList );
}

.NET Framework
Available since 1.1
Return to top
Show:
© 2016 Microsoft