testing php script performance

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bilibytes
    New Member
    • Jun 2008
    • 128

    testing php script performance

    hi there,

    do anyone know a good php script performance tester? (free..)

    thankyou

    regards.
  • chemlight
    New Member
    • Jan 2009
    • 33

    #2
    Speed Test PHP Script

    I was thinking about how to speed test a php script, and I'm not sure.

    I get the basics:

    testing the first action
    Code:
    $i = 0;
    while($i < 10000){
    //do action 1
    }
    //return time
    testing the second action
    Code:
    $i = 0;
    while($i < 10000){
    //do action 2
    }
    //return time
    ...hmmm...would getting the system time in microseconds at the beginning and end, and getting the difference work? sorry, but I just thought of that. I'll get back in a sec with confirmation or lack thereof.

    Comment

    • chemlight
      New Member
      • Jan 2009
      • 33

      #3
      Found the anser at php manual.

      Here is the code for testing time in microseconds:
      [code=php]
      <?php
      /**
      * Simple function to replicate PHP 5 behaviour
      */
      function microtime_float ()
      {
      list($usec, $sec) = explode(" ", microtime());
      return ((float)$usec + (float)$sec);
      }

      $time_start = microtime_float ();

      // Sleep for a while
      usleep(100);

      $time_end = microtime_float ();
      $time = $time_end - $time_start;

      echo "Did nothing in $time seconds\n";
      ?>
      [/code]

      Comment

      • chemlight
        New Member
        • Jan 2009
        • 33

        #4


        that should do it.

        Comment

        • bilibytes
          New Member
          • Jun 2008
          • 128

          #5
          so the usleep() would be replaced by the script to test?

          Nice thanks!

          Comment

          • TheServant
            Recognized Expert Top Contributor
            • Feb 2008
            • 1168

            #6
            Yeah, you will need microtime() function, which returns a float anyway.
            As much as the script above probably works, really all you need to do is have something like:
            Code:
            $timer = explode( ' ', microtime() );
            $start = $timer[1] + $timer[0];
            
            ***Your Code***
            
            $timer = explode( ' ', microtime() );
            echo( ($timer[1] + $timer[0])-$start );

            Comment

            • chemlight
              New Member
              • Jan 2009
              • 33

              #7
              Thanks for the information. I had tried using date('u') initially, but that only returned all 0's.

              Comment

              • TheServant
                Recognized Expert Top Contributor
                • Feb 2008
                • 1168

                #8
                No worries. I did the same thing when I tried to make one last year before I was told about microtime().

                Comment

                • Markus
                  Recognized Expert Expert
                  • Jun 2007
                  • 6092

                  #9
                  Many PHP frameworks come with built-in benchmark testers. I especially like CodeIgniter's. http://codeigniter.com/user_guide/li...benchmark.html

                  Comment

                  • Markus
                    Recognized Expert Expert
                    • Jun 2007
                    • 6092

                    #10
                    Threads merged. Basically the same topic :)

                    Comment

                    Working...