Profiling PHP

One of PHP’s historical problems is the lack of good tools aimed at profiling and debugging. Since the language was created, most programmers have been taught to simply add profiling lines through the PHP code to be able to see in which part the script is actually taking the most time in. The following script would be a good example of the old way of doing things:

<?php
include_once(‘Benchmark/Timer.php’);
$bench = new Benchmark_Timer;
$bench->start();

include_once(‘smarty.class.php’);
$smarty = new smarty;
$smarty->caching = 0;
$bench->setMarker(__LINE__);

// fully clear the Smarty cache
$smarty->assign(‘name’, ‘Joao Prado Maia’);

$smarty->display(‘cache_handler.tpl’);

$bench->stop();
var_dump($bench->getProfiling());
?>

The script is reusing an existing benchmarking utility from PEAR called Benchmark_Timer, and will display an array that contains the benchmarking information, which is built by repeatedly calling the benchmark code that computes the current timestamp and the difference to the previous call, hence calculating the difference in time from one part of the code to the next. Here’s what this script would output on your browser:

Profiling PHP

The information displayed on the screenshot above is pretty useful, but profiling PHP scripts this way is extremely error prone and laborious, since you need to take the time to add the benchmark class to your application, and sprinkle your code with calls to the appropriate function.

Fortunately, there’s a better way to profile PHP scripts with some fairly recent open-source tools. We will go over some of them next.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.223.196.211