PHP Performance Profiling

Techniques for learning where the bottlenecks are in your PHP-based Web application.

Due to the incredible growth of PHP in the last couple of years, it's now being used for tasks ranging from tiny scripts to large-scale Web applications. Some Web applications contain hundreds of thousands of lines of PHP code, and the fact that PHP can scale to these levels is a great testament to its design and the efficient Zend Engine that actually manages PHP code execution.

Of course, bigger and more complex projects result in more load on your servers, and when you throw a database into the mix you have even more potential performance bottlenecks to track. A typical scenario might be you've added a few new features to a Web application and now are seeing more server load and memory usage; thus, pages seem to load slower. What can you do? Maybe you can afford to throw bigger hardware at the problem, but even if that's a viable option, you also should find the parts of your code that are causing the slowdowns and optimize them.

A number of factors can affect the performance of a Web application, including Web server configuration, database performance, data structure, the application design and the implementation of the application. I'm going to assume you've already taken care of the first items, and now you want to iron out bottlenecks in your application implementation, that is, in the actual code. But how do you find the bottlenecks in the first place?

The answer is a technique known as performance profiling. 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.

Profiling Tools

A number of different tools have been developed to help with PHP performance profiling, including Benchmark (a PEAR project), DBG, Xdebug and Advanced PHP Debugger (another PEAR project).

If you're really serious about squeezing every last cycle out of the code, you should investigate all the benchmarking tools you can find, because they work in different ways and allow you to extract different kinds of information. For now, however, I'm going to concentrate on APD, the Advanced PHP Debugger.

APD is a debugger written in C by George Schlossnagle and Daniel Cowgill that loads as an extension to the Zend Engine. It works by hooking into the Zend internals and intercepting PHP function calls, allowing it to do things like measure function execution time, count function calls, perform stack backtraces and other funky things.

Installing APD

Currently, three main ways exist to install APD on a Linux system: grab the source and compile it yourself, use PEAR or use the Debian package. The latest source always is available from the APD Web site. Building and installing it isn't a hard process, but you need to make sure the various PHP development resources are installed on your system. For example, you need the PHP C headers, as well as a program called phpize that is used to prepare the package as a Zend extension. If you decide to go that route, make sure you follow the instructions in the README included with the source.

If you use PEAR, included with PHP4.3+, you can install PHP modules with minimal fuss. Once again, the full instructions are available on the APD Web site, which is part of the PEAR project. Assuming PEAR support is included in your version of PHP, getting it going should be as simple as typing pear install apd and answering a few questions.

Finally, for Debian users I maintain a .deb package of the latest version of APD. It's much too recent to be in Woody, but at the time of this writing it's in Sid (current Unstable) and should enter Sarge (current Testing) soon. You should be able to use apt-get install php4-apd to have everything done for you.

Whatever installation method you use, you should have the CGI version of PHP installed, because some of the command-line tools included with APD are written in PHP and need the parser to run. Personally, I run my Web servers with the Apache module version of PHP because it's much faster, but that doesn't matter. Simply install the CGI version of PHP as well and away you go. It doesn't need to affect your Apache module installation of PHP; they can live side by side quite happily.

You can use the phpinfo() function to confirm that APD installed and loaded properly. Create a file in your Web root that calls phpinfo(), and open it in a browser. The quickest way to do that probably is to type

        echo '<?php phpinfo() ?>' > info.php

in your Web root. When you access the file in your browser through your Web server (not by directly opening the file), it lists all the extensions that PHP has loaded. You should see APD listed somewhere on the page. If that worked fine, you're ready for the next step.



Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.


Valeriu Palos's picture

Great article! APD is a great way to have a local and accurate profiling of your code. That is very useful in large applications since you can concentrate on a specific subsection of the code for optimization without being distracted by other stuff.

However, many times I find that using XDebug+Valgrind is a very good idea, because it can give you a good picture of how your entire application behaves and what are its most expensive (or critical) modules. Also the XDebug method is also the most unobtrusive I think (you only need very minor changes in your code for this to work).

apd not working from within apache server

mukul's picture

HI, I am a newbie in APD, require your help to profile our PHP application. I have installed APD 1.0.1 on a Linux box and I am able to profile sample php scripts by firing them from shell as “php samplescript.php

Re: PHP Performance Profiling

Anonymous's picture

Another nice way to analyze the trace file is by doing
$ pprof2calltree -f [pprof tracefile]
and then open the resulting file with KCachgrind

installation of APD not so easy

misterff's picture

Installation of APD on my Debian system doesn't turn out to be so easy.
I just upgraded to Apache 2.2.0 and PHP5. I tried to install with the pear command, which gave me the following:

$ pear install apd
PHP version >= 5.0.0RC3-dev is required
apd: Dependencies failed

I also tried with:

$ apt-get install php5-apd

But php5-apd doesn't exist so I installed php4-apd which doesn't work.

Any idea's?

RE: installation of APD not so easy

Rene's picture

I had the same problem installing apd on my gentoo machine.
It won't help you, but after the problems I had, I found out the for Gentoo apd is in portage (pecl-apd)
There might be some dependency problems, but once you fix those, apd works.

APD is a pecl package

Stefano Canepa's picture

I found out that to install apd on PHP 5 you need to use pecl install apd command and not pear install apd

apd install errors

Anonymous's picture

has anyone solved this problem yet? tried pecl install apd, but no pecl command available. tried to install pecl, but instructions are too ambiguous -- Does apd REALLY require php v5.0 or is this a bug in the installation software?

After using "pecl install

Anonymous's picture

After using "pecl install apd" and putting where php.ini wanted it there were a few more php.ini directives to add before the module showed up on my phpinfo page. See:

Check the link

jostmart's picture

I managed to install efter looking trough this page: