9, November 2009

Webdigity tutorials : Measure page load time (php class)

 
Webdigity webmaster forums
[ Home | Help | Search | Forum's Shop | Archive | Login | Register | Webmaster Directory ]
Tutorials @ webDigity
Measure page load time (php class)

Subscribe to our tutorials feed!

In this tutorial we will create a basic php class with witch we will see in how much time a page loads

First we need to create the class that will handle the page load time. Bellow is the code we will need for the class:

Code:
<?php
/**
 * A basic PHP Class to measure page load time
 * For support issues please refer to the webdigity forums :
 * www.webdigity.com
 *
 * ==============================================================================
 * 
 * @version $Id: pageloadtime.class.php,v 1 2007/09/25 13:54:32 $
 * @copyright Copyright (c) 2007 Nick Papanotas (http://www.webdigity.com)
 * @author Nick Papanotas <nikolas@webdigity.com>
 * @license http://opensource.org/licenses/gpl-license.php GNU General Public License (GPL)
 * 
 * ==============================================================================
 */

 class 
pageLoad{
 
 var $start;
 
 var $end;
 
 
 
 function pageLoad(){
 
   $this->start();
 
 }
 
 
 
 function start(){
 
  $this->start $this->getTime();
 
 }
 
 
 
 function end(){
 
   $this->end $this->getTime();
 
 }
 
 
 
 function getLoadTime($format '%01.2f'){
 
   if (empty($this->end) )$this->end();
 
   return sprintf($format, ($this->end $this->start));
 
 }
 
 
 
 function getTime(){
 
  $time microtime();
$time explode(' '$time);
return $time[1] + $time[0]; 
 
 }
 }
?>


The class has 5 functions. Bellow we will try to explain what each of them does:

pageLoad()

This is the constructor function. Whenever we initiate the class this function is executed and the class logs the time of the initiation.

start()

This is the actual function that logs the beginning time, thought we don't have to use it as the constructor does this.

end()

This is the same with the start() function, but it logs the time that our script ends. Again we don't have to use it as the next function we will describe is going to.

getLoadTime()

the getLoadTime() function will return the time passed from the beginning. You can give to the function a format string which will be used by sprintf() in order to format the time.

getTime()

This is a private function that returns the time at the time of call.

How to measure page load time with our class

Ok, enough with the theory, let's see how easy is to use this class:

Code:
<?php
include 'pageloadtime.class.php';//This is the supposed file name where we added the class code
$Load = new pageLoad;//Here is the point where we start counting
/*
Here is your code
*/
echo 'This page loaded in ' $Load->getLoadTime() . ' seconds';
?>


That's it, we are done. Hope you liked this tutorial :)
Do you like this tutorial?
Feel free to share it with others using Dzone or your favorite bookmarking service. You can see related discussions in our php forum
This tutorial posted by Nikolas from Sublime Directory

Sponsors :

Code library menu :
[ Tutorials home | Submit a tutorial ]

User Area
Welcome, Guest. Please login or register.
Did you miss your activation email?
Nov 09, 2009, 06:40:05 am





Login with username, password and session length

Donate to our community, and get a permanent link back to your site!

Donate to our community, and get a permanent link back to your site!


Forum Statistics
Total Posts: 42.741
Total Topics: 8.375
Total Members: 6.939
Tutorials : 58
Resources : 929
Designs : 341
Latest Member: Auguste Dubuisson

19 Guests, 3 Users online :

15 users online today:




Web Design Gallery · Whois Lookup · Pagerank · Tag Browsing · Lo-fi version · Syndication · Webmaster forum history · Advertise
Developed by HumanWorks © 2005 - 2009 Webdigity webmaster community · sublime directory
Webdigity Webmaster Forums | Powered by SMF 1.0.12. © 2001-2005, Lewis Media. All Rights Reserved.