One such visualisation is the "flame graph", created by Brendan Gregg, a performance engineer at Netflix. Brendan has released a tool written in Perl that we can use to create flame graphs from Xdebug traces.
Generating Function Traces with Xdebug
First, we need to set up Xdebug to create the function traces we need. Replace your current Xdebug config (after suitable backups have been made of course) with the following:[xdebug]
xdebug.auto_trace = 0
xdebug.trace_output_name = xdebug.trace
xdebug.trace_enable_trigger = 1
xdebug.trace_output_dir = /tmp
xdebug.trace_format = 1
Restart PHP to make this take effect. Note that we are setting the xdebug.trace_enable_trigger option to 1 and xdebug.auto_trace to 0, which means we can enable tracing for any given request by sending a GET, POST or cookie parameter with the name XDEBUG_TRACE. Alternatively, you can enable and disable tracing in your code, using xdebug_start_trace() and xdebug_stop_trace() respectively. For more details about Xdebug's trace options, refer to the documentation.We will then see our trace file appear in the configured directory (in this case /tmp). If you are running systemd, Apache is probably installed with the PrivateTmp option which means it will have its own /tmp directory inaccessible to other services, and you can move your trace file into the "normal" /tmp (or wherever else you may want it) with a command such as this:
Creating the Flame Graph
First, install Brendan Gregg's FlameGraph code somewhere:git clone https://github.com/brendangregg/FlameGraph.git
Now we can feed our trace file into it to produce a lovely SVG flame graph:php -d memory_limit=1G FlameGraph/stackcollapse-xdebug.php /tmp/xdebug.trace.xt | FlameGraph/flamegraph.pl --title="Look at my graph" --subtitle="Wow so cool" > flame-graph.svg