May 27, 2008

Performance counter

Performance counter is used to monitor the running status of an application. The application itself just output the performance data. The data itself don't need to be calcuated by the application, the data is used by external application, like performance MMC. Here discussed how to output performance data only. To output performance data takes several steps.

  1. First setup the register performance counter, and its belong category. For example:
                CounterCreationDataCollection counterCreationDataCollection = new CounterCreationDataCollection();
    
                // Add the counter.
                CounterCreationData averageCount64 = new CounterCreationData();
                averageCount64.CounterType = PerformanceCounterType.AverageCount64;
                averageCount64.CounterName = "AverageCounter64Sample";
                counterCreationDataCollection.Add(averageCount64);
    
                // Add the base counter.
                CounterCreationData averageCount64Base = new CounterCreationData();
                averageCount64Base.CounterType = PerformanceCounterType.AverageBase;
                averageCount64Base.CounterName = "AverageCounter64SampleBase";
                counterCreationDataCollection.Add(averageCount64Base);
    
                // Create the category.
                PerformanceCounterCategory.Create("AverageCounter64SampleCategory",
                "Demonstrates usage of the AverageCounter64 performance counter type.",
                PerformanceCounterCategoryType.SingleInstance, counterCreationDataCollection);
    
                

    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. Please note the base counter is not visible to the performance MMC.

    Base counter type

    Performance counter types

    AverageBase

    AverageTimer32

    AverageCount64

    CounterMultiBase

    CounterMultiTimer

    CounterMultiTimerInverse

    CounterMultiTimer100Ns

    CounterMultiTimer100NsInverse

    RawBase

    RawFraction

    SampleBase

    SampleFraction

  2. Then you need to get a reference of the performance counter
                CounterCreationDataCollection counterCreationDataCollection = new CounterCreationDataCollection();
    
                // Add the counter.
                CounterCreationData averageCount64 = new CounterCreationData();
                averageCount64.CounterType = PerformanceCounterType.AverageCount64;
                averageCount64.CounterName = "AverageCounter64Sample";
                counterCreationDataCollection.Add(averageCount64);
    
                // Add the base counter.
                CounterCreationData averageCount64Base = new CounterCreationData();
                averageCount64Base.CounterType = PerformanceCounterType.AverageBase;
                averageCount64Base.CounterName = "AverageCounter64SampleBase";
                counterCreationDataCollection.Add(averageCount64Base);
    
                // Create the category.
                PerformanceCounterCategory.Create("AverageCounter64SampleCategory",
                "Demonstrates usage of the AverageCounter64 performance counter type.",
                PerformanceCounterCategoryType.SingleInstance, counterCreationDataCollection);
    
                
  3. The last step is output the counter.
    pc.IncrementBy(value); basedPc.Increment();