views:

3563

answers:

4

What's the easiest way to profile a PHP script?

I'd love tacking something on that shows me a dump of all function calls and how long they took but I'm also OK with putting something around specific functions.

I tried experimenting with the microtime function:

$then = microtime();
myFunc();
$now = microtime();

echo sprintf("Elapsed:  %f", $now-$then);

but that sometimes gives me negative results. Plus it's a lot of trouble to sprinkle that all over my code.

+2  A: 

I like to use phpDebug for profiling. http://phpdebug.sourceforge.net/www/index.html

It outputs all time / memory usage for any SQL used as well as all the included files. Obviously, it works best on code that's abstracted.

For function and class profiling I'll just use microtime() + getmemoryusage() + getpeakmemory_usage().

Eric Lamb
+16  A: 

You want xdebug I think. Install it on the server, turn it on, pump the output through kcachegrind (for linux) or wincachegrind (for windows) and it'll show you a few pretty charts that detail the exact timings, counts and memory usage (but you'll need another extension for that).

It rocks, seriously :D

mercutio
I found this a lot easier to implement than the APD solution. But maybe that's because for some reason APD didn't compile properly on my system. Also kcachegrind's charts were as pretty as promised.
wxs
hi mercutio, which extension is it exactly that enables memory usage in the xdebug cachegrinds? I can't for the life of me get this to work.
EvilPuppetMaster
@EvilPuppetMaster, you need to compile php with --enable-memory-limit or use a more modern php version. See http://www.xdebug.org/docs/basic#xdebug_memory_usage
mercutio
xdebug + webgrind quickly became my weapon of choice for quick and easy profiling. http://code.google.com/p/webgrind/
xkcd150
+12  A: 

The PECL APD extension is very easy to use and gives nice results. http://www.php.net/apd

<?php
apd_set_pprof_trace();

//rest of the script
?>

All you need to do afterward is to parse the generated file using pprofp.

Example output:

Trace for /home/dan/testapd.php
Total Elapsed Time = 0.00
Total System Time  = 0.00
Total User Time    = 0.00


Real         User        System             secs/    cumm
%Time (excl/cumm)  (excl/cumm)  (excl/cumm) Calls    call    s/call  Memory Usage Name
--------------------------------------------------------------------------------------
100.0 0.00 0.00  0.00 0.00  0.00 0.00     1  0.0000   0.0009            0 main
56.9 0.00 0.00  0.00 0.00  0.00 0.00     1  0.0005   0.0005            0 apd_set_pprof_trace
28.0 0.00 0.00  0.00 0.00  0.00 0.00    10  0.0000   0.0000            0 preg_replace
14.3 0.00 0.00  0.00 0.00  0.00 0.00    10  0.0000   0.0000            0 str_replace
Vincent
I just want to mention again how helpful this has been!
Mark Biek
A: 

For benchmarking, like in your example, I use the pear Benchmark package. You set markers for measuring. The class also provides a few presentation helpers, or you can process the data as you see fit.

I actually have it wrapped in another class with a __destruct method. When a script exits, the output is logged via log4php to syslog, so I have a lot of performance data to work from.

Gary Richardson