Cinchoo – Using Performance Counters, Part 1

In this section I’ll discuss about how we can use Cinchoo framework’s performance counter helper class to gather data from an application. So we will first understand the fundamentals and then we will see a simple example from which we will collect some performance data.

Before we begin, if you like to know more information about Performance Counter, please visit below links

MSDN Performance Counter Class

An Article on Performance Counter in CodeProject

Cinchoo framework provides nifty performance counter helper classes to makes it easier for developers to develop applications that use Performance Counter to monitor the health of the application.

There are two types of Performance Counter Categories

  • SingleInstance – The performance counter category can have only a single instance.
  • MultipleInstance – The performance counter category can have multiple instances.

Creating and using SingleInstance Performance Counters

1. Add reference to Cinchoo.Core.dll assembly

2. Namespace Cinchoo.Core.Instrumentation.Performance

[ChoPerformanceCounterCategory("Random Single Instance Test", PerformanceCounterCategoryType.SingleInstance)]
public sealed class SingleInstancePerfCounters : ChoPerformanceCounterCategory
{
    #region Performance Counters

    [ChoPerformanceCounter("Random Rate", "", PerformanceCounterType.RateOfCountsPerSecond32)]
    public readonly ChoPerformanceCounter PCRandomRate = null;

    [ChoPerformanceCounter("Random Average", "", PerformanceCounterType.AverageCount64)]
    public readonly ChoPerformanceCounter PCRandomAverage = null;

    #endregion
}
 Now lets see how to instantiate and use them
[STAThread]
static void Main(string[] args)
{
    SingleInstancePerfCounters perfCounter = new SingleInstancePerfCounters();

    Random rand = new Random(42);
    int randMax = 100;

    while (true)
    {
        int r = rand.Next(randMax);

        if (perfCounter.PCRandomRate != null)
            perfCounter.PCRandomRate.Increment();
        if (perfCounter.PCRandomAverage != null)
            perfCounter.PCRandomAverage.IncrementBy(r);

        Thread.Sleep(1000);
    }
}

Creating and using MultipleInstance Performance Counters

1. Add reference to Cinchoo.Core.dll assembly

2. Namespace Cinchoo.Core.Instrumentation.Performance

[ChoPerformanceCounterCategory("Random Multiple Instance Test", PerformanceCounterCategoryType.MultiInstance)]
public sealed class MultipleInstancePerfCounters : ChoPerformanceCounterCategory
{
    #region Performance Counters

    [ChoPerformanceCounter("Random Value", "", PerformanceCounterType.NumberOfItems64, "Standard")]
    public readonly ChoPerformanceCounter PCRandomValue;

    [ChoPerformanceCounter("Random Value", "", PerformanceCounterType.NumberOfItems64, "Inverted")]
    public readonly ChoPerformanceCounter PCRandomInvertedValue;

    #endregion
}

Please remember that the above two Performance counter members are given same category name ‘Random Value’ and different instance names. It qualifies them to create multiple instances of performance counters.

Now lets see how to instantiate and use them

[STAThread]
static void Main(string[] args)
{
    MultipleInstancePerfCounters perfCounter = new MultipleInstancePerfCounters();

    Random rand = new Random(42);
    int randMax = 100;

    while (true)
    {
        int r = rand.Next(randMax);

        if (perfCounter.PCRandomValue != null)
            perfCounter.PCRandomValue.RawValue = r;
        if (perfCounter.PCRandomInvertedValue != null)
            perfCounter.PCRandomInvertedValue.RawValue = randMax - r;

        Thread.Sleep(1000);
    }
}

In the next part, will see how to control the Performance Counters through configuration.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s