Implementation of performance counter classes

The code sample shows the implementation of performance counter classes, in both Visual Basic and C#. In this example, you create a subroutine named PerformanceCounterTest that checks for the existence of the SalesDistribution performance counter category and you delete this category, if it exists. You then create an instance of CounterCreationData named RequestsPerSecond. You create counterCollection as a CounterCreationDataCollection that contains the RequestsPerSecond data. You then create the SalesDistribution performance counter category by using counterCollection. You also create a performance counter that is based on the RequestsPerSecond data and belongs to the SalesDistribution performance counter category. Finally, you create a loop that increments this counter.

Visual Basic

Imports System.Diagnostics Public Class CounterClass

Shared Sub Main(ByVal args() As String)

Dim sc2 As CounterClass = New CounterClass sc2.PerformanceCounterTest() Console.Write("Press ENTER to exit...") Console .ReadLine () End Sub

Public Sub PerformanceCounterTest()

' Delete the Performance Counter Category, if already exists

If PerformanceCounterCategory.Exists("SalesDistribution") Then PerformanceCounterCategory.Delete("SalesDi stribution")

End If

' Prepare for creation of the Performance Counter Dim counterCollection As CounterCreationDataCollection = _ New CounterCreationDataCollection

'Create the CounterCreationData

Dim counterData As CounterCreationData = New CounterCreationData counterData.CounterName = "RequestsPerSec"

counterData.CounterType = PerformanceCounterType.NumberOfItems32 counterData.CounterHelp = "Requests Received per Second" counterCollection.Add(counte rData)

' Create the performance counter category PerformanceCounterCategory.Create("SalesDistri bution", _ "Automated Sales Distribution System", _

PerformanceCounterCategoryType.SingleInstance, counterCollection)

' Retrieve the performance counter to make updates Dim performanceCount As PerformanceCounter = _

New PerformanceCounter("SalesDistribution", "RequestsPerSec",


Console.WriteLine("RequestsPerSec = {0}", performanceCount.RawValue)

' Increment the performance counter performanceCount.Increment() System.Threading.Thread.Sleep(250) x = (x + 1) End While

' Close the performance counter performanceCount.Cl ose() End Sub End Class

using System.Diagnostics;

public class CounterClass {

static void Main(string[] args) {

CounterClass sc2 = new CounterC1ass(); sc2.PerformanceCounterTest(); Conso1e.Write("\n\nPress ENTER to exit..

public void PerformanceCounterTest() {


// Delete the Performance Counter Category, if already exists if (PerformanceCounterCategory.Exists("Sa1esDistribution")) PerformanceCounterCategory.De1ete("Sa1esDi stribution");

// Prepare for creation of the Performance Counter CounterCreationDataCollection counterCollection = new CounterCreationDataCo11ection();

//Create the CounterCreationData

CounterCreationData counterData = new CounterCreationData(); counterData.CounterName = "RequestsPerSec";

counterData.CounterType = PerformanceCounterType.NumberOfItems32; counterData.CounterHelp = "Requests Received per Second"; counterCol1ection.Add(counte rData);

// Create the performance counter category PerformanceCounterCategory.Create("Sa1esDistri bution", "Automated Sales Distribution System",

PerformanceCounterCategoryType.Singlelnstance, counterCollection);

// Retrieve the performance counter for updation PerformanceCounter performanceCount =

new PerformanceCounter("Sa1esDistribution", "RequestsPerSec", int x = 1;

Conso1e.WriteLine("RequestsPerSec = {0}' performanceCount.RawValue);

// Increment the performance counter performanceCount.Increment(); System.Threading.Thread.S1eep(250); x = (x + 1);

// Close the performance counter performanceCount.C1ose();


Was this article helpful?

0 0

Post a comment