Home arrow PHP arrow Page 4 - Benchmarking Applications with PHP

Using an object-oriented approach for benchmarking scripts - PHP

Benchmarking a particular application across its different development cycles is one of the most exciting things for a PHP developer. If you’re anything like me, you have had your head spinning with questions, such as… is the performance of my last project good enough? How long does it take to fetch a particular data set from my database? Do I have to implement a caching system? As you can see, these and other dilemmas (add your own to the list) sometimes make peace of mind a nearly impossible goal.

TABLE OF CONTENTS:
  1. Benchmarking Applications with PHP
  2. Using the microtime() built-in PHP function
  3. Simplifying the use of the microtime() function
  4. Using an object-oriented approach for benchmarking scripts
  5. Defining a wrapper method for the PHP 5 microtime() function
By: Alejandro Gervasio
Rating: starstarstarstarstar / 4
April 23, 2008

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

If creating procedural timing scripts was a no-brainer process, then you’ll probably find that building a timer class is even simpler. Basically, constructing such a class depends on defining a couple of methods that behave as basic wrappers for the built-in PHP “microtime()” function.

Considering the concepts deployed before, a typical timer class could be defined as follows:


// define 'Timer' class with PHP 4

class Timer{

var $elapsedTime;

// start timer

function start(){

if(!$this->elapsedTime=$this->getMicrotime()){

trigger_error('Error obtaining start time!',E_USER_ERROR);

}

}

// stop timer

function stop(){

if(!$this->elapsedTime=round($this->getMicrotime()-$this->elapsedTime,5)){

trigger_error('Error obtaining stop time!',E_USER_ERROR);

}

return $this->elapsedTime;

}

//define private 'getMicrotime()' method

function getMicrotime(){

list($useg,$seg)=explode(' ',microtime());

return ((float)$useg+(float)$seg);

}

}

}


As you can see, defining a timer class with PHP 4 is a very simple process. As I said before, all that I did here was create a wrapping method for the “microtime()” PHP native function, as well as define some additional methods that are useful for starting and stopping the corresponding timer.

Of course, this is only an introductory example and can be greatly improved. However, if you need a basic but effective timer class and don't want to write complex code, you have one here that may fit your needs.

All right, after showing you how to create a simple timer class with PHP 4, have a look at the following example, which demonstrates how to use it:


// instantiate 'Timer' class

$timer=&new Timer();

// start timer

$timer->start();

// do nothing for a while

usleep(100);

// stop timer

$elapsedTime=$timer->stop();

// display elapsed time

echo 'Time spent in doing nothing was '.$elapsedTime.' seconds';

/*

displays the following

Time spent in doing nothing was 0.00996 seconds

*/


As shown above, the entire process for timing or benchmarking a particular script is reduced to calling up the respective “start()” and “stop()” methods that belong to the “Timer” class. That’s it.

At this point, you hopefully have a clear idea of how to develop a timer class with PHP 4, since this is a no-brainer procedure that can be tackled even by beginners. Therefore, the next lesson of this tutorial will be focused on creating another handy timer class, this time using PHP 5.

Want to learn how this will be done? Jump into the following section and keep reading.



 
 
>>> More PHP Articles          >>> More By Alejandro Gervasio
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: