Working with Performance Counters

Working with Performance Counters

Performance counters are Windows's way of collecting performance data from the running processes. Performance counters are organized into categories, with each category defining a specific set of performance counters. The following two categories of performance counters are specifically dedicated for ASP.NET applications:

  • ASP.NET— The ASP.NET category provides performance counters to monitor the performance for all ASP.NET applications on a Web server, such as Request Execution Time, Request Wait Time, Requests Queued, Requests Rejected, and so on.

  • ASP.NET Applications— The ASP.NET Applications category has various counters that monitor the performance of a single instance of an ASP.NET application, such as Requests/Sec, Sessions Total, Transactions/Sec, and so on.

The PerformanceCounter class enables you to read performance samples for processes running on a machine. By using this class, an application can even publish its own performance counters. Figure lists the important members of the PerformanceCounter class.

Figure Important Members of the PerformanceCounter Class






Specifies the performance counter category name.



Closes the performance counter and frees all the resources.



Specifies the performance counter name.



Specifies the performance counter type.



Decrements the performance counter value by one.



Increments the performance counter value by one.



Increments or decrements the performance counter value by a specified amount.



Specifies the instance name.



Specifies the computer name.



Returns an object of type CounterSample that has properties such as RawValue, BaseValue, TimeStamp, and SystemFrequency. These properties provide detailed information on the performance data.



Retrieves the current calculated value (a float type) for a performance counter.



Retrieves the raw, or uncalculated, value for a performance counter.



Indicates whether the PerformanceCounter object is in read-only mode.

The process of reading a performance counter value is referred to as sampling the performance counter. A good way to analyze a performance counter is to graph sample values over time. Windows provides the Performance Monitoring tool (perfmon.exe) for this very purpose.

Although tools such as perfmon.exe make monitoring the performance of an application simple, it is sometimes also useful to read the values programmatically. This can be useful, for example, when you want a program that monitors the performance of another program and takes actions depending on the performance data from that program. As an example, you might want to allow the download of some large files only when the number of incoming requests on a Web site are below a particular level.

The following steps demonstrate how to use an instance of a PerformanceCounter component to monitor the total number of requests since the ASP.NET worker process was started:

  1. Add a new Visual C# Web Application project to the existing solution and name it Example15_3.

  2. Place a Label control, a ListBox control (lbPerformance), and a Button control (btnSample) onto the Web form.

  3. Open Server Explorer and select the Web server from the Servers node. Select the __Total__ instance of the Requests Total performance counter by navigating to Performance Counters, ASP.NET Applications, Requests Total, __Total__ from the server node. Drag the __Total__ instance of the performance counter to the Web form; then name the counter pcRequests. Change the MachineName property of the counter to a single dot (.) to refer to the Web server on which the code is running.

  4. Switch to Code view and add the following using directive:

    using System.Diagnostics;
  5. Attach an event handler to the Click event of the Button control and add the following code in the event handler:

    private void btnSample_Click(object sender,
        System.EventArgs e)
        // Get the next performance data
        CounterSample csSample = pcRequests.NextSample();
        // Add the data to the list box
        lbPerformance.Items.Add(pcRequests.CounterName + ": " +
            csSample.RawValue.ToString() + " at " +
  6. Set the project as the startup project for the solution and run the project. Each time you click the button, a new performance value is added as a new row to the ListBox control.

     Python   SQL   Java   php   Perl 
     game development   web development   internet   *nix   graphics   hardware 
     telecommunications   C++ 
     Flash   Active Directory   Windows