PHP
  Home arrow PHP arrow Page 4 - Benchmarking Applications with PHP
Dev Shed Forums 
Administration  
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 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Download TestComplete 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
IBM Rational Software Development Conference
 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

Benchmarking Applications with PHP
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 3
    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:
      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

    PCmover - $15 Off with Coupon Code CJPH7Q

    Benchmarking Applications with PHP - Using an object-oriented approach for benchmarking scripts
    (Page 4 of 5 )

    If creating procedural timing scripts was a no-brainer process, then you’ll probably find that building a timer class is even simpler. Basically, constructing such a class depends on defining a couple of methods that behave as basic wrappers for the built-in PHP “microtime()” function.

    Considering the concepts deployed before, a typical timer class could be defined as follows:


    // define 'Timer' class with PHP 4

    class Timer{

    var $elapsedTime;

    // start timer

    function start(){

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

    trigger_error('Error obtaining start time!',E_USER_ERROR);

    }

    }

    // stop timer

    function stop(){

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

    trigger_error('Error obtaining stop time!',E_USER_ERROR);

    }

    return $this->elapsedTime;

    }

    //define private 'getMicrotime()' method

    function getMicrotime(){

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

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

    }

    }

    }


    As you can see, defining a timer class with PHP 4 is a very simple process. As I said before, all that I did here was create a wrapping method for the “microtime()” PHP native function, as well as define some additional methods that are useful for starting and stopping the corresponding timer.

    Of course, this is only an introductory example and can be greatly improved. However, if you need a basic but effective timer class and don't want to write complex code, you have one here that may fit your needs.

    All right, after showing you how to create a simple timer class with PHP 4, have a look at the following example, which demonstrates how to use it:


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

    /*

    displays the following

    Time spent in doing nothing was 0.00996 seconds

    */


    As shown above, the entire process for timing or benchmarking a particular script is reduced to calling up the respective “start()” and “stop()” methods that belong to the “Timer” class. That’s it.

    At this point, you hopefully have a clear idea of how to develop a timer class with PHP 4, since this is a no-brainer procedure that can be tackled even by beginners. Therefore, the next lesson of this tutorial will be focused on creating another handy timer class, this time using PHP 5.

    Want to learn how this will be done? Jump into the following section and keep reading.

    More PHP Articles
    More By Alejandro Gervasio


       · One exciting thing that you want to do with PHP, is benchmarking a particular...
       · The technique described (using microtime()) has limitations in that you must add the...
       · Thank you for commenting on my PHP article and for mentioning the xdebug extension....
     

       

    PHP ARTICLES

    - Setting Up a Web-based Image Hosting Service
    - Comparing Files and Databases with PHP Bench...
    - Setting Up a Web-Based Image Gallery
    - Using Timers to Benchmark PHP Applications
    - Benchmarking Applications with PHP
    - Setting Up a Web-Based File Manager: PHPfile...
    - Developing a Modular Class For a PHP File Up...
    - Setting Up a Web-Based File Manager: bfExplo...
    - Defining a Custom Function for File Uploader...
    - Parsing Child Nodes with the DOM XML extensi...
    - Creating an Error Handling Module for a PHP ...
    - Accessing Attributes and Cloning Nodes with ...
    - Retrieving Information on Selected Files wit...
    - Handling HTML Strings and Files with the DOM...
    - Building File Uploaders with PHP 5

     
    Accelerating Trading Partner Performance
     
    Competing on Analytics
     
    Cost Effective Scaling with Virtualization and Coyote Point Systems
     
    Five Checkpoints to Implementing IP Telephony
     
    Hosted Email Security: Staying Ahead of New Threats
     




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