wiki:PhpTop

Version 11 (modified by Vincent Caron, 11 years ago) (diff)

--

phptop

Summary

Source:

Licence: GPL v3

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 with hook into your existing code without any changes, record per query statistics via the standar error log, then collect and print reports with a standalone program. phptop itself is written in Perl and requires LWP.

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

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

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
                                       Hit     Time     User      Sys  Mem/hit  Mem_max  Inc/hit  Inc_max
http://foo.org/node/88                   5      0.3      0.2      0.0      4.3     21.5       15       78
http://bar.org/sv/cities                 5      1.2      0.2      0.1      4.9     24.5       25      125
http://foo.org/homepage/feed            15      0.7      0.5      0.1      4.2     21.2       14       74
http://bip.net/2007/08/24/               5      0.6      0.2      0.0      2.9     14.2       14       71
http://bip.net/feed/atom/                5      0.1      0.1      0.0      2.8     14.0       11       58      
http://baz.com/tag/megaloop/             5      0.2      0.1      0.0      2.4     12.0       11       56   
http://bip.net/                         10      0.7      0.4      0.1      2.9     14.2       14       70      
http://baz.com/spots/                    5      1.2      0.0      0.0      1.6      7.8       16       84  
http://baz.com/tag/international/        5      0.2      0.1      0.0      2.4     11.8       11       56   
http://baz.com/                         10      0.4      0.3      0.1      2.4     12.0       11       56   

Total                                  140      6.4      2.6      0.5

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

There is support for PHP CLI, but error_log does not actually log but prints to stderr. If you take care of redirecting your CLI invocations to a log file, phptop can work on it. Use either the shell construct php script.php 2>>error.log or either syslog with php script.php 2>&1 | logger. You cannot log to Apache ErrorLog files, simultaneous access by Apache and your CLI program may corrupt the log file.

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

Attachments (14)

Download all attachments as: .zip