[ACCEPTED]-Find out where your PHP code is slowing down (Performance Issue)-profiling

Accepted answer
Score: 40

I've used XDebug profiling recently in a similiar situation. It 4 outputs a full profile report that can be 3 read with many common profiling apps ( Can't 2 give you a list though, I just used the 1 one that came with slackware ).

Score: 9

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

Score: 6

Watch this presentation by Rasmus Lerdorf 8 (creator of PHP). He goes into some good 7 examples of testing PHP speed and what to 6 look for as well as some internals that 5 can slow things down. XDebug is one tool 4 he uses. He also makes a very solid point 3 about knowing what performance cost you're 2 getting into with frameworks.

Video: http://www.archive.org/details/simple_is_hard

Slides 1 (since it's hard to see on the video): http://talks.php.net/show/drupal08/1

Score: 4

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

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

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

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

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

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

Score: 3

Also You could use APD (Advanced PHP Debugger).

It's 3 quite easy to make it work.

$ php apd-test.php

$ pprofp -l pprof.SOME_PID

Trace for /Users/martin/develop/php/apd-test/apd-test.php
Total Elapsed Time = 0.12
Total System Time  = 0.01
Total User Time    = 0.07


         Real         User        System             secs/    cumm
%Time (excl/cumm)  (excl/cumm)  (excl/cumm) Calls    call    s/call  Memory Usage Name
--------------------------------------------------------------------------------------
71.3 0.06 0.06  0.05 0.05  0.01 0.01  10000  0.0000   0.0000            0 in_array
27.3 0.02 0.09  0.02 0.07  0.00 0.01  10000  0.0000   0.0000            0 my_test_function
 1.5 0.03 0.03  0.00 0.00  0.00 0.00      1  0.0000   0.0000            0 apd_set_pprof_trace
 0.0 0.00 0.12  0.00 0.07  0.00 0.01      1  0.0000   0.0000            0 main

There is a nice 2 tutorial how to compile APD and make profiling 1 with it : http://martinsikora.com/compiling-apd-for-php-54

Score: 2

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

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

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

Score: 2

Xdebug profile is definitely the way to 7 go. Another tip - WincacheGrind is good, but 6 not been updated recently. http://code.google.com/p/webgrind/ - in a browser 5 may be an easy and quick alternative.

Chances 4 are though, it's still the database anyway. Check 3 for relevant indexes - and that it has sufficient 2 memory to cache as much of the working data 1 as possible.

Score: 1

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

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

Score: 1

you can also try using the register_tick_function 6 function in php. which tells php to call 5 a certain function periodcally through out 4 your code. You could then keep track of 3 which function is currently running and 2 the amount of time between calls. then you 1 could see what's taking the most time. http://www.php.net/register_tick_function

Score: 0

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

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

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

Score: 0

I use a combination of PEAR Benchmark and log4php.

At the top 14 of scripts I want to profile I create an 13 object that wraps around a Benchmark_Timer object. Throughout 12 the code, I add in $object->setMarker("name");calls, especially around 11 suspect code.

The wrapper class has a destroy 10 method that takes the logging information 9 and writes it to log4php. I typically send 8 this to syslog (many servers, aggregates 7 to one log file on one server).

In debug, I 6 can watch the log files and see where I 5 need to improve things. Later on in production, I 4 can parse the log files and do performance 3 analysis.

It's not xdebug, but it's always 2 on and gives me the ability to compare any 1 two executions of the code.

Score: 0

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

More Related questions