Click here to Skip to main content
13,202,262 members (55,637 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


20 bookmarked
Posted 20 Mar 2010

Simplified Performance Counters for Timed Operations

, 20 Mar 2010
Rate this:
Please Sign up or sign in to vote.
A generalized code block which can be used in a common scenario for simple performance benchmarking.


Setting up and using Performance Counters with the .NET API requires a bit of setup and configuration. This article presents a generalized code block which can be used in a common scenario for simple performance benchmarking.



Finding how fast your application runs would be tricky business lacking any way to measure it. In Windows, we've got the built-in Performance Counters which can easily time operations and gather statistics about your software while it executes.

.NET offers a fairly robust API for accessing Performance Counters. It exposes classes and methods for setting and incrementing the counter values, and also the ability to create all the different varieties directly from your program. For an excellent starting point for using all of these, check out An Introduction To Performance Counters by Michael Groeger and Using performance counters to gather performance data by Shivprasad koirala.

As some superhero may have mentioned, "With great flexibility comes great complexity", so we'll look at a way to wrap a common usage with a reusable code block.

Using the Code

My first annoyance with the performance counter classes in .NET is the need to initialize two different classes for a single counter - one for creating it and another for interacting with the values. Since they have several overlapping properties which, by the way, need to be exactly the same or if they won't work, the definitions can be easily coupled:

class CounterCreationCounter
    public CounterCreationData Creation { get; private set; }
    public PerformanceCounter Counter { get; private set; }

    public CounterCreationCounter(
       string counterName, string counterHelp, 
	PerformanceCounterType counterType, string categoryName)
        Creation = new CounterCreationData {
                        CounterName = counterName,
                        CounterHelp = counterHelp,
                        CounterType = counterType };
        Counter = new PerformanceCounter {
                        CategoryName = cate2goryName,
                        CounterName = counterName,
                        MachineName = MACHINE_NAME, 	// this constant is set to "." 
						// for localhost
                        ReadOnly = false };

Next, I want a way to define a group of common counters that will be relevant for measuring operations while benchmarking. To keep the article a reasonable size, I've selected just two:

  • Number of operations per second
  • Average duration of the operation in ticks

There are several more (Total number of operations, and Most recent duration) in the source code which are defined in the same manner.

class OperationalCounterGroup
    private readonly CounterCreationCounter _countPerSec;

    private readonly CounterCreationCounter _averageDuration;

    private readonly CounterCreationCounter _averageDurationBase;

    public OperationalCounterGroup(string operationName, string categoryName)
            = new CounterCreationCounter(
                string.Format("{0} # / sec", operationName),
                string.Format("Number of {0} per second", operationName),

            = new CounterCreationCounter(
                string.Format("{0} avg duration", operationName),
                string.Format("Average duration for {0} in ticks", operationName),

            = new CounterCreationCounter(
                string.Format("{0} avg duration base", operationName),
                string.Format("Average duration for {0} in ticks base", operationName),

    /// <summary>
    /// Operation is complete - increment all counters
    /// </summary>
    public void OperationCompleted(TimeSpan elapsed)

    public IEnumerable<countercreationdata /> GetCreationData()
        yield return _countPerSec.Creation;
        yield return _averageDuration.Creation;
        yield return _averageDurationBase.Creation;

Notice that the OperationCompleted method has the duty of incrementing all of the counters once an operation has been completed. Now, how is all of this wired to something we'd want to actually measure? First, I'll define an interface which exposes an OpeartionCompleted event that will be raised by the object when it has something to measure.

public delegate 
	void OperationCompletedHandler(string operationName, TimeSpan elapsed);

public interface IOperationCompletable
    event OperationCompletedHandler OperationCompleted;

... and here is the OperationPerformanceCounter class to glue it all together. It requires an object implementing IOperationCompletable, along with a list of operations that will be measured.

class OperationPerformanceCounter
	/// <summary>Performance Counter category name</summary>
	private readonly string _category;

	/// <summary>The operations being raised in IOperationCompletable 
         /// to watch</summary>
	private readonly string[] _handledOperations;

	/// <summary>Counter group lookup</summary>
	private readonly IDictionary<string, OperationalCounterGroup> _counterGroups;

	/// <summary>Handler for IOperationCompletable.OperationCompleted</summary>
	private void OperationCompleted(string operationName, TimeSpan elapsed)
		// Only handle operations we know about
		if (_counterGroups.ContainsKey(operationName)) {
			// do all the increments in the PerformanceCounter 
                           // objects for this particular operation

	/// <summary>Constructor</summary>
	public OperationPerformanceCounter(string categoryName, 
				string[] handledOperations,
				IOperationCompletable operationCompletable)
		// Performance category name
		_category = categoryName;
		// List of all the operations that we're interested in measuring
		_handledOperations = handledOperations;

		// create Counter and Creation objects for each of the 
                  // interesting operations
		_counterGroups =
			(from c in _handledOperations
			 select new {Name = c, Group = 
				new OperationalCounterGroup(c, _category)})
				.ToDictionary(o => o.Name, o => o.Group);

		// Create (or update) the counters in Windows
		UpdateOrCreateCategory((from c in _counterGroups
				from cd in c.Value.GetCreationData()
				select cd).ToArray());

		// Watch the object for the operations completing
		operationCompletable.OperationCompleted += OperationCompleted;

The UpdateOrCreateCategory method handles all of the Performance Counter creation automatically. It will also recognize if there are new (or removed) operations and update the local counters:

void UpdateOrCreateCategory(CounterCreationData[] counterCreationData)
    // update mode
    if (PerformanceCounterCategory.Exists(_category))
        var installedCounters = new HashSet<string>(
            from ec in new PerformanceCounterCategory(_category).GetCounters()
            select ec.CounterName);

        var knownCounters = new HashSet<string>(
            from c in counterCreationData
            select c.CounterName);

        // if the installed ones are different, then update
        if (!installedCounters.SetEquals(knownCounters))

void CreateCategory(CounterCreationData[] counterCreationData)
        _category, "", PerformanceCounterCategoryType.Unknown,
        new CounterCreationDataCollection(counterCreationData));

The code to configure and use OperationPerformanceCounter is quite simple. First, an object implementing IOperationCompletable. Here is a demonstration - this object can raise events for two operations that we're interested in - FastOp and SlowOp.

class DemoObject : IOperationCompletable
    public event OperationCompletedHandler OperationCompleted;

    private int _fastOpSleep;

    public void FastOp()
    	Stopwatch sw = new Stopwatch();
       _fastOpSleep += 1; // increment how long it takes next time by 1
	OperationCompleted("FastOp", sw.Elapsed);

    private int _slowOpSleep;
    public void SlowOp()
    	Stopwatch sw = new Stopwatch();
       _slowOpSleep += 2; // twice as slow compared to FastOp!
	OperationCompleted("SlowOp", sw.Elapsed);

And a simple harness in a console application. It calls the operations in separate threads so that the Performance Monitor graphs will directly overlap.

static void Main()
    var demoObject = new DemoObject();
    var opCounter =
        new OperationPerformanceCounter(
            "CodeProject - Simplified Performance Counters", 
            new[] {"FastOp", "SlowOp"},

    var threads
        = new[]
            {new Thread(() => LoopAction(demoObject.FastOp)),
            new Thread(() => LoopAction(demoObject.SlowOp))};

    foreach (var t in threads)

    foreach (var t in threads) // wait for completion

/// <summary>Runs the given action a number of times in a loop</summary>
private static void LoopAction(Action action)
    for (var i = 0; i < 200; i++)

Running this will add the performance counters and record the following data - here are the resulting graphs created while comparing "Operations per Second" and "Average Duration" for "FastOp" and "SlowOp".




  • 20th March, 2010: Initial post


This article, along with any associated source code and files, is licensed under The Creative Commons Attribution-ShareAlike 2.5 License


About the Author

James Kolpack
Software Developer (Senior)
United States United States
No Biography provided

You may also be interested in...

Comments and Discussions

QuestionWhat about the StopWatch class Pin
smesser20-Mar-10 17:39
membersmesser20-Mar-10 17:39 
AnswerRe: What about the StopWatch class Pin
James Kolpack20-Mar-10 17:57
memberJames Kolpack20-Mar-10 17:57 
GeneralRe: What about the StopWatch class Pin
smesser21-Mar-10 11:03
membersmesser21-Mar-10 11:03 
I guess I don't understand completely the usage of performance counters.

I have always just timed how long something takes.

I just brought up the StopWatch class as I created one from scratch once not realizing that the framework had already done it for me.

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.171020.1 | Last Updated 20 Mar 2010
Article Copyright 2010 by James Kolpack
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid