Categories

(83)
(69)
(8)
(34)
(74)
(149)

PHP-code profiling. XHprof — hierarchical profiler with html-interface

06.03.2011
PHP-code profiling. XHprof — hierarchical profiler with html-interface
Author:

What’s profiling?

Profiling – is a process of application performance analysis done for the purpose of acquiring data about efficiency (a set of characteristics like CPU times, RAM and resources processing, number of function calls and so on). Accordingly, the instrument used for that is called a profiler.

Profilers in most cases are used to find hot-spots in the application (a hot-spot is a part in the code which processing time is inexcusably long) and also for analysis and evaluation of application quality.

PHP-code profiling

The most popular PHP-profilers are:
Xdebug's Profiler (http://www.xdebug.org/docs/profiler) and XHprof (http://pecl.php.net/package/xhprof). We’ll talk about the second one here.

About XHprof

XHprof is a hierarchical profiler for PHP with HTML interface. The core is written in C language (the code is ultimately PHP on the level of reports and interface), and for download it’s available as extension for PHP. This profiler knows how to collect data about memory usage, processor resources, number and order of calls of functions and also inclusive time (time spent for the function and all functions started by it) and exclusive time (time spent for the function with no regard to the time taken by the embedded function).

Additionally, XHprof supports the ability to compare two runs (hierarchical DIFF report) and aggregate results from multiple runs for data averaging.

XHprof Install

 XHprof (version 0.9.2, starting with PHP 5.2.0) is available as PECL-package since July 1, 2009. By the way, it’s not available for Windows.

Installation process is as follows:

pecl download xhprof-0.9.2
tar -xvf xhprof-0.9.2.tar.gz
cd xhprof-0.9.2/extension
phpize
./configure
make
make install 

Then a module in php.ini must be enabled:

extension=xhprof.so
xhprof.output_dir=/var/log/xhprof;

Setting reports interface

XHprof module has an ability to build text and graphic reports.

There are two folders xhprof_html (access to UI и GUI) and xhprof_lib (library for data analysis) in installation directory.

Second folder depending on OS should be placed in /usr/local/share/php/ or /usr/local/lib/php/ (providing global access to library by that).

Then an access to UI should be made. For that a new domain or subdomain must be created on server and a folder xhprof_html (as an option xhprof_lib can also be put here) must be put into it.

To start a profiler:

xhprof_enable(XHPROF_FLAGS_CPU + XHPROF_FLAGS_MEMORY);

Here XHPROF_FLAGS_CPU and XHPROF_FLAGS_MEMORY indicate to a profiler that data needs to be collected, processor usage and OS.

Disabling work of profiler:

$xhprof_data = xhprof_disable();

Saving data:

include_once dirname(__FILE__) . '/xhprof_lib/utils/xhprof_lib.php';
include_once dirname(__FILE__) . '/xhprof_lib/utils/xhprof_runs.php';
$namespace = $_SERVER['SERVER_NAME']; 
$xhprof_runs = new XHProfRuns_Default(); 
$run_id = $xhprof_runs->save_run($xhprof_data, $namespace);

Generating link for report view:

print sprintf('http://<xhprof_domain>/xhprof_html/index.php?run=%s&source=%s', $run_id, $profiler_namespace);. 

Graphic interface

XHprof also has an in-built graphic interface.

As for me, they don’t have any noticeable value, but I still can't help mentioning it :) .

For its proper operation Graphviz application must be installed on the server (it's included into most Linux distributives). HTML interface being configured you can use graphic one without any other moves (link [View Full Callgraph] in reports).

After the first usage, one can come across the following error:

Warning: proc_open() [function.proc-open]: open_basedir restriction in effect. File(/dev/null) is not within the allowed path(s)

It means that a user under whose name the script has been run has no access to null device (/dev/null). Two options: allow such access with the help of property open_basedir в php.ini or change line 108 of file /xhprof_lib/utils/callgraph_utils.php replacing "/dev/null" by any value to which our script will have access (for example I replaced by "/tmp/xhprof_stderr" for testing).

Profiling “live” applications

It’s clear that profiling in test environment may differ a lot from what we have on live site.

That’s why I can’t help mentioning one important factor about XHprof: this module was used to work with live applications. And it has native realization, which implies excellent efficiency.

So it can be easily used on live projects. For example, a launch can be done only once in a hundred requests to server:

if (rand(1, 100) == 1) { 
   xhprof_enable(XHPROF_FLAGS_MEMORY); 
   $xhprof_enable = TRUE; 
 } 

// application execution 

 if ($xhprof_enable) { 
   $xhprof_data = xhprof_disable(); 
   // saving data 
 }

Profiler also has an in-built “Lightweight Sampling” mode. Profiling will be executed every 100 milliseconds after this mode is on. To turn it on, start/stop function of profiler should be replaced by xhprof_sample_enable (instead of xhprof_enable), xhprof_sample_disable (instead of xhprof_disable).

Additional information

The most extensive information about the profiler can be found on its web developers’ page (by the way it’s not just somebody, it’s FaceBook Inc itself).

6 votes, Rating: 5

Read also

1

Sooner or later every developer comes across the situation when site “goes down” and it must be “got back to life”. The reasons for that may be diverse.

2

Probably everybody has come across with ready-to-use site packages on Drupal where after installation you get a ready site with the set of necessary modules, personal theme and so on. There are...

3

Cron.php script is used in Drupal for content indexation, sending e-mails to subscribers, gathering rss-feeds and so on – modules can give task themselves to be done according to schedule.

4

First of all what is Apache Solr? Apache Solr – is the advanced search platform with open primary code by Apache Lucene project.

5

While developing a real estate site for one of our clients we tried to import the complete world cities database into the taxonomy (that was the client's demand).

Subscribe to our blog updates