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?


See this thread or:

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


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 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 or subscribe to Phalcon\Mvc\Application application:beforeSendResponse event and modify your HTML, something like:


    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>');


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.