PerformanceCounter Class in VB.NET

In this article I will explain you about the PerformanceCounter Class in VB.NET.
  • 1792
 

We always measure the performance of our code (applications, services, drivers, etc.) in the field in real time or in a testing environment. Then we can diagnose problems and fix them in the future. Performance counters enable us to publish, capture, and analyze the performance data of running code. A performance graph is a two-dimensional plot with one axis indicating time elapsed and the other reporting relevant relative or actual performance statistics.

The common language runtime (CLR) provides a PerformanceCounter class, with which we can read and write performance data on computers running Windows NT, 2000, or XP. We need to call counters in which the performance data is placed. The names of the counters are stored in the Windows registry along with the counters' various settings. Every performance counter has a unique name and location. Another attribute of a counter is its category (the performance object for which the counter measures data). For example, the Processor performance category has the % Processor Time performance counter object (and others), which has the _Total performance counter instance (and others). Please use Performance Monitor MMC if you have Windows NT, 2000, or XP installed (it's found on the Administrative Tools menu) to discover other PerformanceCounter categories, objects, and instances. PerformanceCounter class members are defined in Table 21.8.

Table 21.8: PerformanceCounter Class Members

table21.8.gif

The example given below shows how we might use performance counters. In this example we measure the total processing time with the Processor performance counter.

Example of Using PerformanceCounter

    Imports System.Threading
    Imports System.Diagnostics

        Public Class TestPerfCounter
            Shared myCounter As PerformanceCounter

            Public Shared Sub Main()
                If Not PerformanceCounterCategory.Exists("Processor") Then
                    Console.WriteLine("Object Processor does not exist!")
                    Return
                End
If

                If
Not PerformanceCounterCategory.CounterExists("% Processor Time", "Processor") Then
                    Console.WriteLine("Counter % Processor Time does not exist!")
                    Return
                End If

                myCounter = New PerformanceCounter("Processor", "% Processor Time", "_Total")
      
                 ' The raw value of a counter can be set in your applications as shown below
                ' if the object is not read-only
                Try
                    myCounter.RawValue = 19
                Catch
                    Console.WriteLine("Processor, % Processor Time, _Total instance is READONLY!")
                End Try
                    Console.WriteLine("Press 'CTRL+C' to quit...")
                While True
                    Console.WriteLine("@")

                    Try
                        Console.WriteLine("Current value of Processor, %Processor Time, _Total= " & myCounter.NextValue().ToString())
                    Catch
                        Console.WriteLine("_Total instance does not exist!")
                        Return
                    End
Try

                    Thread.Sleep(1000)
                    Console.WriteLine("Press 'CTRL+C' to quit...")
                    End While
            End
Sub
        End
Class

Output

pc1.gif
 

Conclusion

Hope this article would have helped you in understanding the PerformanceCounter Class in VB.NET.

Categories

More Articles

© 2013 dotNetheaven. All rights reserved.