Performance Monitoring


Performance monitoring can be used to get information about the normal behavior of applications. Performance monitoring is a great tool that helps you understand the workload of the system and observe changes and trends, particularly in applications running on the server.

Microsoft Windows has a lot of performance objects, such as System, Memory, Objects, Process, Processor, Thread, Cache, and so on. Each of these objects has many counts to monitor. For example, with the Process object, the user time, handle count, page faults, thread count, and so on can be monitored for all processes or for specific process instances. Some applications, such as SQL Server, also add application-specific objects.

For the quote service sample application it might be interesting to get information about the number of client requests, the size of the data sent over the wire, and so on.

Performance-Monitoring Classes

The System.Diagnostics namespace provides these classes for performance monitoring:

  • PerformanceCounter can be used both to monitor counts and to write counts. New performance categories can also be created with this class.

  • PerformanceCounterCategory enables you to step through all existing categories as well as create new ones. You can programmatically get all the counters in a category.

  • PerformanceCounterInstaller is used for the installation of performance counters. Its use is similar to that of the EventLogInstaller discussed previously.

Performance Counter Builder

The sample application is a simple Windows application with just one button so that you can see how to write performance counts. In a similar way, you can add performance counters to a Windows Service (see Chapter 22, “Windows Service”), to a network application (see Chapter 35, “Accessing the Internet”) or to any other application from which you would like to receive live counts.

Using Visual Studio, you can create a new performance counter category by selecting the performance counters in the Server Explorer and by selecting the menu entry Create New Category on the context menu. This launches the Performance Counter Builder (see Figure 17-7).

image from book
Figure 17-7

Set the name of the performance counter category to Wrox Performance Counters. The following table shows all performance counters of the quote service.

Open table as spreadsheet

Name

Description

Type

# of Button clicks

Total # of button clicks

NumberOfItems32

# of Button clicks/sec

# of button clicks in one second

RateOfCountsPerSecond32

# of Mouse move events

Total # of mouse move events

NumberOfItems32

# of Mouse move events/sec

# of mouse move events in one second

RateOfCountsPerSecond32

The Performance Counter Builder writes the configuration to the performance database. This can also be done dynamically by using the Create() method of the PerformanceCounterCategory class in the System.Diagnostics namespace. An installer for other systems can easily be added later using Visual Studio.

Adding PerformanceCounter Components

Now you can add PerformanceCounter components from the toolbox. Instead of using the components from the toolbox category Components, you can directly drag and drop the previously created performance counters from the Server Explorer to the design view. This way, the instances are configured automatically; the CategoryName property is set to “Wrox Performance Counters” for all objects, and the CounterName property is set to one of the values available in the selected category. Because with this application the performance counts will not be read but written, you have to set the ReadOnly property to false. Also set the MachineName property to ., so that the application writes performance counts locally.

Here is a part of the code generated into InitalizeComponent() by adding the PerformanceCounter components to the Designer and by setting the properties as indicated previously:

  private void InitializeComponent() {    //...    //    // performanceCounterButtonClicks    //    this.performanceCounterButtonClicks.CategoryName = "Wrox Performance Counts";    this.performanceCounterButtonClicks.CounterName = "# of Button Clicks";    this.performanceCounterButtonClicks.ReadOnly = false;    //    // performanceCounterButtonClicksPerSec    //    this.performanceCounterButtonClicksPerSec.CategoryName =          "Wrox Performance Counts";    this.performanceCounterButtonClicksPerSec.CounterName =          "# of Button Clicks / sec";    this.performanceCounterButtonClicksPerSec.ReadOnly = false;    //    // performanceCounterMouseMoveEvents    //    this.performanceCounterMouseMoveEvents.CategoryName =          "Wrox Performance Counts";    this.performanceCounterMouseMoveEvents.CounterName =          "# of Mouse Move Events";    this.performanceCounterMouseMoveEvents.ReadOnly = false;    //    // performanceCounterMouseMoveEventsPerSec    //    this.performanceCounterMouseMoveEventsPerSec.CategoryName =          "Wrox Performance Counts";    this.performanceCounterMouseMoveEventsPerSec.CounterName =          "# of Mouse Move Events / sec";    this.performanceCounterMouseMoveEventsPerSec.ReadOnly = false;    //... } 

For the calculation of the performance values, you have to add the fields clickCountPerSec and mouseMoveCountPerSec to the class Form1:

 public partial class Form1 : Form {    // Performance monitoring counter values    private int clickCountPerSec = 0;    private int mouseMoveCountPerSec = 0; 

Add an event handler to the Click event of the button and an event handler to the MouseMove event to the form, and add the following code to the handlers:

  private void button1_Click(object sender, EventArgs e)  {     performanceCounterButtonClicks.Increment();     clickCountPerSec++;  }  private void OnMouseMove(object sender, MouseEventArgs e)  {     performanceCounterMouseMoveEvents.Increment();     mouseMoveCountPerSec++;  } 

The Increment() method of the PerformanceCounter object increments the counter by one. If you need to increment the counter by more than one, for example to add information about a byte count sent or received, you can use the IncrementBy() method. For the performance counts that show the value in seconds, just the two variables, clickCountPerSec and mouseMovePerSec, are incremented.

To show updated values every second, add a Timer component. Set the OnTimer() method to the Elapsed event of this component. The OnTimer() method is called once per second if you set the Interval property to 1000. In the implementation of this method, set the performance counts by using the RawValue property of the PerformanceCounter class:

  protected void OnTimer (object sender, System.Timers.ElapsedEventArgs e) {    performanceCounterButtonClicksPerSec.RawValue = clickCountPerSec;    clickCountPerSec = 0;    performanceCounterMouseMoveEventsPerSec.RawValue = mouseMoveCountPerSec;    mouseMoveCountPerSec = 0; } 

The timer must be started:

 public Form1() {    InitializeComponent();    this.timer1.Start(); }

perfmon.exe

Now you can monitor the application. You can start the Performance tool by selecting Administrative Tools image from book Performance with Windows XP or Reliability and Performance Monitor with Windows Vista. Select the Performance Monitor, and click the + button in the toolbar where you can add performance counts. The Quote Service shows up as a performance object. All the counters that have been configured show up in the counter list, as shown in Figure 17-8.

image from book
Figure 17-8

After you’ve added the counters to the performance monitor, you can see the actual values of the service over time (see Figure 17-9). Using this performance tool, you can also create log files to analyze the performance at a later time.

image from book
Figure 17-9




Professional C# 2005 with .NET 3.0
Professional C# 2005 with .NET 3.0
ISBN: 470124725
EAN: N/A
Year: 2007
Pages: 427

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