Inaccurate performance counter timer values in Windows Performance Monitor
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 Stopwa开发者_如何学编程tch'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?
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.
精彩评论