How do I use PerformanceCounterType AverageTimer32?

Posted by Patrick J Collins on Stack Overflow See other posts from Stack Overflow or by Patrick J Collins
Published on 2009-08-03T08:46:46Z Indexed on 2010/05/06 3:18 UTC
Read the original article Hit count: 605

Filed under:

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);

// Count execution time
PerformanceCounter perf2 = new PerformanceCounter("Cat", "CallTime", "Instance", false);
perf2.IncrementBy(Environment.TickCount - init_call_time);

// Average base for execution time
PerformanceCounter perf3 = new PerformanceCounter("Cat", "CallTimeBase", "Instance",     false);


© Stack Overflow or respective owner

Related posts about c#

Related posts about Performance