Home » Benchmarking » Benchmark » Manual
Provides a simple framework for profiling and benchmarking your PHP applications.
Introduction
Introduction – About Benchmarking_Benchmark
About Benchmarking_Benchmark
Benchmark is a simple framework that allows you time, profile and benchmark your PHP scripts and applications. Benchmark provides you with three classes to enable you to profile: Benchmark_Timer, Benchmark_Profiler and Benchmark_Iterate.
The package is very easy to use. We'll discuss each of the three classes in detail, but some concepts are common. You basically instantiate one of the classes, begin your script (or portion of code that you want to profile) and analyze the results afterward.
All the three classes have two modes of operation: automatic and manual. In the automatic mode of operation, the profiling is automatically started and stopped, and results displayed. In the manual mode, you are responsible for starting, stopping and displaying the results.
The automatic mode makes sense in cases where you want to quickly test a fixed portion of code like a script or a single function. The manual mode offers you more control, which means arbitrary portions of code can be profiled, and the results can be carefully analyzed before being displayed. In the automatic mode of operation, the results are always displayed; while in the manual mode you have the option of logging them or storing them in a database.
If you wish to operate a class in automatic mode, simply pass the TRUE parameter to its constructor. Instantiating a class without a parameter or with the FALSE parameter sets the mode of operation to manual for that class.
Which class to choose?
Benchmark_Timer is the most fundamental class in the package. It performs the simple function of recording the amount of time it takes for a fixed amount of code to execute.
Benchmark_Profiler behaves just like Benchmark_Timer with the exception that you are allowed to specify sections between the start and stop times, at which point the timing details are recorded. This provides the ability to nest timers and profile functions which are called multiple times.
Benchmark_Iterate is a class that runs a particular function several times over and records the amount of time it took for each iteration to execute.
Benchmark_Timer
Benchmark_Timer – About Benchmark_Timer
About Benchmark_Timer
As mentioned before, this class simply records the time taken for a particular snippet of code to execute. In the automatic mode, this means the code remaining after the class's instantiation. In manual mode, the snippet of code to be timed is specified between calls to the start() and stop() methods of the class.
Additionally, you may specify markers between the snippet of code to be timed. The timing information is recorded wherever markers are defined.
An example will make this clear.
Benchmark_Timer in automatic mode
<?php
require_once "Benchmark/Timer.php";
$timer = new Benchmark_Timer(TRUE);
// Timer is in automatic mode, so timing starts here.
$j = array();
for ($i = 0; $i < 100; $i++) {
$j[] = $i;
if ($i == 49)
$timer->setMarker('Midway');
}
// Timer automatically stops and results are displayed.
?>
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_Timer
---------------------------------------------------- marker time index ex time perct ---------------------------------------------------- Start 1182558324.48638900 - 0.00% ---------------------------------------------------- Midway 1182558324.48655800 0.000169 62.13% ---------------------------------------------------- Stop 1182558324.48666100 0.000103 37.87% ---------------------------------------------------- total - 0.000272 100.00% ----------------------------------------------------
In most cases however, you would like to do something with that information. When you run the timer in manual mode, you can obtain the results as an associative array using the getProfiling() method. To display the information (as shown above) use the display() method. More information on the methods that Benchmark_Timer implements is available in the API Documentation.
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.
Benchmark_Iterate
Benchmark_Iterate – About Benchmark_Iterate
About Benchmark_Iterate
This class is useful when you want to iterate over a single piece of code (a function) several times over. Timing information is recorded for every iteration. An example follows.
Benchmark_Iterate in manual mode
<?php
require_once 'Benchmark/Iterate.php';
$benchmark = new Benchmark_Iterate;
function calculation($num) {
$x = sin($num) * 100;
$y = tan($x);
return;
}
$benchmark->run(100, 'calculation', 45.6);
$result = $benchmark->get();
print_r($result);
?>
The example above will generate an output as shown below when run with the PHP CLI SAPI.
Results of a test run of Benchmark_Iterate
Array
(
[1] => 0.000073
[2] => 0.000014
[3] => 0.000011
[4] => 0.000011
[5] => 0.000011
..93 more results..
[99] => 0.000011
[100] => 0.000011
[mean] => 0.000011
[iterations] => 100
)
You can display formatted results either by running the script in automatic mode or by using the display() method. For more information of the methods this class implements, refer to the API documentation.