PHP Performance Profiling

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

Your First Test

At this point you should have APD installed on your server,
along with all the other usual stuff--Apache, MySQL or Postgres,
your PHP scripts themselves, and whatever else you need to run your
Web application.

Now, pick a PHP script in your Web application for a test
run, and open it in an editor. In my example here, I use the file
src/webmail.php from
Squirrelmail, a
popular Web mail system written in PHP. The basic procedure is to
invoke special APD functions inside your PHP script that tell APD
to do its stuff. Right at the top of your PHP script, put in a call
to apd_set_pprof_trace(), like this:

<?php
apd_set_pprof_trace();
...etc

What this does is tell APD to start a trace on execution of
your script at that point and dump it out to disk in a predefined
location, which is set in the php.ini file using the apd.dumpdir
directive. If you used my Debian package, the location should be
set to /var/log/php4-apd/; if you used PEAR or compiled from
source, please check the included documentation.

Gathering Some Data

Now that your script is set up and ready to profile, load it
in a Web browser. It should run exactly as before, and you
shouldn't see any difference at all from the client side; the page
loads as it always has.

What is different this time is a pprof tracefile has been
written out to the dumpdir previously defined. A pprof tracefile is
a text file that contains a machine-parsable summary of how your
PHP was processed, and it is named something like pprof.25802. The
number is the process ID of the Web server process that handled the
request. You can take a quick look through the tracefile, but at
this point it won't mean much to you. For my example using
Squirrelmail, it looks something like this:

and so on for several more pages. Essentially it's a
step-by-step record of what the Zend engine did as it processed
your script, but the format is not designed for direct human
consumption. Instead, it's an intermediate log that can be
processed to generate nice reports.

Interpreting The pprof Tracefile

APD comes with a little shell script written in PHP, called
pprofp, that can be run from the command line to parse the pprof
tracefile and give you a human-readable report. To use it, run
pprofp and pass it an option and the path of the tracefile, like
this:

pprofp -u /var/log/php-apd/pprof.25802

This command prints out a function call summary that should
look something like this:

The report above shows time and memory usage on a
per-function basis, sorted by user-time as directed by the -u
option. The first few columns are execution time in seconds. The
Calls column is a count of the number of times that function was
executed by the script. secs/call is the average execution time of
each call to that function, while cumm s/call is the cumulative
time spent on that function. Then it lists memory usage and finally
the name of the function itself. Notice that function call reports
are truncated to 15 functions by default.

If your script has some major performance problems, here is
where they should start to become glaringly obvious. Do you have a
slow function that's called many times? Better take a close look at
it. Are you doing lots of SQL queries? You can see them here right
away.

Comments

Comment viewing options

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).

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

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.

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?