【问题标题】:symfony2 - Get execution timesymfony2 - 获取执行时间
【发布时间】:2012-04-29 23:40:39
【问题描述】:

我想使用 symfony2 为我的应用程序创建一个状态页面,我想在其中打印特定请求的执行时间(以及其他数据)。我无论如何都找不到这样做。

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

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

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

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

【问题讨论】:

  • 内置的分析系统呢?它将在 2.1 中拥有更多很棒的功能,例如详细的时间图,有点类似于 firebug 和 webkit 的图。
  • 我想在生产环境中的一个特殊页面上执行此操作。现在我不知道探查器是如何工作的。当我执行一项操作并且对所有其他操作没有性能影响时,我可以以某种方式“激活”它吗?
  • 要在生产服务器上进行分析,您可能需要查看 xhprof...

标签: php symfony profiling


【解决方案1】:

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

如何在 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';
    }
}

在视图中:

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

app/config/services.yml:

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

【讨论】:

    【解决方案2】:

    我找到了一种我认为适合我们用例的方法。我在 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';
    

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

    <?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。模板调用函数,然后可以显示执行时间:

    {{ performance_exectime() }}
    

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

    【讨论】:

    • 这是个好主意! (考虑把它变成一个 wiki 条目。)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-07-09
    • 2020-03-12
    • 2021-09-12
    • 1970-01-01
    • 1970-01-01
    • 2016-02-11
    • 1970-01-01
    相关资源
    最近更新 更多