Friday, December 26, 2014

PHP Profiling with Xhprof (Output Screenshots)

This article is mainly for how-to enable profiling with Xhprof and viewing output in normal html format.

Xhprof Setup

1)You needed to install xhprof.

apt-get install php5-xhprof

2)Enable that.
Create file /etc/php5/apache2/conf.d/20-xhprof.ini (As my os is Ubuntu;So i am creating this ini file into that location(As per your server directory may be different) Or you can choose alternate way that is you can directly paste the below given conf into php.ini under [xhprof].

As paramter mentioned below output directory.Give that directory 777 if it is not tmp directory.
Ex-
chmod 777 <directory name> -R .
)

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

3)Download xhprof from pecl

http://pecl.php.net/package/xhprof

Extracted that and store it into web root directory (Use case will be find in Point 6)

Php Code


4)xhprof_enable (With all possible option.From where you wanted to start).

Example

//xhprof_enable();
//xhprof_enable(XHPROF_FLAGS_CPU + XHPROF_FLAGS_MEMORY);
xhprof_enable(XHPROF_FLAGS_CPU | XHPROF_FLAGS_MEMORY); //Memory and cpu usage


5)xhprof_disable (Save it into variable.Where you wanted to stop.).

$prof=xhprof_disable();


6)Include library and save_run as mentioned below(after disable)

$XHPROF_ROOT="/var/www/xhprof/xhprof";
include_once $XHPROF_ROOT . "/xhprof_lib/utils/xhprof_lib.php";
include_once $XHPROF_ROOT . "/xhprof_lib/utils/xhprof_runs.php";

// save raw data for this profiler run using default
// implementation of iXHProfRuns.
$xhprof_runs = new XHProfRuns_Default();

// save the run under a namespace "xhprof_foo"
$run_id = $xhprof_runs->save_run($prof, "xhprof_test");
echo "Run Id:".$run_id;


7)Open the xhprof_html/index.php you will see your listed run id there like <runid>.xhprof_test.xhprof

For answering output imagination i am attaching screenshot have a look







References
------------------
Referenced from Various Sources.

For more details explanation and other possibilities go through this
http://erichogue.ca/2011/03/linux/profiling-a-php-application/

No comments: