Filter #2: Benchmark Information

One common need for web developers is to profile their PHP applications to make sure that everything is properly set up performance-wise. The usual way to go about this is to record the time in various portions of code, and then calculate how long each section is taking, and then take measures to improve the performance.

However, when doing this benchmarking work manually by timing each section of the PHP code, the template parsing and compilation steps will not be taken into account. One of the workarounds is to create a postfilter plug-in that simply records the time when Smarty finishes the compilation of a template, and another output filter plug-in to calculate the processed time and add the generated benchmark information to the template output. We will go over these plug-ins next, starting with the overall benchmark script.

The following is the full source code for benchmark.php, which is based on the previous example’s source code:

<?php
ini_set(“include_path”,”.;c:\server\xampp\php\pear\”);
include_once(‘libs/Smarty.class.php’);
$smarty = new Smarty;
$smarty->force_compile = TRUE;

// PEAR’s Benchmark::Timer package
include_once(‘Benchmark/Timer.php’);
$bench = new Benchmark_Timer;
$bench->start();

function register_compilation($source, &$smarty)
{
    $GLOBALS[‘bench’]->setMarker(‘Finished compilation’);
    return $source;
}

function append_benchmark_results($output, &$smarty)
{
    $GLOBALS[‘bench’]->stop();
    $results = $GLOBALS[‘bench’]->getProfiling();
    $output .= ‘<table border=1>’;
    $output .= 

‘<tr><th>Marker</th><th>Diff</th><th>Total</th></tr>’;
    for ($i = 0; $i < count($results); $i++) {
        $output .= ‘<tr><td>’ . $results[$i][‘name’] . ‘</td>’;
        $output .= ‘<td>’ . $results[$i][‘diff’] . ‘</td>’;
        $output .= ‘<td>’ . $results[$i][‘total’] . 

‘</td></tr>’;
    }
    $output .= ‘</table>’;

    return $output;
}

function remove_html_comments($source, &$smarty)
{
    // remove any html comments from the template source, even 
    // if they span multiple lines
    return preg_replace(‘/<!--.*-->/Ums’, ‘’, $source);
}

$bench->setMarker(‘Before display’);

$smarty->register_prefilter(‘remove_html_comments’);
$smarty->register_postfilter(‘register_compilation’);
$smarty->register_outputfilter(‘append_benchmark_results’);
$smarty->load_filter(‘output’, ‘trimwhitespace’);
$smarty->display(‘benchmark.tpl’);
?>

There are many details here. To start with, we are using PEAR’s Benchmark_Timer class to measure the time difference between each portion of the code. You will need to replace the path on the second line of your code with the path where your PEAR installation is stored. We also created two new filter plug-ins, one being a postfilter called register_compilation to be run right after the compilation phase of Smarty. The other one called append_benchmark_results will be executed right before sending the template output back to the web browser, and we will use it to calculate the profile information, and build a HTML table with the results.

One very important detail here is critical to making these plug-ins work the way we want them to, and that is to force Smarty to always compile the given template. We do that by setting Smarty’s force_compile property to TRUE. If that change is not done, the postfilter plug-in will only be executed when a template is compiled into a PHP script. Since the benchmarking example is strictly for development purposes, we are performing this change. You should be sure to disable this property when pushing your website or application onto its production server, as forcing the compilation of templates will result in a big performance loss.

Here is the source for the benchmark.tpl template file, which is the same as the previous example’s template:

<!--
This page will serve as...
-->

<html>
<head>
<title>Removing HTML Comments</title>
</head>

<body>

<!-- page title -->
<h1>Removing HTML Comments</h1>

<!-- real content starts here -->
<p>
  In order to remove HTML comments, you must 
  create a Smarty filter.
</p>

</body>
</html>

Now try running the given PHP script with your web browser. You should see something similar to the following figure:

Filter #2: Benchmark Information

The benchmark results are properly appended to the template output, and you can analyze them to decide potential changes to your PHP code. The first column displays the label of each benchmark marker, while the other columns display the difference in time between that marker and the previous one, and the total execution time so far. As you can see, the total execution time is 0.047573 seconds for this trial run.

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

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