10/23/11

C# Custom Performance Counters - First Performance Counter

Table of content:

I. C# Custom Performance Counters - Introduction
II. C# Custom Performance Counters - First Performance Counter

1. Creating a custom Performance Counters and Performance counters category.

You can create your own Performance category and Performance counters. To achieve this goal you can use Visual Studio tools, or .NET System.Diagnostics objects. In this post I’ll describe counters and category creation in a code using
using System.Diagnostics;

First of all you need to make a counters collection:

CounterCreationDataCollection collection = new CounterCreationDataCollection();

Then you need to create performance counter

CounterCreationData counter = new CounterCreationData();
counter.CounterName = "CountsPerSecond";
counter.CounterHelp = "CountsPerSecond";
counter.CounterType = PerformanceCounterType.RateOfCountsPerSecond32);
collection.Add(counter);

And place it into the category. As the category type you can use SingleInstance and MultiInstance. Multi instance is very usefull when dealing with multithread application. I will describe this in a next post.

if (!PerformanceCounterCategory.Exists("TestCategory"))
{
    PerformanceCounterCategory.Create("TestCategory", "This is test category", PerformanceCounterCategoryType.SingleInstance, collection);
}

Here is the complete code:

using System.Diagnostics;

namespace PerformaceCountersTest
{
    class Program
    {
        static void Main(string[] args)
        {
            CounterCreationDataCollection collection = new CounterCreationDataCollection();

            CounterCreationData counter = new CounterCreationData();
            counter.CounterName = "CountsPerSecond";
            counter.CounterHelp = "CountsPerSecond";
            counter.CounterType = PerformanceCounterType.RateOfCountsPerSecond32);
            collection.Add(counter);

            if (!PerformanceCounterCategory.Exists("TestCategory"))
            {
                 PerformanceCounterCategory.Create("TestCategory", "This is test category", PerformanceCounterCategoryType.SingleInstance, collection);
            }

        }
    }
}

When you run this code, you should see a new category with one counter in Windows performance tool:

If you don't know how to use Windows Performance tool, please look at the
C# Custom Performance Counters part I- Introduction


2. Using the performance counters in a code.

When we have defined the performance counters, it is time to use it.

First of all we need to create an instance of a counter and invoke method Increment()

PerformanceCounter countsPerSec = new PerformanceCounter("TestCategory", "CountsPerSecond", false);
countsPerSec.Increment();

Now it's time to test it:

using System.Diagnostics;

namespace PerformaceCountersTest
{
    class Program
    {
        static void Main(string[] args)
        {
            PerformanceCounter countsPerSec = new PerformanceCounter("TestCategory", "CountsPerSecond", false);

            for (int i = 0; i < 100; i++)
            {
                countsPerSec.Increment();

                int sleep = 1000 - i * 10;
                Console.WriteLine(sleep);
                System.Threading.Thread.Sleep(sleep);
            }
        }
    }
}

If you setup the Performance tool to watch at your performance counter, you should get a result like this:
3. Test application.

Here is the test application code:

PerformanceCounterCreator class
using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;

namespace PerformaceCountersTest
{
    class PerformanceCounterCreator
    {

        CounterCreationDataCollection collection = new CounterCreationDataCollection();

        public PerformanceCounterCreator()
        {
        }

        public void addPerformaceCounter(String name, String description, PerformanceCounterType type)
        {
            CounterCreationData counter = new CounterCreationData();
            counter.CounterName = name;
            counter.CounterHelp = description;
            counter.CounterType = type;
            collection.Add(counter);
        }

        public void createCategory(String categoryName,String categoryHelp, bool force)
        {
            if (PerformanceCounterCategory.Exists(categoryName))
            {
                if (force)
                {
                    PerformanceCounterCategory.Delete(categoryName);
                    PerformanceCounterCategory.Create(categoryName, categoryHelp, PerformanceCounterCategoryType.SingleInstance, collection);
                }
            }
            else
                PerformanceCounterCategory.Create(categoryName, categoryHelp, PerformanceCounterCategoryType.SingleInstance, collection);

        }
    }
}

Main program:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;

namespace PerformaceCountersTest
{
    class Program
    {
        static void Main(string[] args)
        {
            PerformanceCounterCreator creator = new PerformanceCounterCreator();

            creator.addPerformaceCounter("CountsPerSecond", "CountsPerSecond", PerformanceCounterType.RateOfCountsPerSecond32);

            creator.createCategory("TestCategory","This is test category", true);

            //Create counter instance.
            PerformanceCounter countsPerSec = new PerformanceCounter("TestCategory", "CountsPerSecond", false);

            for (int i = 0; i < 100; i++)
            {
                countsPerSec.Increment();

                int sleep = 1000 - i * 10;
                Console.WriteLine(sleep);
                System.Threading.Thread.Sleep(sleep);
            }

        }
    }
}

4. References:
http://www.codeproject.com/KB/dotnet/perfcounter.aspx

http://msdn.microsoft.com/en-us/library/system.diagnostics.performancecountertype.aspx

3 comments:

  1. Pretty good post. I just stumbled upon your blog and wanted to say that I have really enjoyed reading your blog posts. Any way I'll be subscribing to your feed and I hope you post again soon. Big thanks for the useful info. Ad Templates

    ReplyDelete
  2. Thanks for taking the time to discuss this, I feel strongly about it and love learning more on this topic. OMC repair parts

    ReplyDelete