简体   繁体   English

symfony2 - 获取执行时间

[英]symfony2 - Get execution time

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. 我想使用symfony2为我的应用程序创建一个状态页面,我想打印特定请求的执行时间(以及其他数据)。 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? 有没有办法尽可能接近脚本的总执行时间?

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: 我在web文件夹中创建了一个新文件performance.php,如下所示:

<?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: 我还注册了一个twig扩展,它使用全局并计算经过的时间:

<?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";
    }

}

When we want to do some performance measurements, we can simply use performance.php. 当我们想要进行一些性能测量时,我们可以简单地使用performance.php。 The template calls the function and can then display the execution time: 模板调用该函数,然后可以显示执行时间:

{{ performance_exectime() }}

It outputs 0 if the start time is not set (eg when the normal app.php is used), so it's safe to use in any case. 如果未设置开始时间,则输出0(例如,当使用正常的app.php时),因此在任何情况下都可以安全使用。 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. 另一方面,如果有人决定使用performance.php作为入口点,它不应该破坏任何东西,因为只有一个全局变量是不同的。

Since PHP 5.4 we can do microtime(true) - $_SERVER['REQUEST_TIME_FLOAT'] 从PHP 5.4开始我们可以做microtime(true) - $_SERVER['REQUEST_TIME_FLOAT']

How to use in Symfony2: 如何在Symfony2中使用:

src/AppBundle/Twig/AppExtension.php

<?php

namespace AppBundle\Twig;

class AppExtension extends \Twig_Extension
{
    public function getFunctions()
    {
        return [
            new \Twig_SimpleFunction('request_time', [$this, 'requestTime'], ['is_safe' => ['html']]),
        ];
    }

    public function requestTime($decimals = 3)
    {
        return number_format(microtime(true) - $_SERVER['REQUEST_TIME_FLOAT'], $decimals);
    }

    public function getName()
    {
        return 'app_extension';
    }
}

in view: 在视图中:

<footer class="footer">
    <div class="container">
        <p class="text-muted">{{ request_time() }}s</p>
    </div>
</footer>

in app/config/services.yml : app/config/services.yml

services:
    app.twig_extension:
        class: AppBundle\Twig\AppExtension
        public: false
        tags:
            - { name: twig.extension }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM