Find out where your PHP code is slowing down (Performance Issue)

前端 未结 12 709
无人共我
无人共我 2020-12-04 18:25

Here\'s my first question at SO.

I have a internal application for my company which I\'ve been recently ask to maintain. The applications is built in PHP and its fai

相关标签:
12条回答
  • 2020-12-04 18:45

    We use Zend Development Environment (windows). We resolved a memory usage spike yesterday by stepping through the debugger while running Process Explorer to watch the memory/cpu/disk activity as each line was executed.

    Process Explorer: http://technet.microsoft.com/en-us/sysinternals/bb896653.aspx.

    ZDE includes a basic performance profiler that can show time spent in each function call during page requests.

    0 讨论(0)
  • 2020-12-04 18:46

    As Juan mentioned, xDebug is excellent. If you're on Windows, WinCacheGrind will let you look over the reports.

    0 讨论(0)
  • 2020-12-04 18:54

    ifs its a large code base try apc if you're not already.

    http://pecl.php.net/package/APC

    0 讨论(0)
  • 2020-12-04 18:55

    There are many variables that can impact your application's performance. I recommend that you do not instantly assume PHP is the problem.

    First, how are you serving PHP? Have you tried basic optimization of Apache or IIS itself? Is the server busy processing other kinds of requests? Have you taken advantage of a PHP code accelerator? One way to test whether the server is your bottleneck is to try running the application on another server.

    Second, is performance of the entire application slow, or does it only seem to affect certain pages? This could give you an indication of where to start analyzing performance. If the entire application is slow, the problem is more likely in the underlying server/platform or with a global SQL query that is part of every request (user authentication, for example).

    Third, you mentioned minimizing the number of SQL queries, but what about optimizing the existing queries? If you are using MySQL, are you taking advantage of the various strengths of each storage system? Have you run EXPLAIN on your most important queries to make sure they are properly indexed? This is critical on queries that access big tables; the larger the dataset, the more you will notice the effects of poor indexing. Luckily, there are many articles such as this one which explain how to use EXPLAIN.

    Fourth, a common mistake is to assume that your database server will automatically use all of the resources available to the system. You should check to make sure you have explicitly allocated sufficient resources to your database application. In MySQL, for example, you'll want to add custom settings (in your my.cnf file) for things like key buffer, temp table size, thread concurrency, innodb buffer pool size, etc.

    If you've double-checked all of the above and are still unable to find the bottleneck, a code profiler like Xdebug can definitely help. Personally, I prefer the Zend Studio profiler, but it may not be the best option unless you are already taking advantage of the rest of the Zend Platform stack. However, in my experience it is very rare that PHP itself is the root cause of slow performance. Often, a code profiler can help you determine with more precision which DB queries are to blame.

    0 讨论(0)
  • 2020-12-04 18:58

    phpED (http://www.nusphere.com/products/phped.htm) also offers great debugging and profiling, and the ability to add watches, breakpoints, etc in PHP code. The integrated profiler directly offers a time breakdown of each function call and class method from within the IDE. Browser plugins also enable quick integration with Firefox or IE (i.e. visit slow URL with browser, then click button to profile or debug).

    It's been very useful in pointing out where the app is slow in order to concentrate most coding effort; and it avoids wasting time optimising already fast code. Having tried Zend and Eclipse, I've now been sold on the ease of use of phpED.

    Bear in mind both Xdebug and phpED (with DBG) will require an extra PHP module installed when debugging against a webserver. phpED also offers (untried by me) a local debugging option too.

    0 讨论(0)
  • 2020-12-04 19:00

    You can also look at the HA Proxy or any other load balancing solution if your server degraded performance is the cause of the application slow processing. server.

    0 讨论(0)
提交回复
热议问题