0

我正在我维护的应用程序中进行一些广泛的性能调查,并且我已经设置了一个简单的解决方案来跟踪请求的执行时间,但是我无法找到信息来验证这是否会令人满意地准确。

这似乎已经收集了一些很好的信息,因此我已经消除了一些性能问题,但我也看到了一些令人困惑的条目,这些条目让我质疑记录的执行时间的准确性。

我应该在每个调用脚本的末尾添加一个显式方法调用以标记其执行的结束,还是使用析构函数的这种(相当整洁的)方法足够好?

请求脚本顶部的调用代码:

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

这是类定义(为清楚起见减少了):

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

1 回答 1

1

您需要做的是使用Xdebug。设置后非常简单,无需更改代码的任何内容即可全面了解花费了多长时间。

于 2010-01-29T23:21:27.153 回答