wiki:PhpTop

phptop

  • Install Debian package with source deb http://deb.bearstech.com phptop/ (Etch, Lenny, Squeeze)
  • Download phptop-0.5.3.tar.gz
  • Browse at source:/phptop/
  • Documentation (man page): PhpTopMan
  • Checkout with svn co svn://forge.bearstech.com/forge/phptop

phptop prints per query and average metrics comparable to the 'time' program or shell builtin: wallclock, user and system CPU time along with memory and other ressource usages.

It uses a a few lines of PHP which hook into your existing code without any changes, records per query statistics via the standard error log, then collects and prints reports with a standalone program. phptop itself is written in  Perl.

phptop is licensed under the  GNU GPL 3.0.

Usage

On a server with several distinct vhost and applications, anlyzing last 15 minutes and sorting by average memory usage per query:

server:~$ phptop -t 15 -s mem
URL                                    Hit     Time     User      Sys >Mem/hit  Mem_max
http://foo.org/node/88                   9     14.7      7.4      2.7     39.2     39.5
http://bar.org/sv/cities                66    116.1     53.0     20.7     39.8     40.5
http://foo.org/homepage/feed             3      0.7      0.4      0.1     14.0     14.0
http://bip.net/2007/08/24/              22      5.1      3.1      0.5     14.0     14.0
http://bip.net/feed/atom/               16     24.1      2.3      0.3     15.0     16.8
http://baz.com/tag/megaloop/            98     16.0     11.9      1.8     13.8     14.0
http://bip.net/                         13      6.5      1.6      0.2     13.7     14.5
http://baz.com/spots/                    6      2.6      1.1      0.1     14.5     14.5
http://baz.com/tag/international/        5      1.3      0.7      0.1     14.8     15.5
http://baz.com/                          1      0.5      0.2      0.0     12.0     12.0
Total (from last 15 min)              8078   8826.9    772.5    132.7

You may also output several reports sorted along different columns in one go, use the -s <column option multiple times.

Finally, the -o html option provides, well obviously, an HTML output:

Setup

It can be globally activated on a LAMP server and requires little ressources and a single line configuration change in your php.ini. For instance:

server:~# echo auto_prepend_file=/path/to/phptop_hook.php >>/etc/php/php.ini
server:~# apache2ctl reload

If you install the Debian package, the setup is fully automatic (implies an 'Apache reload').

Wait a few minutes to collect enough data then run phptop.

phptop expects that PHP standard error is logged to your web server error log files (or at least via any logging mechanism). This should be the case on any sane LAMP setup, otherwise we suggest you fix it with log_errors=on in your php.ini.

Once the 'recorder hook' is installed, running phptop does not need specific privileges besides read access to the relevant web error log files.

Notes

phptop requires at least PHP >= 4.3.10 or PHP >= 5.0.3.

phptop works with the regular embedded module and the CGI/FastCGI mode. There is currently no way to distinguish those different PHP calling contexts (also known as SAPI's). It is silent in CLI mode since it cannot reach a standard error log to record system measures.

The CPU user and system time are collected with the  getrusage system call, with exclusion of child processes (thus forks, shell_exec and such are not accounted). The precision with Linux is limited by the kernel scheduler frequency: mostly 100Hz with 2.4 series (10ms precision) and 250Hz with 2.6 series (4ms precision). phptop is not a tracing or debugging tool, it is used to drill down among expensive queries and 10ms precision is considered sufficient for its goal. Other kernels than Linux have not been tested.

Attachments