Wednesday, September 3, 2014

Install PHP Profiler XHProf on Debian

# Install Prerequisites
apt-get install php5-dev graphviz php-pear

# Install XHProf
pecl config-set preferred_state beta
pecl install xhprof

# Create XHProf output directory
mkdir /tmp/xhprof
chown www-data:www-data /tmp/xhprof

# Include XHProf Alias in /etc/apache2/sites-available/default

Alias /xhprof_html /usr/share/php/xhprof_html/

   Options FollowSymLinks
   AllowOverride Alll
   Order allow, deny
   Allow from all


# Create PHP page for initializing XHProf e.g. vi /var/www/xhprof_header.php

// Init XHPROF
if (extension_loaded('xhprof')) {
    xhprof_enable(XHPROF_FLAGS_CPU + XHPROF_FLAGS_MEMORY);
    $XHPROF_ROOT = '/usr/share/php';
    include_once $XHPROF_ROOT . "/xhprof_lib/utils/xhprof_lib.php";
    include_once $XHPROF_ROOT . "/xhprof_lib/utils/xhprof_runs.php";
}

$i = 0;
while ($i < 10000) {
    $i++;
}
?>
 
# Create PHP page for de-initializing XHProf e.g. vi /var/www/xhprof_footer.php
// De-Init XHPROF, generate report and link
if (extension_loaded('xhprof')) {
    $data = xhprof_disable();
    $xhprof_runs = new XHProfRuns_Default();
    $run_id = $xhprof_runs->save_run($data, "xhprof");
    $profiler_url = sprintf('http:///xhprof_html/index.php?run=%s', $run_id);
    echo 'Profiler output';
}
?> 

# Configure XHProf via /etc/php5/apache2/php.ini
# Prepend/Append XHProf initializer/deinitializer page

[xhprof]
extension=xhprof.so
xhprof.output_dir="/tmp/xhprof"

auto_prepend_file=/var/www/xhprof_header.php
auto_append_file=/var/www/xhprof_footer.php


# Restart Apache2
service apache2 restart

No comments: