Sgoettschkes
Sgoettschkes

Reputation: 13189

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. 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?

Upvotes: 6

Views: 7413

Answers (2)

luchaninov
luchaninov

Reputation: 6786

Since PHP 5.4 we can do microtime(true) - $_SERVER['REQUEST_TIME_FLOAT']

How to use in 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:

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

Upvotes: 3

Sgoettschkes
Sgoettschkes

Reputation: 13189

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

}

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

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.

Upvotes: 8

Related Questions