views:

74

answers:

2

Dear Gurus,

All of you agree with the value of using Performance Counters for server applications.

I would like to know how to implement these using C#.

Usually performance counters have the following attributes:

  1. They are shared/global
  2. Writing requires locks to ensure synchronization
  3. Reading sometimes requires locks too

Is it better to update them asynchronously? What is the best way to make them asynchronous?

I am planning to use the ThreadPool.QueueUserWorkItem() function. What is your opinion about this?

If my questions seem a bit vague, please look at the HelloWorld WCF service.

I also want to know the following:

  1. How many times it's being hit overall and within a certain period
  2. Average/min/max response times overall and within a certain period

If any one knows of any specialized ways to do this in .NET or WCF, please let me know.

+1  A: 

You could use the standard windows performance counters.

This functionality can be accessed from c# using the system.diagnostics.performancecounter namespace. For a C# example see:

http://msdn.microsoft.com/en-us/library/system.diagnostics.performancecounter.aspx

Shiraz Bhaiji
+2  A: 

If you use the .NET classes for that purpose there is no need to manually lock the increment, decrement and query of a performance counter - the framework will do that for you.

If you need to use performance counters in native code you still should rather use InterlockedIncrement() and friends.

I think updating performance counters asynchronously is not such a good idea, but your milage may vary. It could be considered less usefull, if the data about some interesting situation arrives after the fact. In all cases I would not put pressure on the ThreadPool just to update a performance counter. Now, if you need to do the work that triggers the counter in a thread anyway, that would be something different of course.

Generally, I don't think that the updating of a performance counter is really a bottleneck, but rather the gathering of the data that is used to update the counter.

For example, the .NET GC Memory performance counters are only update when a GC actually happens, because tracking the information (in the background) just to update the counters everytime something chances would be too expensive (sorry, no reference here, but there are a coupble of MS blog entries about that particular subject).

Finally, be adviced that WCF already provides a rather large number of counters out of the box, which might cover all you want to now already.

Christian.K