How-to: Profiling PHP scripts using XDebug

Today, I found a new tool the analyse PHP scripts for bottlenecks. By using XDebug you can collect a list of called functions and their runtime. That information is very useful, because the most time consuming tasks are most likely the ones you want to optimize. After collecting the information, we want to generate a graph to visualize the functions. The image below is a graph of the DuoWorks’ main page. DuoWorks is a MVC PHP framework, designed for optimizing web applications performance, but that is another subject.

An example of a dot graph of XDebug's profiling log of DuoWorks' main page

An example of a dot graph of XDebug's profiling log of DuoWorks' main page

Perhaps you already noticed that not every function is listed in the graph. This is because I generated the graph with the default threshold of at least 1% runtime. Every function with a runtime of less than 1% of 12 ms (= total runtime) is therefore not visible. These functions are useless to optimize anyway.

Installing XDebug

Installing XDebug is pretty easy, because it is available in the Ubuntu repositories.

sudo apt-get install php5-xdebug

After installing, we need to customize the XDebug configuration. Append the following lines to /etc/php5/conf.d/xdebug.ini:

[xdebug]
; enable profiler
xdebug.profiler_enable=On
xdebug.profiler_output_name=profiler-%s-%t
xdebug.profiler_output_dir=/tmp/xdebug

Note: please make sure that the profiler output directory exists. Otherwise XDebug wouldn’t be able to write its profiler logs.

Finally, we restart the web server (Lighttpd in this case).

sudo /etc/init.d/lighttpd restart

Profiling PHP scripts

After restarting the webserver, we can start profiling PHP scripts. Simply open your browser and go to a PHP page. The XDebug log will be saved in /tmp/xdebug/ on your webserver. Download the log file to your PC, save it as “xdebug.log” and we are ready to start inspecting the log.

Creating a dot graph from the XDebug log

Most of the times, images say more than a thousand words. Therefore we will generate a dot graph from the profiling log. We will use the xdebugtoolkit to convert the XDebug cachegrind (= “xdebug.log“) to a dot graph. Make sure that the cachegrind file is in the parent directory of the XDebugToolkit. The following line will install the XDebugToolkit:

svn co http://xdebugtoolkit.googlecode.com/svn/tags/0.1.3/xdebugtoolkit/ xdebugtoolkit

Make sure you have installed the GraphViz packages (which contains dot):

sudo apt-get install graphviz

Now, we can finally start generating the dot graph:

cd xdebugtoolkit/
cg2dot.py ../xdebug.log | dot -Tpng -o ../xdebug.png

As I mentioned in the introduction, the XDebugToolkit uses a default threshold of 1%. You can change this threshold by adding -t THRESHOLD to the argument list. If you want to disable the threshold, use -t 0 and all called functions and their runtime will be displayed. However, disabling the threshold is not recommended, because it will generate a huge graph. The following line will generate a graph with all called the functions:

cg2dot.py -t 0 ../xdebug.log | dot -Tpng -o ../xdebug-all.png

Enjoy profiling your PHP scripts!

Update: Added Daniel’s notes to the How-to. Thanks Daniel!

Tags: , , ,

  1. Daniel says:

    You will need to install graphviz package via

    sudo apt-get install graphviz

    in order to use dot command for creating graphics.

    You should mention that xdebug.profiler_output_dir has to be existent.

Leave a Reply