Timing a function call.

K

Kevin Burton

I am stumpped. I am trying to use the PerformanceCounter
class to time a function and I am having no luck. If I
stick to the rather simple NumberOfItems32 then everything
works just fine. But if I deviate from that type I don't
seem to understand.

I am installing the performance counters as:

installer.Counters.Add(new CounterCreationData("Calls",
"Number
of calls.",

PerformanceCounterType.NumberOfItems32));
installer.Counters.Add(new CounterCreationData("Errors",
"Number of
errors in processing.",

PerformanceCounterType.NumberOfItems32));
installer.Counters.Add(new CounterCreationData("Duration",
"Duration
of processing.",

PerformanceCounterType.CounterDelta64));


At runtime I instantiate the Performance counter like:

calls = new PerformanceCounter("PPC
VRU", "Calls", "_Total", false);
errors = new PerformanceCounter("PPC
VRU", "Errors", "_Total", false);
duration = new PerformanceCounter("PPC
VRU", "Duration", "_Total", false);

(yes the category exits)

I have a separte class to do the timeing but when the
timing is done I log it to the performance counter as:

duration.IncrementBy((long)timer.ElapsedMilliseconds());

I have printed out the return from the timer function and
it is not 0 yet PerfMon shows a steady zero. What am I
doing wrong? Like I said the other calls work just fine.

Thank you for your suggestions.

Kevin
 
J

John Saunders

Kevin Burton said:
I am stumpped. I am trying to use the PerformanceCounter
class to time a function ....
... What am I
doing wrong?

Unless there's a lot more to your requirement than "timing a function", then
the first thing you're doing wrong is to use the PerformanceCounter class to
time the function. :)

So ok, I'm sure there's some reason why you can't use:

DateTime startTime = DateTime.Now;
// Call function
DateTiime endTime = DateTime.Now;
TimeSpan duration = endTime - startTime;

Perhaps if you'll tell us why the above isn't adequate, we'd be better able
to help you.
 
K

Kevin Burton

The easiest way to expose and monitor the duration of a function call is
with PerfMon. If I always had access to a debugger or could write to the
console the results of timing such as you suggest then it would be simple
and the PerformanceCounter class would not be necessary. I want a
convienient way to monitor and time function calls and exposing the timing
to PerfMon seems to be the answer.

Kevin
 
D

Dave

You have to install the performance counters with an installation program.
There's an installer component class
(System.Configuration.Install.Installer) that you need to add to your
program, and then you need to create an installation program (msi file) and
add a custom action to the msi that will invoke the installer class. You can
do it programmatically yourself, emulating what the msi file does, but it is
more then simply adding a category to the install class as you have shown
below.

Also, after installing the category, if you have made any changes at all to
the category, such as adding or removing counters, you must first uninstall,
then reinstall, the category. Otherwise the counter values will appear to be
zero in perfmon.

You should be able to google up links to samples on this.
 
D

Dave

I used the same method, IncrementBy, in timing my methods, but I only used
them with NumberOfItems32 type counters. It's been a long time since I've
looked at that code so my memory is hazy, but I vaguely remember having
similar problems with other counter types. I do know that once you make a
change to any counter you must first uninstall the old and then reinstall
the new counter definitions, otherwise it can hose up your system.
 
K

Kevin Burton

I have come up with a work-around. If I specify the
counter type as NumberOfItems(32|64) and I set the
RawValue as the duration of the function call it seems to
work. I see in PerfMon each time the duration changes.
This is not ideal as it feels like a hack but it does
allow me to move forward.

Kevin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top