We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

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.4k
Accepted
answer

See this thread https://forum.phalcon.io/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);


5.4k

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 https://forum.phalcon.io/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);

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