Working with Performance Counters

   


Instrument and debug a Windows service, a serviced component, a .NET Remoting object, and an XML Web service.

Performance counters are Windows' way of collecting performance data from running processes. Microsoft Windows itself provides several hundred performance counterseach of which monitors a particular system parameter. In addition, the various .NET server products, such as SQL Server and Exchange Server, and applications, such as the .NET Framework, also publish their own custom performance counters. You might be familiar with performance counters from the Windows System Monitor utility, shown in Figure 9.37.

Figure 9.37. The System Monitor utility lets you view changing values of performance counters.

Windows organizes performance counters in categories. Each category defines a specific set of performance counters. For example, there are categories such as Memory, Processor, and PhysicalDisk. The Memory category has various counters, such as Available Bytes, Cache Bytes, and Committed Bytes.

Some categories are further divided into instances. For example, the Process category is divided into several instanceseach representing a running process on the computer. A new instance is added to the category whenever a new process is started and removed when a process is killed . Each instance can have performance counters (such as I/O Read Bytes/sec) that specify the activity of that particular process. Usually, all the instances in a category have the same list of performance counters. Of course, each of the performance counters has unique performance data associated with it.

You can view existing performance counters in a navigable format in Visual Studio .NET Server Explorer, as shown in Figure 9.38.

Figure 9.38. Visual Studio .NET Server Explorer displays all the existing performance counters on a machine.

Note from Figure 9.38, .NET CLR Data, .NET CLR Memory, and so on nodes represent performance counter categories. The # Total committed Bytes, # Total reserved Bytes, and so on nodes are the performance counters in the .NET CLR Memory category. _Global_, aspnet_wp, devenv, and so on are the instances of the # Total committed Bytes performance counter.

The PerformanceCounter class allows you to read performance examples for processes that are running on a machine. By using this class, an application can even publish its own performance counter to inform the world about its performance level.

Table 9.8 lists some important members of the PerformanceCounter class.

Table 9.8. Important Members of the PerformanceCounter Class

Member

Type

Description

CategoryName

Property

Specifies the performance counter category name .

Close

Method

Closes the performance counter and frees all the resources.

CounterHelp

Property

Describes the performance counter.

CounterName

Property

Specifies the name of the performance counter.

CounterType

Property

Specifies the type of the performance counter.

Decrement

Method

Decrements the value of the performance counter by one.

Increment

Method

Increments the value of the performance counter by one.

IncrementBy

Method

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

InstanceName

Property

Specifies the instance name.

MachineName

Property

Specifies the computer name.

NextSample

Method

Gets a sample for the performance counter and returns the raw, or uncalculated, value for it.

NextValue

Method

Gets an example for the performance counter and returns the calculated value for it.

RawValue

Property

Gets an example for the performance counter and returns its raw, or uncalculated, value.

ReadOnly

Property

Indicates whether the performance counter is in read-only mode.

RemoveInstance

Method

Deletes an instance from the PerformanceCounter object.

The .NET Framework allows applications to create their own custom performance counters and publish their performance data. This performance data can then be monitored via the Performance Monitoring tool ( perfmon.exe ).

NOTE

Performance Counters on Remote Machines You can read existing performance counters on remote computers, but you can create custom performance counters only on the local machine.


Visual Studio .NET makes it easy for you to create new performance categories and counters: It provides the Performance Counter Builder Wizard, which is available via Server Explorer. In Step by Step 9.12, you create a Windows application that publishes its performance and allows you to manually increase or decrease its reported performance values.

STEP BY STEP

9.12 Publishing Performance Data

  1. Add a Visual Basic .NET Windows application project to the solution. Name the project StepByStep9-12 .

  2. Rename the Form1.vb form StepByStep9-12.vb in the project. Switch to the code view of the form and modify all references to Form1 so that they refer to StepByStep9_12 instead.

  3. Place three Label controls (one named lblCurrentLevel ), one TextBox control ( txtLevel ), and three Button controls ( btnInc , btnDec , and btnSet ) on the form. Arrange the controls as shown in Figure 9.39.

    Figure 9.39. You can publish custom performance data for an application.

  4. Open Server Explorer and from the Servers node select the server in which you want to create a performance counter. Right-click the Performance Counters node and select the Create New Category option. The Performance Counter Builder dialog box appears. Enter the values in the dialog box, as shown in Figure 9.40, and then click OK.

    Figure 9.40. The Performance Counter Builder dialog box allows you to create a new performance counter category and specify one or more counters to be placed within it.

  5. Select the StepByStep9-12 performance counter from the Performance Counters node in Server Explorer. Drag the performance counter that appears under the StepByStep9-12 node to the form. Set the Name of the counter to pc9_12 and the ReadOnly property to False .

  6. Switch to the code view. Add the following Imports directive:

     Imports System.Diagnostics 
  7. Attach Click event handlers to the Button controls and add the following code in the event handlers:

     Private Sub btnInc_Click(_  ByVal sender As System.Object, _  ByVal e As System.EventArgs) Handles btnInc.Click     pc9_12.Increment()     lblCurrentLevel.Text = _      pc9_12.NextValue().ToString() End Sub Private Sub btnDec_Click(_  ByVal sender As System.Object, _  ByVal e As System.EventArgs) Handles btnDec.Click     pc9_12.Decrement()     lblCurrentLevel.Text = _      pc9_12.NextValue().ToString() End Sub Private Sub btnSet_Click(_  ByVal sender As System.Object, _  ByVal e As System.EventArgs) Handles btnSet.Click     ' Set the performance counter value     pc9_12.RawValue = Int32.Parse(txtLevel.Text)     lblCurrentLevel.Text = _      pc9_12.NextValue().ToString() End Sub 

    NOTE

    Creating a Custom Performance Counter When you create a new performance counter, you must specify a completely new category for your local computer. It is not possible to add a new performance counter to an existing category; however, you can add several performance counters to a new custom category.

    It is also possible to create performance counter categories programmatically by using the PerformanceCounterCategory.Create() method.

  8. Attach Load and Closed event handlers to the form and add the following code in the event handlers:

     Private Sub StepByStep9_12_Load(_  ByVal sender As System.Object, _   ByVal e As System.EventArgs) Handles MyBase.Load     lblCurrentLevel.Text = _      pc9_12.NextValue().ToString() End Sub Private Sub StepByStep9_12_Closed(_  ByVal sender As Object, _  ByVal e As System.EventArgs) Handles MyBase.Closed     pc9_12.RawValue = 0 End Sub 
  9. Set the form as the startup object for the project and set the project as the startup project for the solution.

  10. Run the project. Enter a number in the text box and click the Set button. The label immediately reflects this as the current value of the performance counter. Click the increment and decrement buttons and notice the value changing in the label.

REVIEW BREAK

  • The System event log provides a central repository for various issues that an application might encounter while it executes. Using an event log to record such messages not only makes the job of system administrator easy, but it also allows other applications to take appropriate action when an entry is written to a log.

  • Multiple event sources can write into a single event log. However, each event source can be used to write into only one event log.

  • Performance counters are organized in categories, and each category defines a specific set of performance counters.

  • The process of reading a performance counter value is called sampling the performance counter. The process of updating a performance counter value is called publishing a performance counter.


   
Top


MCAD. MCSD Training Guide (Exam 70-310. Developing XML Web Services and Server Components with Visual Basic. NET and the. NET Framework)
MCAD/MCSD Training Guide (70-310): Developing XML Web Services and Server Components with Visual Basic(R) .NET and the .NET Framework
ISBN: 0789728206
EAN: 2147483647
Year: 2002
Pages: 166

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net