PHP
  Home arrow PHP arrow Page 2 - Using Timers to Benchmark PHP Applicat...
FaxWave - Free Trial.
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 Developerworks
 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 / 2
    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 - Defining a few working classes
    (Page 2 of 4 )

    The first example that I plan to develop here consists of demonstrating how the timer class that I defined in the first article can be used for benchmarking the performance of a PHP application that fetches the contents of a few simple database rows and prints them on the screen.

    In this first case, I’m going to perform all this without using HTTP compression when the data is sent to the client. In the second example, all of the data will be compressed when transmitted across the network.

    Obviously, the timer class will be tasked with timing each of the applications that I mentioned before, indicating which one has the better performance. Also, it should be noted that all the tests will be done using a local host. Thus, the eventual differences that appear between the two examples could be even more significant, if they’re deployed on remote servers.

    Having clarified this point, here is the complete list of classes that will be used with the first example. Have a look at them, please:


    // define 'MySQL' class


    class MySQL{

    private $conId;

    private $host;

    private $user;

    private $password;

    private $database;

    private $result;

    const OPTIONS=4;

    public function __construct($options=array()){

    if(count($options)!=self::OPTIONS){

    throw new Exception('Invalid number of connection parameters');

    }

    foreach($options as $parameter=>$value){

    if(!$value){

    throw new Exception('Invalid parameter '.$parameter);

    }

    $this->{$parameter}=$value;

    }

    $this->connectDB();

    }

    // connect to MySQL

    private function connectDB(){

    if(!$this->conId=mysql_connect($this->host,$this->user,$this->password)){

    throw new Exception('Error connecting to the server');

    }

    if(!mysql_select_db($this->database,$this->conId)){

    throw new Exception('Error selecting database');

    }

    }

    // run query

    public function query($query){

    if(!$this->result=mysql_query($query,$this->conId)){

    throw new Exception('Error performing query '.$query);

    }

    return new Result($this,$this->result);

    }

    }


    // define 'Result' class


    class Result {

    private $mysql;

    private $result;

    public function __construct($mysql,$result){

    $this->mysql=$mysql;

    $this->result=$result;

    }

    // fetch row

    public function fetchRow(){

    return mysql_fetch_assoc($this->result);

    }

    // count rows

    public function countRows(){

    if(!$rows=mysql_num_rows($this->result)){

    throw new Exception('Error counting rows');

    }

    return $rows;

    }

    // count affected rows

    public function countAffectedRows(){

    if(!$rows=mysql_affected_rows($this->mysql->conId)){

    throw new Exception('Error counting affected rows');

    }

    return $rows;

    }

    // get ID from last-inserted row

    public function getInsertID(){

    if(!$id=mysql_insert_id($this->mysql->conId)){

    throw new Exception('Error getting ID');

    }

    return $id;

    }

    // seek row

    public function seekRow($row=0){

    if(!is_int($row)||$row<0){

    throw new Exception('Invalid result set offset');

    }

    if(!mysql_data_seek($this->result,$row)){

    throw new Exception('Error seeking data');

    }

    }

    }


    // define 'Timer' class


    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(){

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

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

    }

    }

    Well, that’s all for now. As you can see, I defined three simple classes. The first two will be used to connect to MySQL and fetch different result sets. The third one (that is the “Timer” class) will be utilized to roughly time the process of retrieving some rows from a sample database table. Pretty easy, right?

    Now that you know how all the previous classes will be put to work in a single example, go ahead and read the following section. That's where I’ll show you how to benchmark the retrieval of database rows.

    More PHP Articles
    More By Alejandro Gervasio


       · This second part of the series shows how to build a simple timer class with PHP that...
     

       

    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 3 hosted by Hostway