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?
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:
I also registered a twig extension which uses the global and calculates the elapsed time:
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:
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.