11

I want to show the users how long the page takes to fully load in the footer of my website.

How do I go about doing this? I assume there is a function that can be used for this?

Not sure what language this type of feature is developed in?

Any help would be appreciated, thanks.

sark9012
  • 5,485
  • 18
  • 61
  • 99
  • 1
    possible duplicate of [PHP Get Page Load Stats - How to measure php script execution / load time](http://stackoverflow.com/questions/4412575/php-get-page-load-stats-how-to-measure-php-script-execution-load-time) – spinsch Aug 10 '14 at 17:23
  • 2
    Do you think any your users would look at it, or even care? I'm not trying to be facetious, but I doubt any of them would even notice. – The Blue Dog Aug 10 '14 at 17:34
  • 2
    I'm trying to do it for my own testing means! I can't find a decent service that allows me to test the page loading time across several devices, I'm playing around with image optimisation. – sark9012 Aug 10 '14 at 17:54
  • Sorry, Luke, it's just that you said, _"I want to show the users..._ I wasn't trying to be rude. :) – The Blue Dog Aug 10 '14 at 18:05
  • If you are not sure what language your customer use then the best solution is using javascript because its support almost all major browser. Please check this link https://stackoverflow.com/questions/14341156/calculating-page-load-time-in-javascript – Marwan Salim Oct 15 '18 at 09:20

4 Answers4

31

You may try like this:

$starttime = microtime(true); // Top of page
// Code
$endtime = microtime(true); // Bottom of page

printf("Page loaded in %f seconds", $endtime - $starttime );

As commented by Ed Heal you need to use JavaScript as network/proxy/routes need to be factored in.

Also you may try this approach as well:

From the source

Put the following code at the very top of your PHP page (if you measure the time needed for particular part of the code put this right before that PHP code part)

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

The following code has to be put at the very end of the web page (or the end of the PHP code part)

<?php
$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.';
?>
Chuck Le Butt
  • 47,570
  • 62
  • 203
  • 289
Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
  • 2
    You need to use JavaScript as network/proxy/routes need to be factored in – Ed Heal Aug 10 '14 at 17:25
  • @EdHeal:- +1 Yes thats true. Should I include that in my answer? – Rahul Tripathi Aug 10 '14 at 17:26
  • Would this answer factor in the entire page load? Including jquery libraries, etc? – sark9012 Aug 10 '14 at 17:30
  • @R.T. Yes - as that my be the largest factor. – Ed Heal Aug 10 '14 at 17:30
  • @EdHeal: Except that the start time would only apply from when the JS script had already loaded. ;) – The Blue Dog Aug 10 '14 at 17:36
  • @TheBlueDog - If you can assume that the two machines have there clocks in sync (that is a big assumption), you could punch in using PHP (or whatever your poison) into the page a time stamp when the request was started being processed. But does it really matter if you are out by a second or two – Ed Heal Aug 10 '14 at 17:43
  • @EdHeal: _does it really matter if you are out by a second or two.._ Hence the wink. – The Blue Dog Aug 10 '14 at 17:44
  • @TheBlueDog - A guess what is trying to be captured is where the delays occur. Usage of database or usage of network – Ed Heal Aug 10 '14 at 17:46
  • 1
    @EdHeal: This is one hell of a discussion for something that the average user won't even look at. :D Nice pooch, by the way. – The Blue Dog Aug 10 '14 at 17:55
  • I'm trying to do it for my own testing means! I can't find a decent service that allows me to test the page loading time across several devices, I'm playing around with image optimisation – sark9012 Aug 10 '14 at 18:00
  • 2
    @TheBlueDog - he is usually nice. But not today. He nicked my breakfast! Anyway I think the numbers are required not for the user but by the website owner – Ed Heal Aug 10 '14 at 18:00
  • 1
    @EdHeal: It was _his_ breakfast, you just thought it was yours. ;) – The Blue Dog Aug 10 '14 at 18:06
  • *Not for users, but for site development ² (i sayd that as soon as i read... – David Augustus Nov 19 '21 at 22:49
  • I just did not understood what do u mean with "use JavaScript as network/proxy/routes need to be factored in" .. – David Augustus Nov 19 '21 at 22:50
3

2 simple steps to show load time on your page:

1. Put this code at beginning of your page:

 <?php $start_time = microtime(true); ?>

2. Put this code at the end of your page:

This page was generated in <?php echo(number_format(microtime(true) - $start_time, 2)); ?> seconds.
Marwan Salim
  • 682
  • 7
  • 14
1

You need two times: the starttime and the endtime. Using JavaScript the starttime can only be approximated because the script will only fire when it is loaded, not before. This means that you will not measure stuff like DNS looking, initial latency and initial downloading. If you're fine with this limitation do this:

  1. In the head of the page determine the current time using an inline script
  2. On the firing of the onload determine the current time again
  3. subtract the first time from the second and then you have the total time it took to load the page

Another caveat: Stuff that gets loaded via ajax is not measured here either. The onload fires before the ajax stuff runs.

Koen Peters
  • 12,798
  • 6
  • 36
  • 59
1

The other answers are correct, there are two methods depending on the version of PHP in use PHP has introduced $_SERVER['REQUEST_TIME_FLOAT'].

To embed a formatted PageGen time use the following;

PHP7.3

<div align="center"><span style="font-family: Verdana, Arial, Helvetica, sans-serif; font-size:8px; color #333333;">(PageGen <?
$tt = number_format(rtrim(sprintf("%.20f", (microtime(true) - $_SERVER['REQUEST_TIME_FLOAT'])), "0"),6,'.',',');
if (strpos(($tt."0"), "0") != 0) {
  $tt = number_format($tt,2,'.',',');
}
echo ($tt);
?> Seconds)</span></div>

On some of my servers this did not work on PHP7.4 and it did not work on PHP4 versions, but it is in the documentation.

Appendix:
https://php.willtech.net.au/manual/en/function.microtime
https://php.willtech.net.au/reserved.variables.server

Willtech
  • 111
  • 5