symfony2 - 获取执行时间 [英] symfony2 - Get execution time

查看:55
本文介绍了symfony2 - 获取执行时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 symfony2 为我的应用程序制作一个状态页面,我想在其中打印特定请求的执行时间(以及其他数据).反正我找不到这样做.

I want to make a status page for my application using symfony2 where I want to print the execution time (along with other data) of the particular request. I could not find anyway to do this.

我知道我可以通过以下方式跟踪代码部分的执行时间:

I know that I can track the execution time of a code part with:

$starttime = microtime();
// do something
$duration = microtime() - $starttime;

但由于显而易见的原因,我不能将它放在控制器中,因为不会跟踪整个引导程序.也不包括渲染模板.

But for obvious reason I cannot place it in the controller, as the whole bootstrap would be not tracked. Also rendering the template would not be included.

有没有办法尽可能接近脚本的总执行时间?

Is there any way to get as near as possible to the total execution time of the script?

推荐答案

我找到了一种我认为适合我们用例的方法.我在 web 文件夹中创建了一个新文件 performance.php,如下所示:

I found a way which I think is ok for our use case. I created a new file performance.php in the web folder which looks like this:

<?php
/**
 * This file is only used for doing realtime performance measurement
 * Right now only the microtime is calculated, but in the future the
 * xhproof module could be used: http://de2.php.net/manual/en/book.xhprof.php
 *
 * MAKE SURE TO NOT USE THIS FILE IN PRODUCTION FOR OTHER STUFF THAN REAL TIME
 * PERFORMANCE MEASUREMENT
 */

$GLOBALS['PerformanceTwigExtensionMicrotime'] = microtime(true);

require_once __DIR__.'/app.php';

我还注册了一个使用全局并计算经过时间的树枝扩展:

I also registered a twig extension which uses the global and calculates the elapsed time:

<?php

namespace Acme\DemoBundle\Extension;

class PerformanceTwigExtension extends \Twig_Extension {

    public function getFunctions() {
        return array(
            'performance_exectime' => new \Twig_Function_Method($this, 'getExecTime')
        );
    }

    public function getExecTime() {
        if (!isset($GLOBALS['PerformanceTwigExtensionMicrotime'])) {
            return 0;
        }

        $durationInMilliseconds = (microtime(true) - $GLOBALS['PerformanceTwigExtensionMicrotime']) * 1000;
        return number_format($durationInMilliseconds, 3, '.', '');
    }

    public function getName() {
        return "performance_extension";
    }

}

当我们想做一些性能测量时,我们可以简单地使用performance.php.模板调用函数,然后可以显示执行时间:

When we want to do some performance measurements, we can simply use performance.php. The template calls the function and can then display the execution time:

{{ performance_exectime() }}

如果没有设置开始时间(例如使用普通的app.php时),它会输出0,因此在任何情况下都可以安全使用.另一方面,如果有人决定使用 performance.php 作为入口点,它不应该破坏任何东西,因为只有一个全局变量不同.

It outputs 0 if the start time is not set (e.g. when the normal app.php is used), so it's safe to use in any case. On the other hand, if someone decides to use performance.php as an entry point, it shouldn't break anything as only one global variable is different.

这篇关于symfony2 - 获取执行时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆