In our application, we use Windows performance counters to store some of our application metrics, which are subsequently retrieved in some web services.
I have a problem with the amount of time it takes to read a value from counters. I looked through the rest of my application and everything is fine, the performance is wise, but reading from counters in a loop (from a list or array) takes a lot of time.
Code example:
In my testing of only the cycle above, the list of 1,359 counters takes 20 seconds, and with a stopwatch in place, it seems that the average time to read the counter is 0-10 ms or about 80-90 ms. Many of them take 0 ms, the highest - about 170 ms, while the average non-zero is about 80-90 ms.
I may be too optimistic, but I would have thought that reading 1000 numerical values ββshould take only a few milliseconds. Is there more processing than I know?
In fact, I have another loop in my logic that gets the second value for the counters counted. It just makes it doubly worse. :)
Thanks!
Update 1
I wrapped the counter in the stopwatch and I am surprised by the results. Reading even a simple .RawValue property still takes too much time. I understand that counters basically work the same way, and the search should be incredibly fast; weird. I also see a template in which 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 should also not be considered.
I posted some stopwatch results for 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, many readings are equal to 0, but in the range of 50-100 m. There are many. I really donβt understand how this can be. Of course, one counter value should be as fast as any other, right?