February 12, 2016
Hot Topics:

Performance Counters Determine Application Performance

  • May 11, 2004
  • By Mark Strawmyer
  • Send Email »
  • More Articles »

Creation and Setup of Performance Counters

Performance counters are fairly easy to set up during development and testing, but they can be tricky to set up and deploy in production.

Performance Counter Installation with Server Explorer

The simplest way to set up performance categories and counters is by using Server Explorer, which is an administrative component available in several editions of Visual Studio .NET. If you don't see Server Explorer, go to the View menu and select the Server Explorer option. You can create new categories and as many counters for each category as you desire. Any base type you use must immediately follow the counter that it will support. The following screenshots show the Server Explorer (Figure 1) and the Performance Counter Builder (Figure 2) you use to create categories and counters within Visual Studio.

Figure 1—Server Explorer

Figure 2—Performance Counter Builder

Performance Counter Installation Sample Code

A user can programmatically create counters at run time, but the user first must have the proper administrative rights (which the ASP.NET user does not have by default). This means you must elevate the rights of the ASPNET account, use impersonation within your application, or create a setup package with install actions to set up the counters.

To programmatically create the counters, create an instance of System.Diagnostics.CounterCreationDataCollection to hold counter instances. Then, create instances of the System.Diagnostics.CounterCreationData class and add them to the collection. Once you've added all of the counters into the collection, use the System.Diagnostics.PerformanceCounterCategory.Create method to create the new category and all of the related counters stored in the collection. The following sample code programmatically creates the counters, assuming your user account has the necessary rights to do so:

// Set up the performance counter(s) if they don't already existif( !PerformanceCounterCategory.Exists("CodeGuru Sample") ){   // Create the collection container   CounterCreationDataCollection counters = new      CounterCreationDataCollection();   // Create counter #1 and add it to the collection   CounterCreationData tests = new CounterCreationData();   tests.CounterName = "Total Tests Executed";   tests.CounterHelp = "Total number of tests executed.";   tests.CounterType = PerformanceCounterType.NumberOfItems32;   counters.Add(tests);   // Create counter #2 and add it to the collection   CounterCreationData testsPerSec = new CounterCreationData();   testsPerSec.CounterName = "Tests Executed / sec";   testsPerSec.CounterHelp = "Number of tests executed per second.";   testsPerSec.CounterType =      PerformanceCounterType.RateOfCountsPerSecond32;   counters.Add(testsPerSec);   // Create counter #3 and add it to the collection   CounterCreationData avgTest = new CounterCreationData();   avgTest.CounterName = "Average Test Duration";   avgTest.CounterHelp = "Average time to execute a test.";   avgTest.CounterType = PerformanceCounterType.AverageTimer32;   counters.Add(avgTest);   // Create counter #4 and add it to the collection   CounterCreationData avgTestBase = new CounterCreationData();   avgTestBase.CounterName = "Average Test Duration Base";   avgTestBase.CounterHelp = "Average time to execute a test base.";   avgTestBase.CounterType = PerformanceCounterType.AverageBase;   counters.Add(avgTestBase);   // Create the category and all of the counters.   PerformanceCounterCategory.Create("CodeGuru Sample",       "Sample performance counters for CodeGuru article.",      counters);}

Page 2 of 3

Comment and Contribute


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



Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Sitemap | Contact Us

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