views:

1064

answers:

6

When considering using performance counters as my companies' .NET based site, I was wondering how big the overhead is of using them.

Do I want to have my site continuously update it's counters or am I better off to only do when I measure?

A: 

The thing that I have found is that it is not that slow for the majoirty of applications. I wouldn't put one in a tight loop, or something that is called thousands of times a second.

Secondly, I found that programmatically creating the performance counters is very slow, so make sure that you create them before hand and not in code.

Paul Kinlan.

Kinlan
+6  A: 

The performance impact is negligible in updating. Microsoft's intent is that you always write to the performance counters. It's the monitoring of (or capturing of) those performance counters that will cause a degradation of performance. So, only when you use something like perfmon to capture the data.

In effect, the performance counter objects will have the effect of only "doing it when you measure."

dpurrington
Thanks. Can you by any chance point me to some numbers? I'm curious as to how good they got it...
Boaz
I've never seen it demonstrated, but I have no reason to doubt it. It would be simple prove in an application.
dpurrington
Hi dpurrington. By "it's the monitoring ... that will cause a degradation of performance" do you mean the degradation will be seen in the process that updates/publishes the counters or by the process that is monitoring them?
urig
The former. . . .
dpurrington
+1  A: 

I agree with famoushamsandwich (Mmmmm...ham sandwich), but would add that as long as your sampling rate is reasonable (5 seconds or more) and you monitor a reasonable set of counters, then the impact of measuring is negligible as well (in most cases).

Patrick Cuff
+5  A: 

A performance counter is just a pointer to 4/8 bytes in shared memory (aka memory mapped file), so their cost is very similar to that of accessing an int/long variabile.

Giulio Vian
+4  A: 

The overhead of setting up the performance counters is generally not high enough to worry about (setting up a shared memory region and some .NET objects, along with CLR overhead because the CLR actually does the management for you). Here I'm referring to classes like PerformanceCounter.

The overhead of registering the perfromance counters can be decently slow, but generally is not a concern because it is intended to happen once at setup time because you want to change machine-wide state. It will be dwarfed by any copying that you do. It's not generally something you want to do at runtime. Here I'm referring to PerformanceCounterInstaller.

The overhead of updating a performance counter generally comes down to the cost of performing an Interlocked operation on the shared memory. This is slower than normal memory access but is a processor primitive (that's how it gets atomic operations across the entire memory subsystem including caches). Generally this cost is not high to worry about. It could be 10 times a normal memory operation, potentially worse depending on the update and what contention is like across threads and CPUs. But consider this, it's literally impossible to do any better than interlocked operations for cross-process communication with atomic updates, and no locks are held. Here I refer to PerformanceCounter.Increment and similar methods.

The overhead of reading a performance counter is generally a read from shared memory. As others have said, you want to sample on a reasonable period (just like any other sampling) but just think of PerfMon and try to keep the sampling on a human scale (think seconds instead of milliseconds) and you proably won't have any problems.

Finally, an appeal to experience: Performance counters are so lightweight that they are used everywhere in Windows, from the kernel to drivers to user applications. Microsoft relies on them internally.

Advice: The real question with performance counters is the learning curve in understanding (which is moderate) and one measuring the right things (seems easy but often you get it wrong).

EWizard
A: 

I've tested these a LOT.

On an old compaq 1Ghz 1 processor machine, I was able to create about 10,000 counters and monitor them remotely for about 20% CPU usage. These aren't custom counters, just checking CPU or whatever.

Basically, you can monitor all the counters on any decent newer machine with very little impact.

The instantiation of the object can take a long time tho, a few seconds to a few minutes. I suggest you multithread this for all the counters you collect otherwise your app will sit there forever creating these objects. Not sure what MS does once you create it that takes so long, but you can do it for 1000 counters with 1000 threads in the same time you can do it for 1 counter and 1 thread.

Eric