PHP
  Home arrow PHP arrow Page 4 - Using Timers to Benchmark PHP Applicat...
Dev Shed Forums 
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Actuate Whitepapers 
VeriSign Whitepapers 
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? 
PHP

Using Timers to Benchmark PHP Applications
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 3 stars3 stars3 stars3 stars3 stars / 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:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb 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

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    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!


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

       · This second part of the series shows how to build a simple timer class with PHP that...
       · It looks like the timer code-coverage did not include the compression function...
     

       

    PHP ARTICLES

    - Viewing and Editing Tasks for a Project Mana...
    - More on Private Methods with PHP 5 Member Vi...
    - Adding Tasks to a Project Management Applica...
    - Utilizing Private Methods with PHP 5 and Mem...
    - Making Changes in a Project Management Appli...
    - Defining Public and Protected Methods with M...
    - HTML for a Project Management Application
    - Using Subclasses and Accessors with Member V...
    - Implementing Internet Protocols with PHP
    - Project Management: The Application
    - Working with Private Properties to Protect P...
    - Protecting PHP 5 Class Data with Member Visi...
    - Setting Up a Web-based Image Hosting Service
    - Comparing Files and Databases with PHP Bench...
    - Setting Up a Web-Based Image Gallery

    Click Here




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway