析构函数是标记脚本执行结束的正确位置吗?

发布于 2024-08-19 20:06:33 字数 1458 浏览 4 评论 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);
    }
}

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

风柔一江水 2024-08-26 20:06:33

你需要做的是使用Xdebug。一旦设置完毕,就非常简单,无需更改代码的任何内容即可完全了解所花费的时间。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文