Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

Is there an easy way to get running time of a page?

As title, I want to show the running time/memory usage of a page in the footer area.

Is there an easy way to do this?



6.3k
Accepted
answer

See this thread http://forum.phalconphp.com/discussion/6343/get-response-time or:

<?php
printf('%.2f ms', (microtime(TRUE) - $_SERVER['REQUEST_TIME_FLOAT']) * 1000);
printf('%.2f MiB', memory_get_peak_usage() / 1024 / 1024);


3.0k

Thanks for your reply. But, where should I put these code (footer area)?

Phalcon is a complcated framework, I don't think it's a good idea to put these code in the [view template]. Because when this code was executed, the page might not be finished.

See this thread http://forum.phalconphp.com/discussion/6343/get-response-time or:

```php <?php printf('%.2f ms', (microtime(TRUE) - $SERVER['REQUESTTIMEFLOAT']) * 1000); printf('%.2f MiB', memorygetpeakusage() / 1024 / 1024); ```

The view is the last thing to be executed, so puting this in your view is probably the best place. You might not get right-to-the-microsecond accuracy, but it should be good enough.

You can use https://php.net/register%5shutdown%5function or subscribe to Phalcon\Mvc\Application application:beforeSendResponse event and modify your HTML, something like:

<?php

$di['eventsManager']->attach(
    'application:beforeSendResponse',
    function (\Phalcon\Events\Event $event, Phalcon\Mvc\Application $app, Phalcon\Http\Response $response) {
        $profiles = 'your html';
        // Remove HTML, also preg_replace('/<\/html>\s*$/', ...)
        $response->setContent(substr(trim($response->getContent()), 0, -7) . $profiles . '</html>');
    }
);


44.8k

You could put it in the view and then also log the finish time at the very end of the bootstrap. Then just compare the differences and see if it matters at all and then in the future you could just mentally add the extra time to the time on the view.