PHP Performance Profiling With APD On Windows

December 27th, 2007 by Stephen Cronin (Please wait) [Shortlink]

When writing code, its important to keep system performance in mind. No matter how useful your software is, if it places too much stress on the server, it won’t be used. How can you tell if your code is efficient? Well, experience helps, but it also pays to have good tools.

Most of the code I write is fairly simple and should only place minimal load on the server, but I wanted to make sure that its as efficient as possible. I went looking for a PHP performance profiling tool to help me.

My search led me to Jonathan Oxer’s PHP Performance Profiling article. This mentions several tools, but focuses on Advanced PHP Debugger (APD). This looked good, so I tried to give it go. I eventually got it working, but it wasn’t easy – the problem was that I wanted to run it on Windows XP.

The focus of this post is how to get APD running on Windows. If you don’t write PHP code or use Windows, then this article is probably not for you!

What Is Performance Profiling?

Before I get into it, I’m aware that some of you will be asking “What’s performance profiling?” Here’s Jonathon’s description, which says it perfectly:

Performance profiling runs your code in a controlled environment and returns a report listing such statistics as time spent within each function, how long each database query takes and how much memory has been used.

By doing performance profiling on your code, you quickly can see where you may be wasting time with slow database queries or inefficient code. Having this information then allows you to spend your time tuning PHP and SQL where it needs it most. No more guessing what’s going on internally: performance profiling gives you hard figures.

APD is a tool which helps you to do this.

The Problem With Installing APD on Windows

To run APD in the Windows environment, you need the php_apd.dll file. Unfortunately, the installation package (from the APD site) doesn’t include this file. The readme notes indicate that you need to compile APD yourself.

Well, I don’t have a C++ compiler installed and I wasn’t interested in downloading one and messing around with setting it up to compile PHP. That would take more time than I was willing to spend.

Surely the compiled file must be available somewhere on the Internet? Well, yes, but information on where to get it is buried amongst an avalanche of pages with no information (people asking “where do I get“, but no answer) or pages with misleading advice, leading to dlls that don’t work.

So Where DO I Get The Php_apd.dll File?

The php_apd.dll file is contained in the PECL extension file.

If you’re using the latest version of PHP, go to the Windows Binaries section of the PHP Downloads page and download the PECL 5.x.x Win32 binaries file (where 5.x.x is the latest version). The php_apd.dll file is in this zip file.

If you’re using an older version of PHP5, go to the Unsupported Historical Releases page, locate the version of PHP you are using and download Collection of PECL modules for PHP 5.x.x file (where 5.x.x is the version you’re using). The php_apd.dll file is in this zip file.

If you’re using PHP4, I’m afraid I can’t tell you where to find the file. There is no separate PECL download for PHP4 versions, because most PECL modules were included with PHP4 (rather than in a separate file as with PHP5). Unfortunately, it seems the APD extension wasn’t one of those included.

Setting APD Up

Once you’ve got the php_apd.dll file, here’s how to set APD up:

First, make sure you have the PEAR extension installed, as APD uses its Console\Getopt.php script. If it is installed, there will be files and folders in the C:\php\pear folder (assuming PHP is installed in C:\php). If the folder is empty (there may be one file) or doesn’t exist, you need to install PEAR.

Installing PEAR should be as simple as running go-pear.bat in the C:\php folder. This asks a few questions, connects to the PEAR site and downloads the appropriate files. If PEAR support is not included in your version of PHP, you will have to download it from the PEAR website and install it yourself.

Second, copy the php_apd.dll file to the C:\php\ext folder – assuming this is where your extensions live. If you are unsure where they live, locate your php.ini file and check the extension_dir setting. This is the folder you need to copy php_apd.dll to.

Third, edit php.ini and add the following to the Dynamic extensions section:

extension=php_apd.dll

I’ve seen it reported that you need to add:

zend_extension = c:\php\php_apd.dll
apd.dumpdir = .
apd.statement_trace = 1

but I didn’t need to, probably because I’m not using the Zend extension.

To check if APD is loaded, run the phpinfo() command. If you’re not sure how to do this, read the following tutorial on creating a page that displays phpinfo(). If you find an APD section somewhere on the page, it’s loaded.

Using APD

For more information on using APD, see Jonathon’s article, but here’s what I did:

1. I added the following to the top of the script I wanted to profile:

apd_set_pprof_trace('C:\temp');

    where C:\Temp is the path I want the output file to be created in.

2. I ran the script once to create the output file.

3. I opened the command prompt, changed to the directory with the
    output file and typed the following:

c:\php\php.exe pprofp -u <filename> >output.txt

    (you need to replace <filename> with the name of the output file).

If you get an error saying there is no pprofp file, try downloading a copy of mine and put it into the folder containing the output file. Note: It should have no extension.

There you go – PHP performance profiling… The information is in output.txt.

APD Not For Profiling WordPress Plugins

I was hoping to use APD to profile my WordPress plugins. Unfortunately, its not suitable for this, because plugins hook into the WordPress core – so APD picks up everything else that’s going on as well. In hindsight, I should have realised this before I tried!

APD could be used to profile the performance of WordPress as a whole. People are becoming more aware of WordPress performance and are tweaking it to improve performance. Using APD while making changes could provide valuable insight into the effectiveness of the changes.

It’s also worth mentioning Lucia’s PluginHogDetector plugin for WordPress. This works by reporting how long it takes to process various parts of the page. To test a plugin (or change), simply compare the times before and after the plugin is activated (or the change is made). This isn’t as exact as performance profiling, lacking a breakdown of functions, how many times they’re called, how much memory they use, etc, but its simple and useful.

The Final Word

Evaluating the performance of your code is very important. I have a few projects on the go where APD will help me do that, so it was worth the struggle to get it working.

If you have any other tips for measuring PHP performance let me know!

Like this article? Plus One it!

Tags: , , , ,

There are 26 reponses to this article. Visit the comments page.

Leave a Reply

Anti-Spam Quiz:


Stephen Cronin

is Manager of Online Service Delivery at a Queensland Government department & has been a freelance WordPress developer/consultant since 2007
*Content on this site is my own and is not related to my employer

Hire me - I'm expensive, but I'm very good!

WordPress Plugins

Want a Custom WordPress plugin? See my Services page.

Greasemonkey Scripts

Visit my home page at Userscripts.org.