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.
Maintained forks
There are two main maintained forks of the xhprof extension:
- open_in_new longxinH/xhprof - available from PECL.
- open_in_new tideways/php-xhprof-extension .
Installation of longxinH/xhprof (PECL)
# Docker PHP-based image:
install-php-extensions xhprof
Set a parameter for the output dir in php.ini:
xhprof.output_dir = /tmp/xhprof
How to use it:
xhprof_enable(XHPROF_FLAGS_NO_BUILTINS | XHPROF_FLAGS_CPU | XHPROF_FLAGS_MEMORY);
// .. code
$xhprof_data = xhprof_disable();
print_r($xhprof_data);
We can use tools from the library to analyze the reports:
xhprof_enable(XHPROF_FLAGS_NO_BUILTINS | XHPROF_FLAGS_CPU | XHPROF_FLAGS_MEMORY);
// .. code
$xhprof_data = xhprof_disable();
include_once "/app/web/xhprof_lib/utils/xhprof_lib.php";
include_once "/app/web/xhprof_lib/utils/xhprof_runs.php";
$xhprof_runs = new \XHProfRuns_Default();
$run_id = $xhprof_runs->save_run($xhprof_data, "xhprof_testing");
echo "http://localhost/xhprof_html/index.php?run={$run_id}&source=xhprof_testing\n";
You can find the tools in the GitHub repository.
Andrew Dorokhov