Performance Monitoring C# Help

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 many 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

Performance-Monitoring Classes

¤ 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.

¤ PerformanceCounterlnstaller is used for the installation of performance counters. Its use is similar to that of the EventLoglnstaller 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 Wmdows Service (see Chapter 23, “Windows Services”), to a network application (see Chapter 41, “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

Figure 18-7

Figure 18-7

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

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.5ne CounterName to one of the values available in the selected category. Because with this application the perform~ce counts will not be read but written, you must set the ReadOnly property to false. Also, set the MaehineName property to . so that the application writes performance counts locally. Here is a part of the code generated into Ini talizeComponent ( ) by adding the PerformanceCounter components to the Designer and by setting the properties as indicated previously:

For the calculationof the performance values,you need toadd the fieldsclickCountperSec and mouseMoveCountPerSec tothe classForml:

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 tothe handlers:

The Increment () method of the PerformanceCounter objectincrements the counter by one. Ifyou need toincrement the counter by more than one, forexample toadd information about a byte count sent or received,you can use the IncrementBy () method. For the performance counts thatshow the value inseconds, justthe two variables,clickCountPerSec and mouseMovePerSec, are incremented.

To show updated values every second, add a Timer component. Setthe OnTimej:”() method to the Elapsed event of thiscomponent. The OnTimer () method iscalledonce per second if you set the Intervalproperty to 1000. In the implementation of thismethod, setthe performance counts by using the RawValue property of the Performancecounter class:


Now you can monitor the application. You can start the Performance tool by selecting Administrative Tools ~ Performance with Windows XPor 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.

Figure 18-8

Figure 18-8

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

Figure 18-9

Figure 18-9

Posted on November 3, 2015 in Tracing and Events

Share the Story

Back to Top