PHP
  Home arrow PHP arrow Page 5 - Benchmarking Applications with PHP
The Best Selling PC Migration Utility.
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 
 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

    Dell PowerEdge Servers

    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!


    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.

       · 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




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