tags:

views:

698

answers:

6

I have a PHP website on a Apache server, and I would like to know if there are tools and other ways I can profile this to find bottlenecks on the code. What I need to know is what functions are taking long to process, etc.

Something like gprof, except for PHP on live apache server.

What are other ways to find bottlenecks in a PHP system.

A: 

You can use phpdebug for this job. It's a php extension.

Fu86
+3  A: 

Try XDebug ( http://www.xdebug.org/ ) you can trigger it with a get-parameter during your debugging-session. This will create cachegrind-files which you can check within KCacheGrind or WinCacheGrind (this first is much better)...

pagid
+1  A: 
Paul Dixon
+1 up vote - Xdebug is the best FOSS option out there, but if you start adding servers, Zend Platform is going to have additional tools to bring to bear for performance optimization.
Pro777
Zend Platform is excellent if you manage multiple servers, but it's a bit old and tough to install/configure. The successor, Zend Server, isn't feature-complete, but is quite good already and much easier to install (RPM or DEB packages). Also, the Community Edition is free.
Martijn Heemels
A: 

if you have a very targeted area to look at, you may want to try sprinkling these around your code:

$f_timeStart=microtime(true);
$f_timeLast=$f_timeStart;


error_log(sprintf("%'08.5f",(microtime(true)-$f_timeLast)).' - '.sprintf("%'05.2f",(microtime(true)-$f_timeStart)).' secs - '.'01 before xyz()'."\n", 3, '/var/tmp/my-errors.log');
$f_timeLast=microtime(true);
xyz();
error_log(sprintf("%'08.5f",(microtime(true)-$f_timeLast)).' - '.sprintf("%'05.2f",(microtime(true)-$f_timeStart)).' secs - '.'01 after xyz()'."\n", 3, '/var/tmp/my-errors.log');
$f_timeLast=microtime(true);
KM
A: 

I would suggest against building your own profiler. There are several excellent profilers freely available that will give you extensive detail and ease of use. I think your best time investment is in the following combination. We use this at the web developent company I work for, and are very pleased with it:

  1. The Zend Server php stack:
    You can use the free Community Edition, and choose which parts of the stack you want to install. We installed only the PHP part, and rely on the Apache and MySQL from the Linux distro. Zend Server provides a Debugger extension, a code optimizer (for a slight speed boost), a bytecode cache (for a substantial speed boost) and a nice GUI for managing PHP settings. The commercial version provides much more. Installation in Linux is easy via RPM or DEB packages.

  2. To use the Debugger extension you need an IDE:
    Install Zend Studio which is an excellent PHP IDE (check the features page), and makes debugging and profiling very easy. No need to make cachegrind files, or other multistep processes, but just click Profile in the toolbar in Firefox and Studio starts profiling that page. The detail and ease of use are huge.

Maybe I'm sounding like a Zend salesman, and maybe this sounds like more than you need, but I'm just a PHP developer who is very happy with the tools he uses. I think it's time well spent to start using Studio, but the combination makes it great and Server will even speed up your live server quite a bit. In my opinion this combo is simply the best PHP development environment currently available. Check out the demo videos. There's one on profiling too.

Martijn Heemels
A: 

If you are running on OpenSolaris, consider dtrace. The biggest advantage probably is that you can also probe into other layers like Apache, Mysql. dtrace has low overhead and you can selectively enable only the probes that you want to monitor.

CruiZen