I'm looking for a way to implement a generic Timing class as a decorator or come up with a solution for timing code execution without cluttering the code.

In my web app, there is a perceived need to measure the execution time of many function calls and database queries using ZFDebug and its timing plugin.


An initial crack at this resulted in lots and lots of timing code interspersed with real code, which made modification and debugging a bit nightmarish.

As a solution, I wrote a very bare-bones Timing class, a decorator that would encapsulate an object, intercept calls to the object via the __call method, start a timer, pass the call through to the encapsulated object, then stop the timer.

My interface went like this:

$obj = new Thing();
Timer::time( $obj ); // $obj becomes a Timer obj with $obj inside it
$obj->thingMethod(); // Timer passes call to Timer->obj

This worked great (and if anyone's interested I can pass on the code) -- my timers worked, and the code originally written for $obj didn't even notice -- except in cases where a function used type hinting and I got Unknown errors. I'm using Zend Framework, and (for instance) I was curious about the timing on my Initializer (a controller plugin), so I thought the following would do the trick:

$initializer = new Initializer($env);
Timer::time( $initializer );
$frontController->registerPlugin( $initializer );

But machinery deep in the bowels of Zend does type hinting for a Zend_Controller_Plugin_Abstract object, not a Timer, and so an error is raised.

Whole point is that I want to be able to use this Timer class on anything. Seems like I should be able to if it weren't for the type hinting.

Is there any way to fool a function into thinking my Timer is the same class as Timer->obj? Is there another way to cleanly wrap method calls for timing/logging purposes? Any other good patterns for measuring code execution time without cluttering the code?

I found this discussion from last December, asking a very similar question, but it seemed unresolved:

<http://www.mail-archive.com/php-general@lists.php.net/msg238889.html>

--
David Kurtz
dku...@technorati.com




--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to