Page view counter

These tutorials demonstrate selected features in ASP.NET version 2.0, but they are compatible with later versions of ASP.NET as well. For the current documentation, see the ASP.NET portal on the MSDN Web site.

 

 

   Welcome   |   ASP.NET   |   Web Services   |   Class Browser   
  |   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 Not PerformanceCounterCategory.Exists(objectName) Then
        Dim ccd As CounterCreationData = new CounterCreationData()
        ccd.CounterName = counterName
        ccd.CounterType = PerformanceCounterType.RateOfCountsPerSecond32
        Dim ccds As New CounterCreationDataCollection
        ccds.Add(ccd)
    
        PerformanceCounterCategory.Create(objectName,"Sample Object",ccds)
    End If
    
    VB


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

    		
    Dim objectName As String = ...
    Dim counterName As String = ...
    Dim instanceName As String = ...
    
    Dim counter As PerformanceCounter
    counter = New PerformanceCounter(objectName, counterName, instanceName)
    
    VB


  3. Setting the RawValue property of the counter:

    		
    counter.IncrementBy(1)
    
    VB

Example

VB PCDemo.exe
View Source



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