One of the most important aspects of a load monitoring tool is that it doesnt create load itself. In order to review the load created by LoadAvg 2.0 we are going to be running it through two of the leading php profiling tools.
In this post we start with XHProf. HXProf is a is a function-level hierarchical profiler for PHP that has a simple HTML based user interface and integrated charts. It was written by Facebook and then Open Sourced and can be found at https://github.com/facebook/xhprof
XHProf is a function-level hierarchical profiler for PHP and has a simple HTML based navigational interface. The raw data collection component is implemented in C (as a PHP extension). The reporting/UI layer is all in PHP. It is capable of reporting function-level inclusive and exclusive wall times, memory usage, CPU times and number of calls for each function. Additionally, it supports ability to compare two runs (hierarchical DIFF reports), or aggregate results from multiple runs.
Installing XHProf Core
Installing XHprof is easy, you can use your package manager or PECL to install it as a php extension. We will assume you can get thois part working!
yum install xhprof
pecl install xhprof
Either of this will install the core PHP profiling component on your system. Since the GUI can create lovely call-charts for you, you will also need to install graphviz
yum intall graphviz
You can test that xhprof is recognized by PHP by chyecking thr PHP modules and seeing if it comes up.
php -m | grep xhprof
If you dont see the php module here then maybe you need help with your core installation. There are lost of great blog posts on installing XHprof, you can try this one
Installing the GUI
Once the core components are in place you will then need to grab the front end (GUI) from GitHub. We are using a GUI enhanced fork of the official FB one by preinheimer on github. You can stick this anywhere on your box – I placed it in by vanilla lamp stack next to LoadAvg at /var/www/html/xhprof as follows:
git clone git://github.com/preinheimer/xhprof.git
Now you are ready to get it up and running. for the GUI to work you will need to create a MySQL database, configure the XHprof settings and then tell Apache to add proiling to all the PHP files you want to track – this is much easier that it sounds.
We used PHPMyAdmin to create a database called
xhprofdb, and then used the table setup query to create the tracking table, it is located at the top of
Next we created our setttings file, by copying the config file in xhprof/xhprof_lib called config.sample.php to config.php and editing it as follows:
// Change these:
$_xhprof['dbtype'] = 'mysql'; // Only relevant for PDO
$_xhprof['dbhost'] = 'localhost';
$_xhprof['dbuser'] = 'DBUSERHERE';
$_xhprof['dbpass'] = 'DBPASSHERE';
$_xhprof['dbname'] = 'DBNAMEHERE';
$_xhprof['dbadapter'] = 'Pdo';
$_xhprof['servername'] = 'myserver';
$_xhprof['namespace'] = 'myapp';
//important! you need to set this to the location of you xhprof folder!
$_xhprof['url'] = 'http://localhost/xhprof/xhprof_html';
We then also commented out the code here for Linux
//These are good for linux and its derivatives.
$_xhprof['dot_binary'] = '/usr/bin/dot';
$_xhprof['dot_tempdir'] = '/tmp';
$_xhprof['dot_errfile'] = '/tmp/xh_dot.err';
And since this is a private VM anyways we set
//Control IPs allow you to specify which IPs will be permitted to control when profiling is on or off within your application, and view the results via the UI.
$controlIPs = false; //Disables access controlls completely.
Now to test it all out, point your browser to:
Tell Apache what to profile
Now we are good to go – we just need to tell apache to use us when serving up LoadAvg files and we can start! Since we arent using a virtual host, just vanilla LAMP on our dev box, we tell Apache where to find the xhprof header and footer which need to be appended to the code that you need profiled.
This is quite easy as well, just edit you httpd.conf file
and add the following towards the end – the location inside the Directory brackets is the location of our LoadAvg installation that we will be tracking and the location of the header and footer are inside our xhprof download from git location.
php_value auto_prepend_file “/var/www/html/xhprof/external/header.php”
php_value auto_append_file “/var/www/html/xhprof/external/footer.php”
The restart Apache and we will be ready to Test.
service httpd restart
Profiling Our Code
For XHProf to profile your code, you need to tell it by adding a profiling value in the URL of the code you are running,
?_profile=1. We do this for our LoadAvg installation like this
Now, in the footer of the page, there will be a link to the profiling output at the bottom of the page called ‘profiling output’. Click this link and you will see XHProf in action!
You can drill down and around your code by clicking around, and if you want to see the callgraph you can click at the top of the page on the View Callgraph link which shows something like this
We are instantly able to identify areas where LoadAvg can be streamlined and cpu usage can be reduced and optimized! We will do another post once start tweaking LoadAvg codebase based on the output of this sweet tool.