Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Extracting Performance Data from Your .NET Applications : Page 3

Using only three classes from the .NET Framework class library, you can quickly create performance counters and publish performance data about your .NET applications.


advertisement
Creating Performance Counters Programmatically
To create your performance counters in code, you will use a series of classes from the System.Diagnostics namespace. Let's start off with the CounterCreationDataCollection class. This collection class holds CounterCreationData instances; these physically represent the custom counters. Therefore, after instantiating a CounterCreateDataCollection object, you will then create your counters by building CounterCreateData objects and adding them to the CounterCreationDataCollection instance. The following snippet shows the code necessary to create the records loaded/second counter.

// C# // First create the CounterCreationDataCollection // object counters = new CounterCreationDataCollection(); // Create the counter instances to add to the // collection... CounterCreationData ctr = new CounterCreationData(); // Set the counter's properties ctr.CounterName = "Records Loaded/sec"; ctr.CounterType = PerformanceCounterType.RateOfCountsPerSecond32; // Add the counter to the collection counters.Add(ctr);

After you have built up your collection of counters, you must create them in a particular category using the PerformanceCounterCategory class. This class exposes a static/shared member called Create. After passing the category name, category help topic, and the CounterCollectionData instance in to the Create method, the counter(s) will be registered on the local computer. In addition to creating the counter, the Create method will also create the category if it doesn't currently exist on the target machine.

// Create the counter PerformanceCounterCategory.Create ("Northwind Data Loader", "", counters);

Publishing Performance Data
The counters you created can now accept data. Let's add the publishing code to the data loader. Figure 3 shows the basic architecture of the Northwinds data loader component. We will place all of the important activity, from a performance perspective, inside the Loader class. Through its ProcessFile method, the Loader class opens the data file and sends each line through to a SqlCommand object (via the private InsertRow routine) that performs the insert into the Products table in the Northwind database. In the following pseudo-code for the record insertion process, it becomes clearer exactly where you should publish the performance counter data.



public void ProcessFile() { // Open the file // Loop: read in each line // Parse the file line // Try: Insert the values // Update perf counters // Continue loop // Catch: // Update per counters // Continue loop // End loop // Close the file }

There are really two important points within the source: when a record is successfully inserted, and when an exception is encountered during a record insert.

With the code sites located, you can now focus on publishing the performance data. Writing to performance counters involves using the PerformanceCounter class from the System.Diagnostics namespace. The PerformanceCounter class represents a registered instance of a performance counter. Performance counters are uniquely identified by name; the x class allows you to identify the counter instance you want to work with through its CounterName property. You will also identify the counter through its category name (via the CategoryName property) and through the machine name that is hosting the counter (via the MachineName property). This code will create a 'connection' to our previously created records loaded/sec counter.

// Configure the loadsPerSec counter loadsPerSec = new PerformanceCounter(); // Set the category name loadsPerSec.CategoryName = "Northwind Loader"; // Set the counter name loadsPerSec.CounterName = "Records Loaded/sec"; // Set the machine name loadsPerSec.MachineName = ".";

You will also set the ReadOnly property (to indicate that you want to write into the counter), and the RawValue property. The code uses the RawValue property to directly assign a value to the counter. In the code snippet below, I use RawValue to initialize the counter to 0.

loadsPerSec.ReadOnly = false; loadsPerSec.RawValue = 0;

You can create handles to the other counters in a similar fashion. Once you have the handle to a counter, to record a newly loaded record you just need to call the PerformanceCounter.Increment() method. This method will increment the counter's raw value by 1. Although the records loaded/sec counter represents an average value over time, you don't need to write any other code. The counter handles averaging and other calculations internally.

loadsPerSec.Increment();

 
Figure 6: This graph shows the DataLoader application in action. At its peak, it processed just short of 1,100 records per second.
Listing 1 shows the completed Loader class. To simplify the code a bit, I created a CounterHelper class (see Listing 2). I use CounterHelper to create the PerformanceCounter objects and expose three public methods that the Loader class uses to increment those counters: IncLoadsPerSec, IncErrorsPerSec, and IncTotalLoaded.

To see the performance counters in action, run the Windows Performance Monitor and then run the data loader application. You begin the load process by dropping a test file into the loader's data directory. Figure 6 shows the performance counters during the load of a file with approximately 31,000 records. Some records contained errors (as shown by the errors/sec counter).

By leveraging performance counters in your applications, you can generate an accurate view of your application's performance levels, whether during the construction phase or after deployment into production. Accompanying source code for this article, in both Visual Basic and C#, is available at http://www.brilliantstorm.com/.



Lars Powers, MCSD .NET has over 12 years of experience designing and implementing solutions on the Microsoft platform. He is the co-author of "Visual Basic Programmer's Guide to the .NET Framework Class Library" (SAMS Publishing). You can reach him here.
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date