Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Get Page Load Stats - How to measure php script execution / load time

What I have in the header:

$time = microtime();
$time = explode(' ', $time);
$time = $time[1] + $time[0];
$start = $time;

What I have in the footer:

$time = microtime();
$time = explode(' ', $time);
$time = $time[1] + $time[0];
$finish = $time;
$total_time = round(($finish - $start), 4);
echo 'Page generated in ' . $total_time . ' seconds.';

Output: Page generated in 1292008977.54 seconds.

Can someone please help me figure out why the result is not right?? I am using PHP5.

like image 490
ATLChris Avatar asked Dec 10 '10 19:12

ATLChris


1 Answers

Seeing how this is the first result in Google I thought I'd share my solution to this problem. Put this at the top of your page:

$startScriptTime=microtime(TRUE);

And then put this code at the bottom of your page:

$endScriptTime=microtime(TRUE);
$totalScriptTime=$endScriptTime-$startScriptTime;
echo "\n\r".'<!-- Load time: '.number_format($totalScriptTime, 4).' seconds -->';

When you view source of a page you can see the load time in a comment on the last line of your HTML.

like image 165
GhostInTheSecureShell Avatar answered Oct 14 '22 00:10

GhostInTheSecureShell