Use Xdebug to analyze PHP programs and find out performance bottlenecks

Source: Internet
Author: User
Tags zts
There is a well-known 80-20 law in economics, which is referenced in programming, that is, 80% of the performance bottleneck is caused by 20% of the Code. With the XDebug extension of PHP, the Code 20% can be found effectively. 1. Install configuration 1. Download The XDebug extension of PHP. url: xdebug. org2. Compile and install XDebug in Linux and reference tar-xzfxd.

There is a well-known 80-20 law in economics, which is referenced in programming, that is, 80% of the performance bottleneck is caused by 20% of the Code. With the XDebug extension of PHP, the Code 20% can be found effectively. 1, install configuration 1, download PHP XDebug extension, URL: http://xdebug.org/2, compile and install XDebug reference tar-xzf xd in Linux

There is a well-known 80-20 law in economics, which is referenced in programming, that is, 80% of the performance bottleneck is caused by 20% of the Code. With the XDebug extension of PHP, the Code 20% can be found effectively.

I. installation and configuration
1. Download PHP XDebug extension, URL: http://xdebug.org/

2. Compile and install XDebug in 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 paths are not necessarily placed in this path, you can specify xdebug in zend_extension_ts. so location.

Reference
Vi/usr/local/php/lib/php. ini
Modify php. ini to remove the PHP acceleration module. Add the following configuration information to support XDebug extension.

View plaincopy to clipboardprint?
[Xdebug]
Zend_extension_ts = "/usr/local/php/lib/php/extensions/no-debug-non-zts-20020429/xdebug. so"
Xdebug. profiler_enable = on
Xdebug. trace_output_dir = "/tmp/xdebug"
Xdebug. profiler_output_dir = "/tmp/xdebug"
Xdebug. profiler_output_name = "script"
[Xdebug]
Zend_extension_ts = "/usr/local/php/lib/php/extensions/no-debug-non-zts-20020429/xdebug. so"
Xdebug. profiler_enable = on
Xdebug. trace_output_dir = "/tmp/xdebug"
Xdebug. profiler_output_dir = "/tmp/xdebug"
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
: Http://sourceforge.net/projects/wincachegrind/

Ii. analysis process
1. Visit your website and click various links on the home page several times. XDebug generates the following files in the/tmp/xdebug directory:
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 and use the client software WinCacheGrind to open each file. It is found that the execution of the following PHP program takes the longest time:
/Usr/local/apache/htdocs/tag/tags. php takes 840 ms

Iii. analysis results:
1./usr/local/apache/htdocs/tag/tags. php

  

(1) The longest time-consuming filter_tags function appears in row 158th of/usr/local/apache/htdocs/tag/tags. php:
$ Tags. = filter_tags ($ videos [$ I] ['tags']). "";

View plaincopy to clipboardprint?

{

$ 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, 1024 );
$ Line = trim ($ line );
$ Rows = explode ("#", $ line );
$ Coumns = explode ("=", trim ($ rows [0]);
If (""! = Trim ($ coumns [0])
{
$ Arrconf [trim ($ coumns [0])] = trim ($ coumns [1]);
}
}
}
Return $ arrconf;
}
}

{

$ 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, 1024 );
$ Line = trim ($ line );
$ Rows = explode ("#", $ line );
$ Coumns = explode ("=", trim ($ rows [0]);
If (""! = Trim ($ coumns [0])
{
$ Arrconf [trim ($ coumns [0])] = trim ($ coumns [1]);
}
}
}
Return $ arrconf;
}
}


  

(5) possible causes of bottlenecks:
The 156 keywords to be filtered are stored row by row in the/usr/local/apache/template/tags/forbidden_tags.txt file. The efficiency of the text database is not high.
The function trim that reads fgets row-by-row functions and removes spaces on both sides of the string or specified characters is inefficient at high loads. It can be used to test file reading functions such as fopen, fread, and fscanf, comparison.

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.