PHP PEAR Benchmarking Posted by: Jordan in ProgrammingPHP on
In an earlier blog I told you that my benchmarking functions seemed flawed and that I would find a new one.  I signed up for a PHP Unit Test class using Zend Studio but it isn't until the 19th so for now I've been playing with the PEAR Benchmark Package.

The class is easy to use:
  1. Set a "Start" marker
  2. Execute code you want to benchmark
  3. Set custom marker (your own marker)
  4. repeat 2/3 as many times as needed
  5. Set a "Stop" marker

The sample class in the documentation shows how simple it is to use.


 
  1. require_once 'Benchmark/Timer.php';
  2.  
  3. function wait($amount) {
  4.     for ($i=0; $i < $amount; $i  ) {
  5.         for ($i=0; $i < 100; $i  );
  6.     }
  7. }
  8.  
  9. // Pass the param "true" to automatically display results
  10. $timer = new Benchmark_Timer();
  11. $timer->start();
  12.  
  13. wait(10);
  14.  
  15. $timer->setMarker('Mark1');
  16.  
  17. echo "Elapsed time between Start and Mark1: " .
  18.       $timer->timeElapsed('Start', 'Mark1') . "n";
  19.  
  20. wait(50);
  21.  
  22. $timer->stop();
  23. $timer->display();



Testing Echo
I wanted to test echo using single quotes and double quotes.  The last tests I ran with my custom micro benchmarking function showed that double quotes were faster than single quotes which I believe is wrong.  I rewrote the test using the PEAR Benchmark class. 


 
  1. require 'Benchmark/Timer.php';
  2.  
  3. function executeFunction($callback, $amount) {
  4.     for ($i=0; $i < $amount; $i++) {
  5.             eval($callback);
  6.     }
  7. }
  8.  
  9. // Pass the param "true" to constructor to automatically display the results
  10. $timer = new Benchmark_Timer();
  11. $timer->start();
  12.  
  13. // Echo single
  14. executeFunction("echo 'CC';", 1000);
  15. $timer->setMarker('echo Single');
  16. $timer->timeElapsed('Start', 'echo Single') . "n";
  17.  
  18. // Echo Double
  19. executeFunction("echo "CC";", 1000);
  20. $timer->setMarker('echo Double');
  21. $timer->timeElapsed('echo Single', 'echo Double') . "n";
  22.  
  23. // Stop the microtimer and
  24. // Display the Results      
  25. $timer->stop();
  26. $timer->display();



The executefunction is a wrapper to execute any string passed to it as PHP code (using the eval function).  You can see the different markers I set after executing a branch of code.

Results
And I get the results I expected:

 time indexex time%
Start1214519925.13337300-0.00%
echo Single1214519925.144217000.01084448.17%
echo Double1214519925.155665000.01144850.86%
Stop1214519925.155883000.0002180.97%
total-0.022510100.00%


I intend on doing more benchmarking with this class and once I complete the Zend Unit Testing webinar I'll blog about that as well. As always, if you need any help feel free to ask in our forum!





Trackback(0)
feed0 Comments

Write comment
 
 
quote
bold
italicize
underline
strike
url
image
quote
quote
smile
wink
laugh
grin
angry
sad
shocked
cool
tongue
kiss
cry
smaller | bigger
 

security image
Write the displayed characters


busy