A script to convert a directory of xhprof or uprofiler sampling files to a format that can be read by Brendan Gregg's FlameGraph script.
Note: This works with files generated by xhprof_sample_enable
/xhprof_sample_disable
. It will not work with the more common hierarchical xhprof/uprofiler format.
First, generate the sampling data, writing the files with a sample_xhprof
(resp. sample_uprofiler
) file extension. Then run the script passing the directory containing those files as an argument like so:
One way to generate the sample data is prepend the following fragment to the entry point of the application, often index.php
:
if (extension_loaded('uprofiler')) {
$profiler = 'uprofiler';
}
elseif (extension_loaded('xhprof')) {
$profiler = 'xhprof';
}
else {
$profiler = NULL;
}
if ($profiler) {
$enable = "{$profiler}_sample_enable";
$disable = "{$profiler}_sample_disable";
$uri = substr($_SERVER['REQUEST_URI'], 1);
$uri = preg_replace('/[?\/]/', '-', $uri);
$enable();
register_shutdown_function(function () use($disable, $profiler, $uri) {
$filename = "/tmp/xhprof/{$uri}." . uniqid() . ".{$profiler}";
file_put_contents($filename, serialize($disable()));
chmod($filename, 0777);
});
}
./xhprof-sample-to-flamegraph-stacks /directory/with/sample/xhprof/files | flamegraph.pl > xhprof-flamegraph.svg