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

Displaying database records using HTTP compression - 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.

TABLE OF CONTENTS:
  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
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

According to the concepts that I expressed in the previous section, the second benchmarking example that I plan to develop will be similar to the first one. However, the main difference will involve displaying the respective database rows using HTTP compression. This means that if everything goes as expected, there should be a small reduction in the time it takes to display these rows on the browser.

Having explained how the benchmarking test in question will be performed, what I’ll do next is define an additional class, called “DataCompressor,” that will be tasked with compressing any data inputted into its constructor via the GZIP algorithm. That said, here is the signature for this brand new class:


// define 'DataCompressor' class


class DataCompressor{

private $data;

public function __construct($data){

if(!is_string($data)){

throw new Exception('Data must be a string!');

}

$this->data=$data;

}

public function compressData(){

// remove white spaces from (X)HTML code

$this->data=preg_replace(array("/r/","/n/"),'',$this->data);

// check if browser supports gzip encoding

if(strstr($_SERVER['HTTP_ACCEPT_ENCODING'],'gzip')){

// start output buffer

ob_start();

// echo page contents to output buffer

echo $this->data;

// compress data with gzip

$this->data=gzencode(ob_get_clean(),9);

// send content encoding http header

header('Content-Encoding: gzip');

}

return $this->data;

}

}


As shown above, the “DataCompressor” class compresses any string passed as an argument to the respective constructor via its “compressData()” method and returns this data to calling code.

As you’ll realize, the logic implemented by this class is pretty easy to grasp. Therefore, pay attention to the following example, which fetches the same ten rows from the previous “USERS” MySQL database table and displays them on the browser, but this time using HTTP compression:


// example using HTTP compression

try{

// instantiate 'Timer' class

$timer=new Timer();

// start timer

$timer->start();

// connect to MySQL

$db=new MySQL(array
('host'=>'host','user'=>'user','password'=>'password','database'=>'database'));

$result=$db->query('SELECT * FROM users');

$data='';

while($row=$result->fetchRow()){

$data.='ID: '.$row['id'].' Name: '.$row['name'].' Email: '.$row['email'].'<br />';

}

$data.='Time spent in fetching database rows was '.$timer->stop().' seconds';

// use 'DataCompressor' object

$dataComp=new DataCompressor($data);

echo $dataComp->compressData();

}

catch(Exception $e){

echo $e->getMessage();

exit();

}


/* displays the following


ID: 1 Name: user1 Email: user1@domain.com

ID: 2 Name: user2 Email: user2@domain.com

ID: 3 Name: user3 Email: user3@domain.com

ID: 4 Name: user4 Email: user4@domain.com

ID: 5 Name: user5 Email: user5@domain.com

ID: 6 Name: user6 Email: user6@domain.com

ID: 7 Name: user7 Email: user7@domain.com

ID: 8 Name: user8 Email: user8@domain.com

ID: 9 Name: user9 Email: user9@domain.com

ID: 10 Name: user10 Email: user10@domain.com

Time spent in fetching database rows was 0.0049 seconds


*/


As you can see, the previous example shows a small, almost negligible difference when compared to the first case, because database rows were sent to the client compressed. However, you must remember that the two examples were tested with a local server. And even considering this condition, the overall result was slightly faster when HTTP compression was applied to the respective data.

On one hand, you learned how to define a timer class with PHP 5, while on the other, you saw two concrete cases where this class was used with pretty good results. Of course, my main recommendation here is that you test the previous timer class with your own PHP applications to evaluate their real performance.

Final thoughts

Finally, we’ve come to the end of this article. In this second tutorial of the series, I developed a couple of hands-on examples with the purpose of using a timer class to benchmark their respective performances. I hope this experience was pretty instructive for you.

In the last part of the series, I’ll show you how to use the same timer class, but this time to evaluate the difference between fetching rows from a database table and retrieving content from a text file.

See you in the last article!



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