Click here to Skip to main content
6,596,602 members and growing! (19,475 online)
Email Password   helpLost your password?
Languages » VB.NET » Samples     Intermediate License: The Code Project Open License (CPOL)

Using custom attributes to add performance counters to your application

By Duncan Edwards Jones

A brief overview of what performance counters are and a library that makes their use a bit easier.
VB.NET 1.0, .NET 1.1, .NET 2.0, Win2K, WinXP, Win2003VS.NET2003, VS2005, Dev
Posted:8 Feb 2006
Views:40,431
Bookmarked:23 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
12 votes for this article.
Popularity: 4.59 Rating: 4.25 out of 5
1 vote, 8.3%
1
2 votes, 16.7%
2

3
5 votes, 41.7%
4
4 votes, 33.3%
5

Introduction

A performance counter monitors the behavior of performance objects on a computer. These include physical components, such as processors, disks, and memory, and system objects, such as processes and threads. There are also a large number of performance counters created (and maintained) by the .NET common language run time environment, including % time spent in garbage collection, and total size of managed heaps etc.

The windows preformance monitoring tool

The behaviour of your system can be monitored by running the perfmon.exe utility which has the interface as shown above. You select the performance counters that you are interested in and they are graphed for you.

Creating custom performance counters

The .NET framework includes two classes that you can use to interact with the performance monitor: PerformanceCounterCategory and PerformanceCounter - using these, you can create custom performance counters to monitor aspects of your own application to keep an eye on its performance.

Attribute for performance monitoring

The attached library contains two attribute derived classes, PerformanceCounterCategoryAttribute and PerformanceCounterAttribute, which you can use to decorate your business object classes, and then use the shared functions in the PerformanceCounterUtilities class to create and update these performance counters.

Associating a class with a performance counter category

To associate a class with a particular performance counter category, you decorate that class definition with a PerformanceCounterCategoryAttribute attribute.

<PerformanceCounterCategory("Attribute Test", _
"Test of the performance counter utility library")> _
Public Class PerformanceCounterTestClass
'- - 8< - - - - - - - - - - - - - - - - - - -

Associating a property of a class with a performance counter

To associate a public property of your class with a particular performance counter, you decorate that property definition with a PerformanceCounterAttribute attribute.

'- - 8< - - - - - - - - - - - - - - - - - - - 

<PerformanceCounter("Value List Length", _
"The number of items in the value list", _ 
PerformanceCounterType.NumberOfItems32)> _
Public ReadOnly Property CurrentList() As Collection
'- - 8< - - - - - - - - - - - - - - - - - - -

There are overloaded constructors for the PerformanceCounterAttribute attribute that allow you to specify the type of the performance counter (if none is specifically selected, PerformanceCounterType.NumberOfItems32 is assumed), and for those performance counter types that require an associated base performance counter (for example, a RawFraction counter type requires an associated Rawbase counter type), you can specify the name of the property that supplies the base performance category.

(Re)Creating the performance counters

Before you can assign values to the various performance counters, you need to create them. To do this, you pass an instance of your class (that has the attributes on it) to RebuildPerformanceCounterCategory. This creates a set of CounterCreationData objects and puts them in the right order needed to create the performance counters for that object type.

Private TestClass As New PerformanceCounterTestClass
'\\ Create and register the performance counter categories       

PerformanceCounterUtilities.PerformanceCounterUtilities.
            RebuildPerformanceCounterCategory(TestClass)

Updating all the performance counters from an instance of a class

To update all the performance counters associated with a class, call the UpdatePerformanceCounter method and pass the class instance in.

PerformanceCounterUtilities.UpdatePerformanceCounter(TestClass)

Updating a single performance counter from a class and property name

To update a single performance counter associated with a class property, call the UpdatePerformanceCounter method and pass the class instance and the property name in.

PerformanceCounterUtilities.UpdatePerformanceCounter(TestClass, _
                                                   "Iterations")

Usage scenarios

Custom performance counters are a great way of instrumenting your .NET applications in order to monitor the performance and diagnose any bottlenecks or problems with that application, and in the long term, they can save a great deal of stress and hair loss.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Duncan Edwards Jones


Member
Microsoft MVP 2006, 2007
Visual Basic .NET
Occupation: Software Developer (Senior)
Location: Ireland Ireland

Other popular VB.NET articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 13 of 13 (Total in Forum: 13) (Refresh)FirstPrevNext
Generaluse for wcf service PinmemberMember 336990916:52 15 Dec '08  
GeneralPerformanceCounterAttribute Pinmembershatl2:08 4 Sep '06  
GeneralRe: PerformanceCounterAttribute PinmemberDuncan Edwards Jones2:18 4 Sep '06  
GeneralExample of usage PinmemberDSSI-Ryan15:50 15 Mar '06  
GeneralRe: Example of usage PinmemberDuncan Edwards Jones12:55 20 Mar '06  
GeneralRe: Example of usage PinmemberDSSI-Ryan15:55 20 Mar '06  
GeneralRe: Example of usage PinmemberDuncan Edwards Jones21:49 20 Mar '06  
GeneralRe: Example of usage Pinmemberastanton19783:12 1 Dec '08  
GeneralNice Job Pinmembercomputerguru923825:42 15 Feb '06  
GeneralRe: Nice Job PinmemberDuncan Edwards Jones6:14 15 Feb '06  
GeneralRe: Nice Job Pinmembercomputerguru923827:49 15 Feb '06  
GeneralRe: Nice Job PinmemberHenk-Jan Visscher1:10 16 Feb '06  
GeneralRe: Nice Job PinmemberDuncan Edwards Jones1:19 16 Feb '06  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 8 Feb 2006
Editor: Smitha Vijayan
Copyright 2006 by Duncan Edwards Jones
Everything else Copyright © CodeProject, 1999-2009
Web16 | Advertise on the Code Project