PHP Profiler for a live system on top of Apache

2020-07-11 09:26发布

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.

标签: php profiling
9条回答
Emotional °昔
2楼-- · 2020-07-11 09:56

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);
查看更多
女痞
3楼-- · 2020-07-11 09:58

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.

查看更多
▲ chillily
4楼-- · 2020-07-11 10:02

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

查看更多
登录 后发表回答