AverageTimer32 Not Working

Jul 12, 2012 at 8:44 PM
Edited Jul 12, 2012 at 8:51 PM


I am having no success using the AverageTimer32 or AverageCounter64 counters. The counters (and instance names) are appearing in perfmon, but they are always reporting 0.

My code example is below. RequestsIn is working properly, but RequestTime is not.

Declaration of performance counters:

[PerformanceCounterCategory("Operations", PerformanceCounterCategoryType.MultiInstance, "")]
public enum OperationMetrics
  [PerformanceCounter("Requests In", "Incoming requests per second", PerformanceCounterType.RateOfCountsPerSecond32)]
  [PerformanceCounter("Request Time", "The average length of time (in milliseconds) taken to respond to a request", PerformanceCounterType.AverageTimer32)]

Usage of performance counters:

Stopwatch stopwatch = Stopwatch.StartNew();
OperationResponse response = Execute(peer);
PerformanceHelper.CreateCounterHelper<OperationMetrics>("Example").IncrementBy(OperationMetrics.RequestTime, stopwatch.ElapsedTicks);
Jul 25, 2012 at 1:23 PM



Sorry for responding late. Have you downloaded the code with the test project? In there you will find an example of the AverageTimer32, it is called "AverageTimer32Example" in the Enum "SingleInstance_PerformanceCounters". 

On the other hand, the "Execute" method is actually taking a couple of seconds? Since there might be a bug within Stopwatch the use the internal DateTime structure that might miss quite low measures.




Jul 25, 2012 at 5:46 PM

I had looked at the examples but had not run them on my machine. I did manage to find my issue however - I was attempting to use the same counter (and thus creating a new one) in multiple threads. When creating the counter in Thread B, it would be destroyed in Thread A, and so thread A would fail to report the result.

On a separate note, I noticed a small issue in your example project, namely that in MultiInstance you use stopwatch.ElapsedMilliseconds instead of stopwatch.ElapsedTicks.

Jul 25, 2012 at 6:10 PM

Thanks I will see to change it.