Wednesday, November 10, 2010

XHProf PHP Profiler

apt-get install php5-common
 
pecl config-set preferred_state beta
pecl install xhprof
If it was not possible from the above steps please use this
wget http://pecl.php.net/get/xhprof-0.9.2.tgz
tar xvf xhprof-0.9.2.tgz
cd ./xhprof-0.9.2/extension/
phpize
./configure --with-php-config=/usr/local/bin/php-config
make
make install
make test
After installation add to the php.ini file
[xhprof]
extension=xhprof.so
xhprof.output_dir="/var/tmp/xhprof"
Now you need to create 2 files header.php and footer.php
header.php
<?php
if (extension_loaded('xhprof')) {
include_once '/usr/local/lib/php/xhprof_lib/utils/xhprof_lib.php';
include_once '/usr/local/lib/php/xhprof_lib/utils/xhprof_runs.php';
xhprof_enable(XHPROF_FLAGS_CPU + XHPROF_FLAGS_MEMORY);
}
footer.php
if (extension_loaded('xhprof')) {
$profiler_namespace = 'myapp'; // namespace for your application
$xhprof_data = xhprof_disable();
$xhprof_runs = new XHProfRuns_Default();
$run_id = $xhprof_runs->save_run($xhprof_data, $profiler_namespace);
 
// url to the XHProf UI libraries (change the host name and path)
$profiler_url = sprintf('http://myhost.com/xhprof/xhprof_html/index.php?run=%s&source=%s', $run_id, $profiler_namespace);
echo '<a href="'. $profiler_url .'" target="_blank">Profiler output</a>';
}
Now the files are ready we have to include in all pages 
of our website. We can do that by adding it to the .htaccess file.
php_value auto_prepend_file /var/www/xhprof/header.php
php_value auto_append_file /var/www/xhprof/footer.php

Tuesday, November 9, 2010

Zend http client

To replace file_get_contents

$client = new Zend_Http_Client($url, array(
        'maxredirects' => 0,
        'timeout'      => 30));
 
$response = $client->request('GET');
$json = $response->getBody();