Use Xdebug to analyze PHP programs to find performance bottlenecks [original]

Source: Internet
Author: User
First, installation configuration
1, download php xdebug extension, website: http://xdebug.org/

2. Compile and install Xdebug under Linux

Reference

Tar-xzf xdebug-2.0.0rc3.gz
CD XDEBUG-2.0.0RC3
/usr/local/php/bin/phpize
./configure--enable-xdebug
CP 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.

Reference

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

View Plainprint?

    1. [Xdebug]

    2. Zend_extension_ts= "/usr/local/php/lib/php/extensions/no-debug-non-zts-20020429/xdebug.so"

    3. Xdebug.profiler_enable=on

    4. Xdebug.trace_output_dir= "/tmp/xdebug"

    5. Xdebug.profiler_output_dir= "/tmp/xdebug"

    6. Xdebug.profiler_output_name= "Script"


Reference

Mkdir-p/tmp/xdebug
chmod 755/tmp/xdebug
Chown 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.out
Usr_local_apache_htdocs_app_login_showheaderlogin_php_cachegrind.out
Usr_local_apache_htdocs_app_play_play_php_cachegrind.out
Usr_local_apache_htdocs_app_user_member_php_cachegrind.out
Usr_local_apache_htdocs_tag_tags_php_cachegrind.out
Usr_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:

View Plainprint?

    1. Function getforbiddentags ()

    2. {

    3. $tagsPath =template_ File_path. " Tags/forbidden_tags.txt ";

    4. If (file_exists ($tagsPath))

    5. {

    6. $fp = fopen ($tagsPath, "R");

    7. $arrconf = array ();

    8. If ($fp)

    9. {

    10. while (!feof ($fp))

    11. {

    12. $line = fgets ($fp, 1024x768);

    13. $line = Trim ($line);

    14. $rows = Explode ("#", $line);

    15. $coumns = explode ("=", Trim ($rows [0]);

    16. If (""!=trim ($coumns [0])

    17. {

    18. $arrconf [Trim ($coumns [0])] = Trim ($coumns [1]);

    19. }

    20. }

    21. }

    22. Return $arrconf;

    23. }

    24. }


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

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