The Developer Day | Staying Curious

TAG | profiling

Aug/07

20

Profiling PHP applications

A while ago I was in need to profile one of our PHP applications. I needed to be able to see execution times, memory usage and some kind of a graphical interface to view it all. I remembered a PHP profiler debugger tool named Xdebug. Xdebug works as a PHP Zend extension. If you are a windows user installing it on your machine is very straightforward:

Download the appropriate windows module from xdebug.org. Edit your php.ini file by adding the following line to the extensions section:

zend_extension_ts=”path/xdebug.dll”

Restart your web server and look for a module named Xdebug in your phpinfo() output. If you’re lucky enough you’ll find it there.

After that you need to configure xdebug extension.

;enables the profiler
xdebug.profiler_enable=1;
;specifies profiler output files directory
xdebug.profiler_output_dir=”c:/php/xdebug/”
;specifies profiler output file name pattern.
xdebug.profiler_output_name=”cachegrind.%u.out”
;enables auto tracing on every script startup
xdebug.auto_trace=1
;specifies trace files output direcctory
xdebug.trace_output_dir=”c:/php/xdebug/”
;specifies trace output file name pattern.
xdebug.trace_output_name=”trace.%u”
;enable memory usage tracking
xdebug.show_mem_delta=1

Restart your web server again. Run any of your php applications and you should find files in your specified directories:

trace.1187359095_993350.xt
cachegrind.1187366421_815700.out

Now you can view cachegrind files with WinCacheGrind. The application I tested was spawning few processes of it’s own and the profile information was in one file by default. But apparently something didn’t want to work so I separated each process to a separate file by adding %u to the output file name pattern.

The most interesting part for me was memory usage which you can find in the .xt files. It shows the total memory used after every line of code executed and a delta’s between executions. A tool for viewing/analyzing these trace files would be nice. Did not look for one though. All in all Xdebug is useful to find those nasty bottlenecks. It also has other nice features that help you to daily develop your apps. Nice thing I didn’t try yet is the “Code coverage” test. It might help me find some unused code in our applications.

, , , Hide

Find it!

Theme Design by devolux.org