<p>Here's my first question at SO.</p>

<p>I have a internal application for my company which I've been recently ask to maintain. The applications is built in PHP and its fairly well coded (OO, DB Abstraction, Smarty) nothing WTF-ish. </p>

<p>The problem is the applications is <strong>very slow</strong>.</p>

<p>How do I go about finding out what's slowing the application down? I've optimized the code to make very few DB queries, so I know that it is the PHP code which is taking a while to execute. I need to get some tools which can help me with this and need to devise a strategy for checking my code.</p>

<p>I can do the checking/strategy work myself, but I need more PHP tools to figure out where my app is crapping up. </p>

<p>Thoughts?</p>

[{'answer_id': 55737, 'author': 'Juan', 'author_id': 550, 'author_profile': 'https://Stackoverflow.com/users/550', 'pm_score': 6, 'selected': True, 'text': '<p>I\'ve used <a href="http://www.xdebug.org/docs/profiler" rel="noreferrer">XDebug profiling</a> recently in a similiar situation. It outputs a full profile report that can be read with many common profiling apps ( Can\'t give you a list though, I just used the one that came with slackware ). </p>\n'}, {'answer_id': 55783, 'author': 'Mark', 'author_id': 3915, 'author_profile': 'https://Stackoverflow.com/users/3915', 'pm_score': 0, 'selected': False, 'text': '<p>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. </p>\n\n<p>Process Explorer: <a href="http://technet.microsoft.com/en-us/sysinternals/bb896653.aspx" rel="nofollow noreferrer">http://technet.microsoft.com/en-us/sysinternals/bb896653.aspx</a>. </p>\n\n<p>ZDE includes a basic performance profiler that can show time spent in each function call during page requests.</p>\n'}, {'answer_id': 56754, 'author': 'Gary Richardson', 'author_id': 2506, 'author_profile': 'https://Stackoverflow.com/users/2506', 'pm_score': 0, 'selected': False, 'text': '<p>I use a combination of <a href="http://pear.php.net/package/Benchmark" rel="nofollow noreferrer">PEAR Benchmark</a> and <a href="http://incubator.apache.org/log4php/" rel="nofollow noreferrer">log4php</a>. </p>\n\n<p>At the top of scripts I want to profile I create an object that wraps around a <a href="http://pear.php.net/package/Benchmark/docs/latest/Benchmark/Benchmark_Timer.html" rel="nofollow noreferrer">Benchmark_Timer</a> object. Throughout the code, I add in <code>$object-&gt;setMarker("name");</code>calls, especially around suspect code.</p>\n\n<p>The wrapper class has a destroy method that takes the logging information and writes it to log4php. I typically send this to syslog (many servers, aggregates to one log file on one server). </p>\n\n<p>In debug, I can watch the log files and see where I need to improve things. Later on in production, I can parse the log files and do performance analysis.</p>\n\n<p>It\'s not xdebug, but it\'s always on and gives me the ability to compare any two executions of the code.</p>\n'}, {'answer_id': 56783, 'author': 'MattBelanger', 'author_id': 655, 'author_profile': 'https://Stackoverflow.com/users/655', 'pm_score': 3, 'selected': False, 'text': '<p>As Juan mentioned, xDebug is excellent.  If you\'re on Windows, <a href="http://sourceforge.net/projects/wincachegrind/" rel="noreferrer">WinCacheGrind</a> will let you look over the reports.</p>\n'}, {'answer_id': 57425, 'author': 'Patrick Hogan', 'author_id': 4065, 'author_profile': 'https://Stackoverflow.com/users/4065', 'pm_score': 3, 'selected': False, 'text': '<p>Watch this presentation by Rasmus Lerdorf (creator of PHP). He goes into some good examples of testing PHP speed and what to look for as well as some internals that can slow things down. XDebug is one tool he uses. He also makes a very solid point about knowing what performance cost you\'re getting into with frameworks.</p>\n\n<p>Video:\n<a href="http://www.archive.org/details/simple_is_hard" rel="noreferrer">http://www.archive.org/details/simple_is_hard</a></p>\n\n<p>Slides (since it\'s hard to see on the video):\n<a href="http://talks.php.net/show/drupal08/1" rel="noreferrer">http://talks.php.net/show/drupal08/1</a></p>\n'}, {'answer_id': 57544, 'author': 'Brendon-Van-Heyzen', 'author_id': 1425, 'author_profile': 'https://Stackoverflow.com/users/1425', 'pm_score': 1, 'selected': False, 'text': '<p>ifs its a large code base try apc if you\'re not already.</p>\n\n<p><a href="http://pecl.php.net/package/APC" rel="nofollow noreferrer">http://pecl.php.net/package/APC</a></p>\n'}, {'answer_id': 57641, 'author': 'RobS', 'author_id': 3907, 'author_profile': 'https://Stackoverflow.com/users/3907', 'pm_score': 2, 'selected': False, 'text': '<p>phpED (<a href="http://www.nusphere.com/products/phped.htm" rel="nofollow noreferrer">http://www.nusphere.com/products/phped.htm</a>) 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).</p>\n\n<p>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.</p>\n\n<p>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.</p>\n'}, {'answer_id': 59779, 'author': 'SeanDowney', 'author_id': 5261, 'author_profile': 'https://Stackoverflow.com/users/5261', 'pm_score': 1, 'selected': False, 'text': '<p>you can also try using the register_tick_function function in php. which tells php to call a certain function periodcally through out your code. You could then keep track of which function is currently running and the amount of time between calls. then you could see what\'s taking the most time. \n<a href="http://www.php.net/register_tick_function" rel="nofollow noreferrer">http://www.php.net/register_tick_function</a></p>\n'}, {'answer_id': 60202, 'author': 'Alister Bulman', 'author_id': 6216, 'author_profile': 'https://Stackoverflow.com/users/6216', 'pm_score': 2, 'selected': False, 'text': '<p>Xdebug profile is definitely the way to go.  Another tip - WincacheGrind is good, but not been updated recently.  <a href="http://code.google.com/p/webgrind/" rel="nofollow noreferrer">http://code.google.com/p/webgrind/</a> - in a browser may be an easy and quick alternative.</p>\n\n<p>Chances are though, it\'s still the database anyway.  Check for relevant indexes - and that it has sufficient memory to cache as much of the working data as possible.</p>\n'}, {'answer_id': 64438, 'author': 'giltotherescue', 'author_id': 8215, 'author_profile': 'https://Stackoverflow.com/users/8215', 'pm_score': 2, 'selected': False, 'text': '<p>There are many variables that can impact your application\'s performance. I recommend that you do not instantly assume PHP is the problem.</p>\n\n<p>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 <a href="http://www.google.com/search?q=php+code+accelerator" rel="nofollow noreferrer">PHP code accelerator</a>? One way to test whether the server is your bottleneck is to try running the application on another server.</p>\n\n<p>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). </p>\n\n<p>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 <a href="http://dev.mysql.com/doc/refman/5.0/en/explain.html" rel="nofollow noreferrer">EXPLAIN</a> 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 <a href="http://www.devshed.com/c/a/MySQL/Analyzing-Queries-for-Speed-with-EXPLAIN/" rel="nofollow noreferrer">such as this one</a> which explain how to use EXPLAIN.</p>\n\n<p>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.</p>\n\n<p>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.</p>\n'}, {'answer_id': 15717173, 'author': 'Fedir RYKHTIK', 'author_id': 634275, 'author_profile': 'https://Stackoverflow.com/users/634275', 'pm_score': 2, 'selected': False, 'text': '<p>Also You could use <a href="http://pecl.php.net/package/apd" rel="nofollow">APD</a> (Advanced PHP Debugger). </p>\n\n<p>It\'s quite easy to make it work.</p>\n\n<pre><code>$ php apd-test.php\n\n$ pprofp -l pprof.SOME_PID\n\nTrace for /Users/martin/develop/php/apd-test/apd-test.php\nTotal Elapsed Time = 0.12\nTotal System Time  = 0.01\nTotal User Time    = 0.07\n\n\n         Real         User        System             secs/    cumm\n%Time (excl/cumm)  (excl/cumm)  (excl/cumm) Calls    call    s/call  Memory Usage Name\n--------------------------------------------------------------------------------------\n71.3 0.06 0.06  0.05 0.05  0.01 0.01  10000  0.0000   0.0000            0 in_array\n27.3 0.02 0.09  0.02 0.07  0.00 0.01  10000  0.0000   0.0000            0 my_test_function\n 1.5 0.03 0.03  0.00 0.00  0.00 0.00      1  0.0000   0.0000            0 apd_set_pprof_trace\n 0.0 0.00 0.12  0.00 0.07  0.00 0.01      1  0.0000   0.0000            0 main\n</code></pre>\n\n<p>There is a nice tutorial how to compile APD and make profiling with it : <a href="http://martinsikora.com/compiling-apd-for-php-54" rel="nofollow">http://martinsikora.com/compiling-apd-for-php-54</a></p>\n'}, {'answer_id': 30209716, 'author': 'Asuquo12', 'author_id': 4238757, 'author_profile': 'https://Stackoverflow.com/users/4238757', 'pm_score': 0, 'selected': False, 'text': '<p>You can also look at the <a href="http://www.haproxy.org/" rel="nofollow">HA Proxy</a> or any other load balancing solution if your server degraded performance is the cause of the application slow processing. server. </p>\n'}]
['https://Stackoverflow.com/questions/55720', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/3983/']