开发者

Is a destructor the right place to mark the end of a script's execution?

开发者 https://www.devze.com 2022-12-18 08:09 出处:网络
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 i

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_e开发者_运维百科ncode(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);
    }
}


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消