Nice n' Easy JQuery Image Rotator
Changing Mailman Python Scripts for Virtual Host Support
Using Multi-Byte Character Sets in PHP (Unicode, UTF-8, etc)
Native Linux Space Warfare: Freespace 2
Book Review: How to Implement Design Patterns in PHP
Development Resource Project

Visualising Website Performance with Flame Graphs

Sunday, 17 May 20, 10:35 am
As well as allowing you to step through code line-by-line, and to record profiling data, Derick Rethans' Xdebug can log all function calls, including parameters and return values, to give further insight into how your code runs. These "function traces" can be dumped as HTML, human-readable text or in a machine-readable format that can be read by other software packages to create visualisations.

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:
sudo find /tmp -name '*.xt' -exec mv {} /tmp \;

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

Please enter your comment in the box below. Comments will be moderated before going live. Thanks for your feedback!

Cancel Post

/xkcd/ Linear Sort