PerformanceCounter in C#


This article has been excerpted from book "The Complete Visual C# Programmer's Guide" from the Authors of C# Corner.

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. 

table21.8.gif

Table 21.8: PerformanceCounter Class Members 

Listing 21.17 illustrates how we might use performance counters. In this example we measure the total processing time with the Processor performance counter. 

Listing 21.17: Using PerformanceCounter (perf1.cs) 

using System;
using System.Threading;
using System.Diagnostics;

public class TestPerfCounter
{
    static PerformanceCounter myCounter;

    public static void Main()
    {
        if (!PerformanceCounterCategory.Exists("Processor"))
        {
            Console.WriteLine("Object Processor does not exist!");
            return;
        }

        if (!PerformanceCounterCategory.CounterExists(@"% Processor Time", "Processor"))
        {
            Console.WriteLine(@"Counter % Processor Time does not exist!");
            return;
        }

        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!");
        }

        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;
            }

            Thread.Sleep(1000);
            Console.WriteLine(@"Press 'CTRL+C' to quit...");
        }
    }
}

Conclusion

Hope this article would have helped you in understanding the PerformanceCounter Class in C#. See other articles on the website on .NET and C#.

visual C-sharp.jpg
The Complete Visual C# Programmer's Guide covers most of the major components that make up C# and the .net environment. The book is geared toward the intermediate programmer, but contains enough material to satisfy the advanced developer.

erver'>

Similar Articles