Xdebug Analyzing PHP code performance

Source: Internet
Author: User
Tags crc32 checksum stack trace

Xdebug is an open source PHP program debugger (a debug tool) that can be used to track, debug, and analyze the health of PHP programs. is a C language Extension pack (under Windows with a. dll extension).
Xdebug:
http://xdebug.org/download.php

Wincachegrind:
http://sourceforge.net/projects/wincachegrind/

1. Download
Take the Windows environment php5.2 as an example. To the official download of the appropriate version of Xdebug, see, should be in the first two links to choose a download, depending on whether your PHP5.2 is non-thread-safe or thread-safe version, I here is the latter, so choose the red box calibration link Download

After the download is a DLL file, it is recommended to modify it to a more friendly file name: Php_xdebug.dll.

2. Configuration
Copy Php_xdebug.dll to $php_installed_dir/ext directory, modify PHP.ini, you can set the following options first:

PHP.ini Code
    1. [Xdebug]
    2. Extension=php_xdebug.dll
    3. Xdebug.profiler_enable=on
    4. Xdebug.trace_output_dir= "D:\temp\xdebug"
    5. Xdebug.profiler_output_dir= "D:\temp\xdebug"


Note: Output_dir need to be built manually beforehand, otherwise the profiler data will not be generated to this directory.

3. Test Installation Success
Restart Apache, run <?php phpinfo (), and see the following options to indicate that the installation was successful


4. Start Debugging
1). Test Case1

PHP code
    1. <?php
    2. Require_once (' empty.php ');
    3. ?>


Run results


Xdebug output file Format (cachegrind.out)


Xdebug output file Format (cachegrind.out)

The performance test output file name of the Xdebug can be configured.
The default is Xdebug.profiler_output_name = cachegrind.out.%p
The%p is the PID of the server and outputs files such as "cachegrind.out.1408".
It may not be so easy to test many files on a Web site. In addition, the file name is the same for a single entry.
There is no description of this parameter in the Chinese article that I see on the Internet.

       
%c CRC32 checksum value for the current working directory Trace.%c Trace.1258863198.xt
%p PID of the current server process Trace.%p Trace.5174.xt
%r Random number Trace.%r Trace.072db0.xt
%s Script file name (note) cachegrind.out.%s cachegrind.out._home_httpd_html_test_xdebug_test_php
%t Unix Timestamp (seconds) Trace.%t Trace.1179434742.xt
%u Unix Timestamp (microseconds) trace.%u Trace.1179434749_642382.xt
%H $_server[' Http_host '] Trace.%h Trace.kossu.xt
%R $_server[' Request_uri '] Trace.%r Trace._test_xdebug_test_php_var=1_var2=2.xt
%s session_id (from $_cookie if set) trace.%s Trace.c70c1ec2375af58f74b390bbdd2a679d.xt
%% % character trace.%% Trace.%.xt
Note This does not apply to the file name of the trace file

You can find some parameters that are appropriate for you from the table above.

For example, I want to generate an output file for each file.

Then I can use:

Xdebug.profiler_output_name = cachegrind.out.%s

Multiple domain names can also be combined to use

Xdebug.profiler_output_name = cachegrind.out.%h.%u.%s

=============================================================

Use the Xdebug configuration of the Xdebug debug and optimize PHP program series tutorials to explain xdebug parameter configuration in detail according to Xdebug's official documentation.

Xdebug Configuration

Part I: Installation
Pre-compiled modules
It is easy to install precompiled modules. Just put them in a directory and add the following to PHP.ini: (Don't forget to change the path and file name to your own value and be sure you're using the full path)
Zend_extension_ts = "C:/php/modules/php_xdebug.dll"

Part II: Basic Features:
Related parameter settings
Xdebug.default_enable
Type: Boolean default value: On
If this setting is on, the stack trace will be displayed by default in the Error event. You can suppress the display of stack traces by using xdebug_disable () in your code. Since this is one of the basic functions of xdebug, it is wise to set this parameter to ON.

Xdebug.max_nesting_level
Type: Integer default value: 100
The value of this setting was the maximum level of nested functions, is allowed before the script would be aborted.
Limit the access depth of infinite recursion. The value set by this parameter is the maximum access depth of the nested program allowed before the script fails.

Part Three: Stack trace:
Related parameter settings
Xdebug.dump_globals
Type: Boolean default value: 1
Limits whether the value of a hyper-global variable defined by the Xdebug.dump.* setting is displayed
For example, Xdebug.dump.SERVER = Request_method,request_uri,http_user_agent will print PHP Hyper global Variables $_server[' Request_method '], $_server [' Request_uri '] and $_server[' http_user_agent '].

Xdebug.dump_once
Type: Boolean default value: 1
Limit whether the value of a hyper-global variable should be dumped in all error environments (when set to off) or only at the beginning (set to ON)

xdebug.dump_undefined
Type: Boolean default value: 0
If you want to dump an undefined value from a hyper-global variable, you should set the parameter to on, otherwise set to OFF

Xdebug.show_exception_trace
Type: integer default value: 0
When this parameter is set to 1 o'clock, even if an exception is caught, Xdebug will still enforce exception tracking when an exception occurs.

Xdebug.show_local_vars
Type: integer default value: 0
When this parameter is set to not equal to 0 o'clock, the stack dump generated by xdebug in the wrong environment will also show all local variables, including the one that has not yet been initialized. Note that this generates a lot of information and is therefore closed by default.

Part IV: Analyzing PHP scripts
Related parameter settings
Xdebug.profiler_append
Type: integer default value: 0
When this parameter is set to 1 o'clock, the file will not be appended when a new requirement is added to the same file (depending on the xdebug.profiler_output_name setting). Instead, the file will be appended as a new file.

Xdebug.profiler_enable
Type: integer default value: 0
The right to open the Xdebug file is to create the file in the file output directory. Those files can be read by Kcachegrind to show your data. This setting cannot be set by calling Ini_set () in your script.

Xdebug.profiler_output_dir
Type: String default value:/tmp
This file is written by the profiler file output and is sure that the PHP user has permission to write to the directory. This setting cannot be set by calling Ini_set () in your script.

Xdebug.profiler_output_name
Type: String default value: cachegrind.out%p
This setting determines the name of the file to which the dump trace is written.

Part V: Remote Debug
Related parameter settings
Xdebug.remote_autostart
Type: Boolean default value: 0
In general, you need to use explicit HTTP Get/post variables to turn on remote Debug. And when this parameter is set to On,xdebug will often try to open a remote debug session and attempt to connect to the client, even if the Get/post/cookie variable is not current.

Xdebug.remote_enable
Type: Boolean default value: 0
This switch controls whether Xdebug should try to connect a debug client that sets the listening host and port according to Xdebug.remote_host and Xdebug.remote_port.

Xdebug.remote_host
Type: String default value: localhost
Select the host that the debug client is running on, and you can use not only the host name but also the IP address

Xdebug.remote_port
Type: Integer default value: 9000
This port is xdebug trying to connect to the remote host. 9000 is the default port for the generic client and the debug client being bound. Many clients use this port number, and it is best not to modify this setting.

Note: After all the above parameters have been modified, to restart Apache to take effect!

=======================================================

Xdebug Analyzing PHP code performance

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.