Page 1 of 1

[solved] measure load time of a webpage

Posted: Thu Sep 22, 2005 4:46 am
by damithc
Newbie here :oops: ,
I'm trying to to calculate the load time of one php page (say aaa.php) using another php page (say bbb.php). Anybody can give me a tip on how to do this?

Posted: Thu Sep 22, 2005 5:10 am
by CoderGoblin

Posted: Thu Sep 22, 2005 5:18 am
by damithc
thx for the reply coderGoblin,
microtime() does solve my problem, but only partly.

here's the (pseudo)code for what i intend to do.

$starttime=microtime();
load external page
$endtime=microtime();

$loadtime=$starttime-$endtime;

it's the load external page part that i don't know how to do. any suggestions?

Posted: Thu Sep 22, 2005 5:27 am
by CoderGoblin
To include the other page use the include() function.

If using php4 you need the function given in the example (full code below).

Code: Select all

<?php
/**
 * Simple function to replicate PHP 5 behaviour
 */
function microtime_float()
{
   list($usec, $sec) = explode(" ", microtime());
   return ((float)$usec + (float)$sec);
}

$time_start = microtime_float();

// Process Subpage
include ("bbb.php");

$time_end = microtime_float();
$time = $time_end - $time_start;

echo "bbb.php processed in $time seconds\n";
?>

Posted: Thu Sep 22, 2005 5:40 am
by damithc
almost there. thx a lot CoderGoblin for your continuous help. :)
in my case, i don't want to include contents of bbb.php in my aaa.php. Rather, aaa.php should only display the load time of bbb.php.
i tried it anyway, and hit upon another probem. When i include aaa.php inside bbb.php, aaa.php complains that HTTP_USER_AGENT is not defined.
:oops:

Posted: Thu Sep 22, 2005 6:10 am
by CoderGoblin
Not sure about the error (would potentially need to look at your code) but In this case I would be tempted to use a session variable. (OK I know whole different subject)

bbb.php

Code: Select all

<?php
session_start();
$time_start = microtime();

//Do all your processing;

$time_end = microtime();
$time = $time_end - $time_start;
if (isset($_SESSION['timings']['bbb'])) {
  $_SESSION['timings']['bbb'][]="processed in $time seconds\n"; 
} else {
  $_SESSION['timings']['bbb'][0]="processed in $time seconds\n"; 
}
?>
aaa.php

Code: Select all

<?php
session_start();
if (isset($_SESSION['timings']) {
  foreach ($_SESSION['timings'] as $page=>$values) {
     foreach ($values as $index=>$timestr) {
         echo("Page $page Run ".($index+1)." $timestr<br />");
     }
     echo('<hr />');
  }
} else {
  echo("No timing details");
}
?>

Or something similar (PHP4 needs that microtime function (possibly in an include). Hope that helps. (Note I have placed the values in the arrays so aaa.php show all runs of anytimed files, not just one.)

Posted: Thu Sep 22, 2005 6:30 am
by damithc
that did the trick! thx a million. you made my day :D

fixed two minor errors:

Code: Select all

sesion_start();
should be session_start(); in both cases

Code: Select all

if (isset($_SESSION['bbb'])) {...
should be if (isset($_SESSION['timings']['bbb'])) {

Posted: Thu Sep 22, 2005 6:49 am
by CoderGoblin
Glad to be of help. Have edited the original post with the changes in case anyone else wants it.