Monitoring Performance

Microsoft Windows includes several hundred performance counters, each monitoring a particular service parameter. Other applications such as Exchange and SQL Server also provide a wide variety of performance counters that may be used to obtain current operational parameters. Microsoft Windows organizes performance counters into categories, such as Memory, Processor, or PhysicalDisk. Categories may also be further divided into instances. Each instance of the Process class, for example, will have its own associated performance counters.

Reading Performance Data

The PerformanceCounter class is provided to allow your .NET applications to read performance data from processes running on a local or remote system. Table 14.4 details some important members of the PerformanceCounter class.

Table 14.4. Some Important Members of the PerformanceCounter Class

Member

Type

Description

CategoryName

Property

Performance counter category name

Close

Method

Closes the performance counter and releases its resources

CounterHelp

Property

Performance counter description

CounterName

Property

Performance counter name

CounterType

Property

Performance counter type

Decrement

Method

Decrements the performance counter value by one

Increment

Method

Increments the performance counter value by one

IncrementBy

Method

Increments or decrements the performance counter value by the specified amount

InstanceName

Property

Instance name

MachineName

Property

Computer name

NextSample

Method

Returns the raw, uncalculated value newly sampled for a performance counter

NextValue

Method

Returns the calculated value newly sampled from a performance counter

RawValue

Property

Returns a sampled raw value from a performance counter

ReadOnly

Property

Indicates whether a PerformanceCounter object is in read-only mode

RemoveInstance

Method

Removes an instance from a PerformanceCounter object

Sampling is the process of reading the value of a performance counter. You may sample existing performance counters on local and remote systems as well as create new custom performance counters on the local system. You may view sampled performance data using the Performance Monitor ( perfmon.exe ) utility as well as within your .NET application. A listing of available performance counters can be easily accessed within the Server Explorer of Visual Studio .NET, where you may drag and drop the desired counter into your application (see Figure 14.3).

Figure 14.3. Selection of the Available Bytes performance counter within the Server Explorer.

graphics/14fig03.jpg

Publishing Performance Data

The .NET Framework also allows you to create your own custom performance counters, which can then be accessed from other applications, as well as to use the Performance Monitor utility. You can use the Create method of PerformanceCounterCategory to programmatically add a new category, or you can right-click the Performance Counters node of the Server Explorer and select Create New Category from the options provided.

graphics/note_icon.gif

When you create a new performance counter, it must be placed within a new category. It is not possible to place a custom counter within an existing category, although it is possible to add multiple new performance counters within the newly created category.


To practice reading performance data, follow these steps:

  1. Create a new Visual Basic .NET Windows Application project.

  2. Place a ListView control ( lvwPerformance ) on the default form. Change the View property of the ListView control to Details. Click the Columns property of the ListView control and add four column headers with the Text properties of Counter Name ( chChounterName ), Counter Type (chCounterType), Time Stamp ( chTimeStamp ), and Raw Value ( chRawValue ).

  3. Drag a Timer component onto the form. Set the Enabled property to True and the Interval property to 500.

  4. Open the Server Explorer and select the server from the Servers node. Select the % Processor Time performance counter by navigating to Performance Counters, Processor, % Processor Time, Total from the Servers node. Drag the Total counter to the form. Name the counter pcTime.

  5. Switch to the code view. Add the following code to the top of the form's module:

     Imports System.Diagnostics 
  6. Attach an event handler to the Tick event of the Timer control and add the following code in the event handler:

     Private Sub Timer1_Tick(ByVal sender As System.Object, _  ByVal e As System.EventArgs) Handles Timer1.Tick     ' Get the next performance data     ' Add the data to the listview     Dim csSample As CounterSample = pcTime.NextSample()     Dim lviItem As ListViewItem = _      New ListViewItem(New String() _      {pcTime.CounterName, _      csSample.CounterType.ToString(), _      csSample.TimeStamp.ToString(), _      csSample.RawValue.ToString()})     lvwPerformance.Items.Add(lviItem) End Sub 
  7. Set the form as the startup object for the project and set the project as the startup project for the solution.

  8. Run the project. Every second a new performance value is added as a new row to the list view, as shown in Figure 14.4.

    Figure 14.4. The Processor Time performance counter.

    graphics/14fig04.jpg



Developing and Implementing WindowsR-based Applications with Visual BasicR. NET and Visual StudioR. NET Exam CramT 2 (Exam 70-306)
Developing and Implementing WindowsR-based Applications with Visual BasicR. NET and Visual StudioR. NET Exam CramT 2 (Exam 70-306)
ISBN: N/A
EAN: N/A
Year: 2002
Pages: 188

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