Benchmark_Profiler

Benchmark_Profiler – About Benchmark_Profiler

About Benchmark_Profiler

This class behaves mostly like Benchmark_Timer with the exception that you are allowed to specify the beginning and end of sections within the code to be profiled. sections are different from markers (markers) don't have ends in the sense that they are stateful and remember how many times they have been entered.

Timing information is recorded at the beginning and end of the code to be profiled and at the beginning and end of every section everytime it is encountered. An example follows.

Benchmark_Profiler in manual mode

<?php

require_once 'Benchmark/Profiler.php';

$profiler = new Benchmark_Profiler;

function 
wasteTime() {
    
$j 2;
    for (
$i 0$i 100$i++) {
        
$j $j 10;
    }
    return;
}    

function 
myFunction() {
    global 
$profiler;
    
$profiler->enterSection('myFunction');
    
wasteTime();
    
$profiler->leaveSection('myFunction');
    return;
}

// Manual mode, so start manually
$profiler->start();

wasteTime();
myFunction();
myFunction();
wasteTime();

// Manual mode, stop and display results
$profiler->stop();
$profiler->display();

?>

The example above will generate an output as shown below when run with the PHP CLI SAPI. Running it with the Apache SAPI will produce the same results, but formatted as HTML.

Results of a test run of Benchmark_Profiler

------------------------------------------------------------------------------------
Section             Total Ex Time         Netto Ex Time         #Calls    Percentage
------------------------------------------------------------------------------------
myFunction          5.8174133300781E-5    5.8174133300781E-5    2          17.22%
Global              0.00033783912658691   0.00027966499328613   1         100.00%

As you can see, the default output may not be too helpful. You can obtain detailed section by section profiling information in manual mode through methods like getSectionInformations() and getAllSectionsInformations(). Refer to the API documentation for more information.