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

See this thread http://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);


4.7k

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