PHP
  Home arrow PHP arrow Page 5 - Benchmarking Applications with PHP
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

Benchmarking Applications with PHP
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 4
    2008-04-23


    Table of Contents:
  • Benchmarking Applications with PHP
  • Using the microtime() built-in PHP function
  • Simplifying the use of the microtime() function
  • Using an object-oriented approach for benchmarking scripts
  • Defining a wrapper method for the PHP 5 microtime() function

  • 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


    Benchmarking Applications with PHP - Defining a wrapper method for the PHP 5 microtime() function
    ( Page 5 of 5 )

    As I expressed in the section that you just read, it’s possible to define yet another timer class using the advantages offered by PHP 5 with reference to its “microtime()” function. This brand new timing class can be created as follows:


    // define 'Timer' class with PHP 5

    class Timer{

    private $elapsedTime;

    // start timer

    public function start(){

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

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

    }

    }

    // stop timer

    public function stop(){

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

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

    }

    return $this->elapsedTime;

    }

    //define private 'getMicrotime()' method

    private function getMicrotime(){

    return microtime();

    }

    }


    In this case, the above “Timer” class looks nearly identical to its PHP4-based counterpart. Aside from using exceptions to handle potential errors, the class in question uses a simple wrapper for the native “microtime()” function and it can be put to work as follows:


    try{

    // 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';

    }

    catch(Exception $e){

    echo $e->getMessage();

    exit();

    }


    The previous example is very easy to grasp. Thus, it doesn’t merit any additional discussion here. However, feel free to tweak the source code of all the classes that I showed and add your own modifications to it. You’ll have a good time!

    To wrap up

    In this first article of the series, I introduced the basics on how to create benchmarking scripts with PHP 4 and PHP 5. This ranged from procedural approaches to object-oriented methods. You saw that there are different options for timing your applications.

    However, this journey isn’t finished yet, since in the next tutorial, I’ll show you how to use some of these classes to evaluate the performance of different PHP scripts. Don’t miss the next 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 1 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek