Performance counters types with the .NET Framework

NumberOfItems32

Measures a raw value: “shows the most recently observed value.”

using System.Diagnostics;
using System.Threading;

namespace PerformanceCounterExperiments
{
    class Program
    {
        static void Main(string[] args)
        {
            string categoryName = "Experimental";
            string counterName = "number of items";

            // delete the category if it already exists
            if (PerformanceCounterCategory.Exists(categoryName))
            {
                PerformanceCounterCategory.Delete(categoryName);
            }

            // create the performance category and counter
            PerformanceCounterCategory.Create(
                categoryName,
                "",
                PerformanceCounterCategoryType.SingleInstance,
                new CounterCreationDataCollection(new[]
                {
                    new CounterCreationData(
                        counterName,
                        "",
                        PerformanceCounterType.NumberOfItems32)
                }));

            // obtain the writable counter object
            PerformanceCounter counter = new PerformanceCounter(
                categoryName,
                counterName,
                false);

            // increment the counter every 100 milliseconds
            Console.WriteLine("Starting");
            while (true)
            {
                counter.Increment();
                Thread.Sleep(100);
            }
        }
    }
}

AverageCount64

Measures ITEMS per OPERATION: “how many items are processed, on average, during an operation.”

// create the performance category and counters
PerformanceCounterCategory.Create(
    categoryName,
    "",
    PerformanceCounterCategoryType.SingleInstance,
    new CounterCreationDataCollection(new[]
    {
        new CounterCreationData(
            counterName,
            "",
            PerformanceCounterType.AverageCount64),
        new CounterCreationData(
            counterName + " base",
            "",
            PerformanceCounterType.AverageBase)
    }));

// obtain the writable counter objects
PerformanceCounter counter = new PerformanceCounter(
    categoryName,
    counterName,
    false);
PerformanceCounter baseCounter = new PerformanceCounter(
    categoryName,
    counterName + " base",
    false);

// increment the counter every 100 milliseconds
Console.WriteLine("Starting");
Random random = new Random();

while (true)
{
    counter.IncrementBy(50 + random.Next(-10, 10));
    baseCounter.Increment();
    Thread.Sleep(100);
}

AverageTimer32

Measures TIME, in seconds: “the time it takes, on average, to complete a process or operation.”

This chart shows average operation time, in milliseconds. The counter actually measures the time in seconds. We multiplied it by x1000 in Performance Monitor, so that the 50 milliseconds value would show as 50.

// create the performance category and counters
PerformanceCounterCategory.Create(
    categoryName,
    "",
    PerformanceCounterCategoryType.SingleInstance,
    new CounterCreationDataCollection(new[]
    {
        new CounterCreationData(
            counterName,
            "",
            PerformanceCounterType.AverageTimer32),
        new CounterCreationData(
            counterName + " base",
            "",
            PerformanceCounterType.AverageBase)
    }));

// obtain the writable counter objects
PerformanceCounter counter = new PerformanceCounter(
    categoryName,
    counterName,
    false);
PerformanceCounter baseCounter = new PerformanceCounter(
    categoryName,
    counterName + " base",
    false);

// do a 50 msec operation every 100 msec, and measure the time
Console.WriteLine("Starting");
Random random = new Random();

while (true)
{
    // simulate an operation taking ~50 msec
    Stopwatch stopwatch = Stopwatch.StartNew();
    Thread.Sleep(50 + random.Next(-10, 10));
    stopwatch.Stop();

    // update the counter and base with the number of ticks
    // the operation took
    counter.IncrementBy(stopwatch.ElapsedTicks);
    baseCounter.Increment();

    Thread.Sleep(100);
}

CountPerTimeInterval32

Measures the TIME PERCENTAGE an operation takes out of a larger INTERVAL. The MSDN documentation appears to be somewhat misleading as to this counter type.

// create the performance category and counters
PerformanceCounterCategory.Create(
    categoryName,
    "",
    PerformanceCounterCategoryType.SingleInstance,
    new CounterCreationDataCollection(new[]
    {
        new CounterCreationData(
            counterName,
            "",
            PerformanceCounterType.CountPerTimeInterval32)
    }));

// obtain the writable counter objects
PerformanceCounter counter = new PerformanceCounter(
    categoryName,
    counterName,
    false);

// do a 100 msec operation every 250 msec, and measure the time
Console.WriteLine("Starting");
Random random = new Random();

while (true)
{
    Stopwatch stopwatch = Stopwatch.StartNew();
    Thread.Sleep(100);
    stopwatch.Stop();

    counter.IncrementBy(stopwatch.ElapsedTicks);

    Thread.Sleep(150);
}

RateOfCountsPerSecond32

Measures OPERATIONS per SECOND: “shows the average number of operations completed during each second.”

// create the performance category and counters
PerformanceCounterCategory.Create(
    categoryName,
    "",
    PerformanceCounterCategoryType.SingleInstance,
    new CounterCreationDataCollection(new[]
    {
        new CounterCreationData(
            counterName,
            "",
            PerformanceCounterType.RateOfCountsPerSecond32)
    }));

// obtain the writable counter objects
PerformanceCounter counter = new PerformanceCounter(
    categoryName,
    counterName,
    false);

// increment the counter every 100 milliseconds
Console.WriteLine("Starting");
Random random = new Random();

while (true)
{
    counter.IncrementBy(5);

    Thread.Sleep(100);
}

Reference

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

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

%d bloggers like this: