Performance counters types with the .NET Framework
April 7, 2011 Leave a Comment
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




