PHP
  Home arrow PHP arrow Page 4 - Using Timers to Benchmark PHP Applications
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
Google.com  
PHP

Using Timers to Benchmark PHP Applications
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 4
    2008-04-30


    Table of Contents:
  • Using Timers to Benchmark PHP Applications
  • Defining a few working classes
  • Displaying database rows without using HTTP compression
  • Displaying database records using HTTP compression

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    Using Timers to Benchmark PHP Applications - Displaying database records using HTTP compression
    ( Page 4 of 4 )

    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
     

       

    PHP ARTICLES

    - Implementing Factory Methods in PHP 5
    - Merging a File Split for FTP Upload using PHP
    - Getting Data from Yahoo Site Explorer Inboun...
    - Method Chaining: Adding More Selecting Metho...
    - How to Split a File During an FTP Upload Usi...
    - Expanding a Custom CodeIgniter Library with ...
    - Using the Yahoo Site Explorer Inbound Links ...
    - Building a CodeIgniter Custom Library with M...
    - Building an E-mini Trading System Using PHP ...
    - Completing the MySQL Class with Method Chain...
    - Building Dynamic Queries with Chainable Meth...
    - PHP Encryption and Decryption Methods
    - Building a MySQL Abstraction Class with Meth...
    - Completing a Sample String Processor with Me...
    - Mastering WHILE Loops for PHP and MySQL





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 3 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek