Use Xdebug to analyze PHP programs to identify performance bottlenecks

Source: Internet
Author: User
There is a famous 80-20 law in economics, which is quoted in programming: 80% of the performance bottlenecks are caused by 20% of the code. With PHP's xdebug extension, you can effectively find this 20% code.

First, installation configuration

1, download php xdebug extension, website: http://xdebug.org/

2. Compile and install Xdebug under Linux

Tar-xzf xdebug-2.0.0rc3.gzcd xdebug-2.0.0rc3/usr/local/php/bin/phpize./configure--ENABLE-XDEBUGCP modules/ xdebug.so/usr/local/php/lib/php/extensions/no-debug-non-zts-20020429/

Note:/usr/local/php/lib/php/extensions/no-debug-non-zts-20020429/different PHP version of the path is not necessarily placed in the path, you can Zend_extension_ The location of the xdebug.so is specified in TS.

Vi/usr/local/php/lib/php.ini

Modify the php.ini, remove the PHP accelerator module, add the following configuration information to support Xdebug extension

[xdebug]zend_extension_ts= "/usr/local/php/lib/php/extensions/no-debug-non-zts-20020429/xdebug.so" Xdebug.profiler_enable=onxdebug.trace_output_dir= "/tmp/xdebug" xdebug.profiler_output_dir= "/tmp/xdebug" Xdebug.profiler_output_name= "Script" Mkdir-p/tmp/xdebugchmod 755/tmp/xdebugchown www:www/tmp/xdebug/usr/local/ Apache/bin/apachectl-k restart

3. Client (Windows): Wincachegrind Download Address: http://sourceforge.net/projects/wincachegrind/

Second, the analysis process

1, visit your website, the first page on the various links click a few times, xdebug in the/tmp/xdebug directory to generate the following files:

Usr_local_apache_htdocs_app_checknum_chknum_php_cachegrind.outusr_local_apache_htdocs_app_login_ ShowHeaderLogin_php_cachegrind.outusr_local_apache_htdocs_app_play_play_php_cachegrind.outusr_local_apache_ Htdocs_app_user_member_php_cachegrind.outusr_local_apache_htdocs_tag_tags_php_cachegrind.outusr_local_apache_ Htdocs_top_top_php_cachegrind.out

2, copy the above files to Windows, with the client software Wincachegrind open each file, found that the following PHP program execution took the longest time:

/usr/local/apache/htdocs/tag/tags.php Time Consuming 840ms

Third, the analysis results:

1,/usr/local/apache/htdocs/tag/tags.php

(1) The longest-consuming filter_tags function appears on line 158th of/usr/local/apache/htdocs/tag/tags.php:

$tags. = Filter_tags ($videos [$i] [' tags ']). " ";

(2) The Filter_tags function was called 21 times by the Filter_tags function from the/usr/local/apache/htdocs/include/misc.php,getforbiddentags function, Filter_ The majority of the time the tags function is caused by the getforbiddentags function. The contents of the Getforbiddentags function are as follows:

function Getforbiddentags () {    $tagsPath =template_file_path. Tags/forbidden_tags.txt ";    if (file_exists ($tagsPath))    {        $fp = fopen ($tagsPath, "R");        $arrconf = Array ();        if ($fp)         {while            (!feof ($fp))             {                $line = fgets ($fp, 1024x768);                $line = Trim ($line);                $rows = Explode ("#", $line);                $coumns = explode ("=", Trim ($rows [0]));                if (""!=trim ($coumns [0]))                {                     $arrconf [trim ($coumns [0])] = Trim ($coumns [1]);        }} return $arrconf;    }}

(3) The Getforbiddentags function is analyzed, and the PHP function trim is called 16,827 times.

(4) Possible causes of bottlenecks:

The 156 keywords to filter are stored row by line in the/usr/local/apache/template/tags/forbidden_tags.txt file, and the text database is inefficient.

Line-by-row reading of the function fgets, and the removal of whitespace on either side of the string or the function trim of the specified character under high load is inefficient, you can test the fopen, Fread, fscanf and other file reading functions, compare.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.