Tracking PHP Scripts execution time with (apd and
xdebug) to tackle bottlenecks in website performance
Yesterday while reading a book on PHP 5 I found a chapter which was
talking about scripts performance.
The book included quite a lot information on tracking performance,
however what was personally interesting for me was two of the
suggested ways to track performance through
APD (Advanced PHP Debugger)
or alternatively using the
xdebug
.
Herein I include little information about apd:
APD is a full-featured PHP profiler/debugger that is loaded as a
zend_extension. It aims to be an analog of C's gprof or Perl's
Devel::DProf.
More information on
how to install configure
and use the advanced php debugger can be found here
Anyways for the impatient and lazy to read the apd docs, here is a
few steps that would install and enable APD on your Linux or BSD
powered system.
1. Install the apd using pecl (php extension community
library)
debian:~# pecl install apd
you should see some debug here ....
Build process completed successfully
Installing '/usr/lib/php5/20060613/apd.so'
2. Edit your php.ini to include the newly installed apd.so
library:
- In debian the php.ini would be located in
/etc/php5/apache2/php.ini, so you will need to edit
/etc/php5/apache2/php.ini , add in it:
zend_extension = "/usr/lib/php5/20060613/apd.so"
apd.dumpdir="/var/tmp/apd"
Afterwards execute the following commands:
debian:~# mkdir /var/tmp/apd
debian:~# chmod 1777 /var/tmp/apd
debian:~# /usr/sbin/apache2ctl restart
To enable a profile of the time required for a certain php script
to execute, you will have to:
- include apd_set_pprof_trace(); in the beginning of the
questionable script.
If you would like to enable the apd system wide you will have to
put the
apd_set_pprof_trace(); function into a new file and
include in your php.ini the
auto_prepend_file directive
pointing to the newly created file containing the
apd_set_pprof_trace()
To see some statistics concerning apd execution times execute the
command:
debian:~# pprofp -O 9 -u
/var/tmp/apd/pprof.25515
The above command is about to show you which php fucntions has
consumed the maximum amount of server CPU time, so havind that in
mind laters you can substitute the cpu heavy php functions with a
less cpu intensive ones.
Note that the
pprof.25515 is an example file you should
check in your
/var/tmp/apd/ to find out the pprof name that
apd has created.
APD has plenty of other good function trace and general php time
debug functions so for more check the manuals.
An alternative way to track your php functions cpu usage and
execution times is using
xdebug .
Xdebug is available for both Linux and Windows OS
platforms.
On Debian Linux xdebug is even available as a php 5 module in deb
package.
The install of xdebug on Debian is directly through apt-get package
manager:
debian:~# apt-get install php5-xdebug
If you're not on Debian you can use the pecl system to install
xdebug:
Issue:
shell:# pecl install xdebug
Next to the pecl install you will find some futher instructions on
how to enable the newly installed xdebug module with the
zend_extension variable
Note: You should ignore any prompts to add "extension=xdebug.so"
to php.ini - this will cause problems.
Complete
xdebug
installation instructions are available on xdebug's are
here
To enable php scripts profiling with xdebug you will need to add in
your
php.ini:
xdebug.profiler_enable = 1
xdebug.profiler_output_dir=/var/log/php-profiler
Make sure your
xdebug.profiler_output_dir is an existing
directory, if you want to use the one shown in the example above,
create the dir with command:
shell:# mkdir /var/log/php-profiler
shell:# chown www-data:www-data
/var/log/php-profiler
Here I use the www-data user and group which are the user and group
used on Debian Linux, if your apache user is different change it to
the appropriate user.
Here I want get into details on
more about xdebug php code
profiling check here
Xdebug profile dumps can even be nicely analysed and visualized in
X with
Kcachegrind
.
Probably there is much, much more left unsaid on the topic of php
execution code profiling however I'll stop here and leave the topic
article for discussion and suggestions by my dear readers
:)