Performance Testing with PHP

After writing some functions or classes its good idea to make sure your code is executing in a timely fashion, especially for web applications. For PHP I use a function derived from the chronmeter class to look for bottle necks (the function is below or download it here).

We recently ran into a problem of a web form running particular slow. It had the following functionality.

  • Insert some data into 2 mysql tables
  • Grab some data from a Microsoft SQL Database
  • Read in a text file, replace some verbiage, then send out a couple of emails.
  • I architected the form and was letting one of our interns implement it. Everything seemed pretty straight forward – this code should fly. After the site went live, I checked out the form and found it running slow. Using the chronometer function I paired it down to our phpmailer. Turns out we were using php’s built in mail function and not phpmailer.

    After switching to phpmailer and setting the option to smtp, I cut the runtime in half. I then realized we were sending out 2 emails and each time opening/closing an smtp connection. I refactored so only 1 connection was opened. Here are the results of the average form runtime:

  • PHP’s built in mail function: Avg: 3 seconds.
  • PHPMAILER function with 2 connections: 1.5 seconds.
  • PHPMAILER function with 1 connection: .105 seconds
  • To use the chronometer function, simply call it before a class or function call and then call it right after it .

    $chronometer(); //start
    $runtime = chronometer();
    print “SendSalesEmail took: $runtime”;


    define(”RET_TIME”, “s”); //Can be set to “ms” for milliseconds or “s” for seconds
    function chronometer()

    $now = microtime(TRUE); // float, in _seconds_

    if (RET_TIME === ’s’) {
    $now = $now + time();
    $malt = 1;
    $round = 7;
    } elseif (RET_TIME === ‘ms’) {
    $malt = 1000;
    $round = 3;
    } else {
    die(”Unsupported RET_TIME value”);

    if ($CHRONO_STARTTIME > 0) {
    /* Stop the chronometer : return the amount of time since it was started,
    in ms with a precision of 3 decimal places, and reset the start time.
    We could factor the multiplication by 1000 (which converts seconds
    into milliseconds) to save memory, but considering that floats can
    reach e+308 but only carry 14 decimals, this is certainly more precise */

    $retElapsed = round($now * $malt - $CHRONO_STARTTIME * $malt, $round);


    return $retElapsed;
    } else {
    // Start the chronometer : save the starting time


    return 0;



    A .NET, PHP, Marketing Guru authority, at least I hope I am. Reach me at

    6 thoughts on “Performance Testing with PHP

    1. yimp says:

      umm good script.Thank for your tutorial


    2. I combined it down to our phpmailer. Turns out we were utilizing php’s worked in mail work and not phpmailer

    3. You’ve written pleasant post, I am going to bookmark this page; a debt of gratitude is in order for data. I really value your own particular position and I will make certain to return here. Much thanks to you for offering your insight to us. I am always tried to read this kind of post, continue sharing.

    4. John Arnold Is An Academic Writer Of The Dissertation-Guidance. Who Writes Quality Academic Papers For Students To Help Them In Accomplishing Their Goals.

    5. I Personally Like Your Post, You Have Shared Good Article. It Will Help Me In Great Deal.

    Leave a Reply

    Your email address will not be published. Required fields are marked *


    You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>