how to set up phpunit & xhprof for your web application

PHPUnit is a very great tool to test your web application. Really hate finding some mistake just before living my web application.So I guess if I could test my application asap by using some automatic test libs is very good.

Any way, let begin set up the enviroment first.

Very easy to download by using PEAR. Just follow the guide of PHPUnit document. Here is the link http://www.phpunit.de/manual/current/en/installation.html

Just pay attention to the path of you pear. If you install your php like

/somedisk/somepath/php/pear

You should use the correct path.

After installing PHPUnit. You can find phpunit in your path. If not, just set it up. Modify the ~/.bash_profile file. Add new PATH.

Done!!!

You can write you unit test case for you web application now. Just include the Autoload.php under PHPUnit folder.

XHProf is very cool tool . It is a function-level hierarchical profiler for PHP and has a simple HTML based user interface. Published by Facebook.com. Nice!!!

Download XHProf http://pecl.php.net/get/xhprof-0.9.2.tgz

tar zxf xhprof-0.9.2.tgz
cd xhprof-0.9.2
cp -r xhprof_html xhprof_lib /somedisk/somepath/www/xhprof/
cd extension/
/somedisk/somepath/php/bin/phpize
./configure –with-php-config=/somedisk/somepath/php/bin/php-config
make && make install

Add blow to your php.ini

extension=xhprof.so
xhprof.output_dir=/somedisk.somepath/logs/xhprof

Reset your web server. That’s it.

Now put some code to your PHP files.

<?php
xhprof_enable();
xhprof_enable(XHPROF_FLAGS_NO_BUILTINS);
xhprof_enable(XHPROF_FLAGS_CPU + XHPROF_FLAGS_MEMORY);
$xhprof_on = true;
//your code......
if($xhprof_on) {
$xhprof_data = xhprof_disable();
$xhprof_root = '/somedisk/somepath/www/xhprof/';
include_once $xhprof_root . "xhprof_lib/utils/xhprof_lib.php";
include_once $xhprof_root . "xhprof_lib/utils/xhprof_runs.php";
$xhprof_runs = new XHProfRuns_Default();
$run_id = $xhprof_runs->save_run($xhprof_data, "hx");
echo '<br /><a href="http://127.0.0.1/xhprof/xhprof_html/index.php?    run='.$run_id.'&source=hx" target="_blank">View Report</a>';
}
?>

Done & Enjoy!