How do I use PerformanceCounterType AverageTimer32?
- by Patrick J Collins
I'm trying to measure the time it takes to execute a piece of code on my production server. I'd like to monitor this information in real time, so I decided to give Performance Analyser a whizz. I understand from MSDN that I need to create both an AverageTimer32 and an AverageBase performance counter, which I duly have. I increment the counter in my program, and I can see the CallCount go up and down, but the AverageTime is always zero. What am I doing wrong? Thanks!
Here's a snippit of code :
long init_call_time = Environment.TickCount;
// ***
// Lots and lots of code...
// ***
// Count number of calls
PerformanceCounter perf = new PerformanceCounter("Cat", "CallCount", "Instance", false);
perf.Increment();
perf.Close();
// Count execution time
PerformanceCounter perf2 = new PerformanceCounter("Cat", "CallTime", "Instance", false);
perf2.NextValue();
perf2.IncrementBy(Environment.TickCount - init_call_time);
perf2.Close();
// Average base for execution time
PerformanceCounter perf3 = new PerformanceCounter("Cat", "CallTimeBase", "Instance", false);
perf3.Increment();
perf3.Close();
perf2.NextValue();