In our application, we are using the Windows Performance Counters to store some of our application metrics, which are later retrieved in some web services.
I'm having an issue with the amount of time it takes to read the value from the counters. I've looked through the rest of my app and everything is fine, performance wise, but reading from the counters within a loop (from List or array) takes a painful amount of time.
Example code:
// This triggers a read of the counter's initial value (1000ms delay following for calculated counters)
counters.ToList().ForEach(counter => counter.NextValue());
In my testing of just the loop above, a list of 1,359 counters takes 20 seconds, and with a stopwatch in place, it seems the average time to read a counter value is either 0-10ms, or about 80-90ms. A lot of them take 0ms, the highest being about 170ms, with the average non-zero being about 80-90ms.
Maybe I am being too optimistic, but I would have figured that reading 1,000 numeric values should take only a few milliseconds. Is there a lot more processing going on here than I'm aware of?
I actually have another loop later in my logic that gets a second value for calculated counters. That just makes this doubly worse. :)
Thanks!
Update 1
I wrapped the counter retrieval in a stopwatch and I'm surprised by the results. Reading even the simple property of .RawValue
still takes an excessive amount of time. It's my understanding that counters all basically work the same, and retrieval should be incredibly fast; strangely I'm also seeing a pattern where counters for Networking categories take longer.
According to http://joe.blog.freemansoft.com/2014/03/windows-performance-counters.html, performance for the Performance Counter service shouldn't even be a consideration.
I've posted some stopwatch results to the following pastebin: http://pastebin.com/raw.php?i=aDJk2Tru
My code is as follows:
Stopwatch t;
foreach (var c in counters)
{
t = Stopwatch.StartNew();
var r = c.RawValue;
Debug.WriteLine(t.ElapsedMilliseconds.ToString("000") + " - " + c.CategoryName + ":" + c.CounterName + "(" + c.CounterType + ") = " + r);
}
As you can see in the paste, a lot of reads are 0, but there are a lot in the 50-100ms range. I don't really understand how that can be. Surely one counter value should be just as fast as any other, right?
foreach(var counter in counters.ToList()) { counter.NextValue(); }
PerformanceCounter.NextValue()
is not entirely trivial. Are you reading performance counter data from the local computer, or a remote computer?