Compiling APD for PHP 5.4

APD (Advanced PHP Debugger) is a very useful PHP module for profiling and testing PHP scripts. I don't know why it's not maintained any more and therefore you can't even install it easily using PECL. With PHP 5.4 even more problems occurred and it's really tedious work googling how to fix each of them.

I know, I could use just Xdebug, but I prefer to profile CLI scripts using APD because it's very similar to Python's -m cProfiler and you can quickly check the generated profiles with pprofp.

But at the end I succeed and APD really works with PHP 5.4. Here are a couple of steps (just note that this is for PHP 5.4, it may be different for later versions):

Obtaining APD

  1. Download the latest version of APD from (it's probably 1.0.1).

  2. Unpack it (in terminal it's for instance tar -xvzf apd-1.0.1.tgz).

Compiling APD

  1. At first just make sure your phpize is the one that comes with PHP 5.4, because you might have more versions of PHP installed on your system and PHP 5.4 probably won't be the default one. So, this is what it looks like when I try phpize -v on PHP 5.4.5:

    Configuring for:
    PHP Api Version:         20100412
    Zend Module Api No:      20100525
    Zend Extension Api No:   220100525

    If you see something like PHP Api Version: 2009XXXX, then you're probably using phpize for PHP 5.3.

  2. Run ./configure

  3. Finally you can run make and see what happens. Probably it will end throwing some warnings and 1-3 errors. (I think 1 error on PHP 5.3 and 3 errors on PHP 5.4)

Fixing APD source code

After running make compilation will probably fail with these errors (tested on PHP 5.4.5, with PHP 5.3 you'll see probably just one error). You can fix all of them manually or by downloading and applying this diff file (note: this patch should be compatible with both PHP 5.3 and PHP 5.4).

If you're familiar with using patch files on unix based systems it's probably easier to download diff file and apply it (this also fixes APD version in php_apd.h:

patch < apd-php54.diff


Before you can start using APD, run make install. This should copy the compiled .so library into your PHP extensions directory (if it didn't, do it manually). Then just enable it as a zend_extension in your php.ini and eventually set apd.dumpdir but it's not necessary.

Just to proof that APD really works we can try some simple PHP script like this

// apd-test.php

$someArray = array();

function my_test_function($index, $array) {
     if (in_array($index, $array)) {
          // something
     } else {
          // blah

for ($i=0; $i < 10000; $i++) {
     my_test_function($i, $someArray);
     $someArray[$i] = true;

and run it php apd-test.php. It should create a file in the same directory, something like pprof.SOME_PID.
That's nice, the last thing is to view what it generated. APD comes with script called pprofp, it's maybe a good idea to make a symlink or copy it into your /usr/bin.
Then call pprofp -l pprof.SOME_PID and watch the profiler output:

Trace for /Users/martin/develop/php/apd-test/apd-test.php
Total Elapsed Time = 0.12
Total System Time  = 0.01
Total User Time    = 0.07

         Real         User        System             secs/    cumm
%Time (excl/cumm)  (excl/cumm)  (excl/cumm) Calls    call    s/call  Memory Usage Name
71.3 0.06 0.06  0.05 0.05  0.01 0.01  10000  0.0000   0.0000            0 in_array
27.3 0.02 0.09  0.02 0.07  0.00 0.01  10000  0.0000   0.0000            0 my_test_function
 1.5 0.03 0.03  0.00 0.00  0.00 0.00      1  0.0000   0.0000            0 apd_set_pprof_trace
 0.0 0.00 0.12  0.00 0.07  0.00 0.01      1  0.0000   0.0000            0 main

And that's it. You should be good to go with APD on PHP 5.4.

blog comments powered by Disqus