[solved] measure load time of a webpage

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
damithc
Forum Newbie
Posts: 22
Joined: Sat Apr 10, 2004 12:18 am
Location: singapore
Contact:

[solved] measure load time of a webpage

Post 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?
Last edited by damithc on Thu Sep 22, 2005 6:31 am, edited 1 time in total.
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post by CoderGoblin »

User avatar
damithc
Forum Newbie
Posts: 22
Joined: Sat Apr 10, 2004 12:18 am
Location: singapore
Contact:

Post 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?
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post 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";
?>
User avatar
damithc
Forum Newbie
Posts: 22
Joined: Sat Apr 10, 2004 12:18 am
Location: singapore
Contact:

Post 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:
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post 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.)
Last edited by CoderGoblin on Thu Sep 22, 2005 6:50 am, edited 2 times in total.
User avatar
damithc
Forum Newbie
Posts: 22
Joined: Sat Apr 10, 2004 12:18 am
Location: singapore
Contact:

Post 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'])) {
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post by CoderGoblin »

Glad to be of help. Have edited the original post with the changes in case anyone else wants it.
Post Reply