« XML Serialization Support Reminders | Main | Serial COM port IO »

Performance Counters Pattern

The following class encapsulates the performance counters used by a module and includes the CreateCategory method to register the counters if this is the first time the program is run on the local machine.

         public class PerformanceCounters {

              static readonly string category = "MVC Folgers Seal Inspection";

              public PerformanceCounter ImagesInspected;

              public PerformanceCounter ImagesInspectedRate;

              public PerformanceCounters() {

                  if (!PerformanceCounterCategory.Exists(category)) CreateCategory();

                  ImagesInspected = new PerformanceCounter(category, "Images Inspected", false);

                  ImagesInspectedRate = new PerformanceCounter(category, "Images Inspected Rate", false);

              }

 

              private void CreateCategory() {       

                  CounterCreationDataCollection ccdc = new CounterCreationDataCollection();

       

                  // Add ImagesInspected counter.

                  ccdc.Add(new CounterCreationData

                       ( "Images Inspected"

                       , "The number of images inspected."

                       , PerformanceCounterType.NumberOfItems32

                       ));

       

                  // Add the ImagesInspectedRate counter.

                  ccdc.Add(new CounterCreationData

                       ( "Images Inspected Rate"

                       , "The number of images inspected per second."

                       , PerformanceCounterType.RateOfCountsPerSecond32

                       ));

 

                  // Create the category.

                  PerformanceCounterCategory.Create

                       ( category

                       , "Counters provided by the Folgers Seal Inspection machine vision application."

                       , ccdc

                       );

              }

         }

To use a performance counter, do something like:

     pcs = new PerformanceCounters();

     pcs.ImagesInspected.Increment();

 

Post a comment

(If you haven't left a comment here before, you may need to be approved by the site owner before your comment will appear. Until then, it won't appear on the entry. Thanks for waiting.)