views:

194

answers:

1

I am implementing instrumentation within an application and have encountered an issue where the value that is displayed in Windows Performance Monitor from a PerformanceCounter is incongruent with the value that is recorded.

I am using a Stopwatch to record the duration of a method execution, then first i record the total milliseconds as a double, and secondly i pass the Stopwatch's TimeSpan.Ticks to the PerformanceCounter to be recorded in the Performance Monitor.

Creating the Performance Counters in perfmon:

var datas = new CounterCreationDataCollection();
datas.Add(new CounterCreationData 
{
    CounterName = name, 
    CounterType = PerformanceCounterType.AverageTimer32
});

datas.Add(new CounterCreationData 
{
    CounterName = namebase, 
    CounterType = PerformanceCounterType.AverageBase
});

PerformanceCounterCategory.Create("Category", "performance data",
    PerformanceCounterCategoryType.SingleInstance, datas);

Then to record i retrieve a pre-initialized counter from a collection and increment:

_counters[counter].IncrementBy(timing);
_counters[counterbase].Increment();

...where "timing" is the Stopwatch's TimeSpan.Ticks value.

When this runs, the collection of double's, which are the milliseconds values for the Stopwatch's TimeSpan show one set of values, but what appears in PerfMon are a different set of values.

For example... two values recorded in the List of milliseconds are:

23322.675, 14230.614

And what appears in PerfMon graph are:

15.546, 9.930

Can someone explain this please?

+1  A: 

A few guesses.

You are using PerformanceCounterType.AverageTimer32. That's taking an average of the time. It is also possible that you are not reseting the Stopwatch on each method call so the values you are storing in the list are the total running time so far of every call to that method.

You said milliseconds for the list, but ticks for the performance counter. A tick is 100 nanoseconds which is 0.0001 milliseconds. I would have expected the sizes you got to be reversed such as perfmon getting 14230.614 and the list getting 15.546 but that would still be off by an order of magnitude.

Not much of an answer, but it didn't fit in a comment.

Mike Two
haha no it's not the Stopwatch :P
krisg
Just read this here : http://www.codeproject.com/KB/system/monitorserviceperformance.aspx which states that the AverageTimer32 generates a value in PerfMon by averaging just the last 2 values recorded. That's probably the cause of the issue with different values. As for the order of magnitude, i'm guessing that though you are passing PerfMon time in ticks, it's displaying it in a more "human friendly" format of seconds. That's just a guess though.
krisg
Marked as answer anyway as it provided me with at least some modicum of enlightenment ;)
krisg