Patrick
Patrick

Reputation: 3172

How to get the symfony page load time?

I have a symfony 1.4.x application and would like to be able to display the page load time.

In the dev environment, the load time is displayed in the debug toolbar.

How do I get that page load time in the app/%APP_NAME%/templates/layout.php?

enter image description here Thanks

Upvotes: 1

Views: 1375

Answers (1)

Mariusz Sakowski
Mariusz Sakowski

Reputation: 3280

In general to measure execution time in php you use something like that:

$start = microtime(true /*get as float*/);

//some code goes here (i.e. your symfony request dispatching)

$end = microtime(true);
echo sprintf("some code executed for %.3f seconds", $end - $start);

The trick with the Symfony framework will be to inject your code in good place, my proposals are:

  • use the filter chain (if you are familiar with this mechanism). Just insert your filter before any other, record first $start timestamp, and compute the difference in your layout file
  • or IMHO uglier, but slightly more accurate - hack symfony front controller to record your $start timestamp before dispatch call

Of course computing difference of page load time in layout file has it's drawbacks, because it does not calculate rendering time of entire page, just to the moment layout is rendered (which is acceptable, as rendering layout is one of last task of request dispatching, BUT layout is not always rendered)

To resolve this you could use your filter at the end of request to output javascript, which will append page load time to document (pretty much what symfony does with it's debug toolbar)

Upvotes: 1

Related Questions