Home   |   QuickStart Welcome   |   ASP.NET   |   Web Services   |   How Do I...?   
  |   I want my samples in...      

How Do I...? Common Tasks QuickStart Tutorial

How Do I...Count rate of change?

Windows performance counters enable your applications and components to publish, capture, and analyze the performance data that applications, services, and drivers provide. You can use this information to determine system bottlenecks and fine-tune system and application performance. For example, you can use a performance counter to track the number of orders processes per second or the number of users currently connected to the system. Using the common language runtime's PerformanceCounter component, you can easily create your own custom counters and publish performance data relevant to your application, such as those mentioned above.

This sample illustrates how to publish the number of orders processed per second using a custom performance counter. It's a small console application you can run from the command prompt. You have to run the app first to install the counter as follows:
> PCDemo.exe /inst
Then you have to run the app again without any arguments to see it work as follows:
> PCDemo.exe

Now, wait for the application to display "Started" and run the PerfMon.exe. In PerfMon, choose the Add toolbar button. A dialog will open. Select the ACounterDemo performance object, CountPerSecond counter, and _Total instance. Choose Add, close the dialog, and notice that you can use the PCDemo sample to change the published value by pressing + or -. When the application starts, it simulates the processing of two new orders per second. The + and - keys can be used to double the number or divide it in two.

To delete the counter you could run the app with the delete switch as follows:
> PCDemo.exe /del

In its simplest form, writing to a custom performance counter that counts number of items per second involves:
  1. Creating a counter of the RateOfCountsPerSecond32 type:

    if(!PerformanceCounterCategory.Exists(objectName)) {
        CounterCreationData ccd = new CounterCreationData();
        ccd.CounterName = counterName;
        ccd.CounterType = PerformanceCounterType.RateOfCountsPerSecond32;
        CounterCreationDataCollection ccds = new CounterCreationDataCollection();
        PerformanceCounterCategory.Create(objectName,"Sample Object",ccds);

  2. Creating a new instance of a PerformanceCounter component and pointing it to an appropriate performance counter:

    String objectName = ... ;
    String counterName = ... ;
    String instanceName = ... ;
    PerformanceCounter counter;
    counter = new PerformanceCounter(objectName, counterName, instanceName);

  3. Setting the RawValue property of the counter:



VB PCDemo.exe
View Source
[This sample can be found at C:\DevFusion.Data\legacy\quickstart.developerfusion.co.uk\QuickStart\howto\samples\Services\PerformanceCounters\PCDemo\]

Microsoft .NET Framework SDK QuickStart Tutorials Version 2.0
Copyright � 2004 Microsoft Corporation. All rights reserved.