Home arrow PHP arrow Page 2 - Using Timers to Benchmark PHP Applications

Defining a few working classes - PHP

If you’ve been using PHP for a while, then it’s possible that you want to learn how to create benchmarking scripts. If this is true, then this series of tutorials will be pretty helpful. Welcome to the second article of the series “Benchmarking applications with PHP.” Composed of three parts, this series walks you through the development of several approaches aimed at benchmarking specific blocks of code and even entire PHP applications.

  1. Using Timers to Benchmark PHP Applications
  2. Defining a few working classes
  3. Displaying database rows without using HTTP compression
  4. Displaying database records using HTTP compression
By: Alejandro Gervasio
Rating: starstarstarstarstar / 4
April 30, 2008

print this article



The first example that I plan to develop here consists of demonstrating how the timer class that I defined in the first article can be used for benchmarking the performance of a PHP application that fetches the contents of a few simple database rows and prints them on the screen.

In this first case, I’m going to perform all this without using HTTP compression when the data is sent to the client. In the second example, all of the data will be compressed when transmitted across the network.

Obviously, the timer class will be tasked with timing each of the applications that I mentioned before, indicating which one has the better performance. Also, it should be noted that all the tests will be done using a local host. Thus, the eventual differences that appear between the two examples could be even more significant, if they’re deployed on remote servers.

Having clarified this point, here is the complete list of classes that will be used with the first example. Have a look at them, please:

// define 'MySQL' class

class MySQL{

private $conId;

private $host;

private $user;

private $password;

private $database;

private $result;

const OPTIONS=4;

public function __construct($options=array()){


throw new Exception('Invalid number of connection parameters');


foreach($options as $parameter=>$value){


throw new Exception('Invalid parameter '.$parameter);






// connect to MySQL

private function connectDB(){


throw new Exception('Error connecting to the server');



throw new Exception('Error selecting database');



// run query

public function query($query){


throw new Exception('Error performing query '.$query);


return new Result($this,$this->result);



// define 'Result' class

class Result {

private $mysql;

private $result;

public function __construct($mysql,$result){




// fetch row

public function fetchRow(){

return mysql_fetch_assoc($this->result);


// count rows

public function countRows(){


throw new Exception('Error counting rows');


return $rows;


// count affected rows

public function countAffectedRows(){


throw new Exception('Error counting affected rows');


return $rows;


// get ID from last-inserted row

public function getInsertID(){


throw new Exception('Error getting ID');


return $id;


// seek row

public function seekRow($row=0){


throw new Exception('Invalid result set offset');



throw new Exception('Error seeking data');




// define 'Timer' class

class Timer{

private $elapsedTime;

// start timer

public function start(){


throw new Exception('Error obtaining start time!');



// stop timer

public function stop(){


throw new Exception('Error obtaining stop time!');


return $this->elapsedTime;


//define private 'getMicrotime()' method

private function getMicrotime(){

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

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



Well, that’s all for now. As you can see, I defined three simple classes. The first two will be used to connect to MySQL and fetch different result sets. The third one (that is the “Timer” class) will be utilized to roughly time the process of retrieving some rows from a sample database table. Pretty easy, right?

Now that you know how all the previous classes will be put to work in a single example, go ahead and read the following section. That's where I’ll show you how to benchmark the retrieval of database rows.

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

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- 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: