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

29,085

Solution 1

I've used XDebug profiling 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 ).

Solution 2

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

Solution 3

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.

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

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

Solution 4

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.

Solution 5

Also You could use APD (Advanced PHP Debugger).

It's 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 tutorial how to compile APD and make profiling with it : http://martinsikora.com/compiling-apd-for-php-54

Share:
29,085
Rushi
Author by

Rushi

Updated on May 13, 2020

Comments

  • Rushi
    Rushi about 4 years

    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 fairly well coded (OO, DB Abstraction, Smarty) nothing WTF-ish.

    The problem is the applications is very slow.

    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.

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

    Thoughts?

  • Rushi
    Rushi over 15 years
    I have a windows machine lying around that I can put to good use
  • Rushi
    Rushi over 15 years
    Even though I'm going with XDebug i will check out Pear Benchmark. I can use it in other projects
  • Rushi
    Rushi over 15 years
    You're right too. I actually looked at the database end of things and they seemed fine. Apache was an issue, but it was fixed. I will install an op-code cache program too. But still profiling your app helps improve general programming anyways.
  • blueyed
    blueyed about 14 years
    KCacheGrind (KDE/Linux) is much better.
  • Félix Adriyel Gagnon-Grenier
    Félix Adriyel Gagnon-Grenier over 9 years
    Hey there. Were you te review your question, would you leave it as it is? You probably know what to do however, so I'll leave you do it. Please include relevant parts of your link :)
  • Lee Taylor
    Lee Taylor over 9 years
    While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes.
  • Fedir RYKHTIK
    Fedir RYKHTIK over 9 years
    The answer is "Use APD". Additional link it's for those, who has problems with the installation.