views:

33

answers:

1

I'm doing some broad performance investigation in an application I maintain and I've set up a simple solution to track the execution time of requests, but I'm unable to find information to verify if this is going to be satisfyingly accurate.

This appears to have netted some good information and I've already eliminated some performance issues as a result, but I'm also seeing some confusing entries that make me question the accuracy of the recorded execution time.

Should I add an explicit method call at the end of each calling script to mark the end of its execution or is this (rather tidy) approach using the destructor good enough?

The calling code at the top of the requested script:

if( file_exists('../../../bench') )
{
    require('../../../includes/classes/Bench.php');
    $Bench = new Bench;
}

And here is the class definition (reduced for clarity):

require_once('Class.php');

class Bench extends Class
{
    protected $page_log = 'bench-pages.log';
    protected $page_time_end;
    protected $page_time_start;

    public function __construct()
    {
        $this->set_page_time_start();
    }

    public function __destruct()
    {
        $this->set_page_time_end();
        $this->add_page_record();
    }

    public function add_page_record()
    {
        $line = $this->page_time_end - $this->page_time_start.','.
                base64_encode(serialize($_SERVER)).','.
                base64_encode(serialize($_GET)).','.
                base64_encode(serialize($_POST)).','.
                base64_encode(serialize($_SESSION))."\n";

        $fh = fopen( APP_ROOT . '/' . $this->page_log, 'a' );
        fwrite( $fh, $line );
    }

    public function set_page_time_end()
    {
        $this->page_time_end = microtime(true);
    }

    public function set_page_time_start()
    {
        $this->page_time_start = microtime(true);
    }
}
+1  A: 

What you need to do is use Xdebug. It is very simple once you set it up and don't have to change anything about your code to get full coverage of what took how long.

Eric Butera
Thanks for checking in, Eric, but I specifically avoided asking for profile strategy suggestions (that's sufficiently covered elsewhere). My question is about the timing of PHP unloading the object with relation to the request being served. (For the record, we already use Xdebug.)
Trevor Bramble