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

前端 未结 12 708
无人共我
无人共我 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条回答
  • 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

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

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

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

    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

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

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

    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.

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

    I use a combination of PEAR Benchmark and log4php.

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

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

    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.

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

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

    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. http://www.php.net/register_tick_function

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