For PHP application speed, speed up, and then speed! Part 2nd

Source: Internet
Author: User
Tags config memory usage php source code valgrind versions zend linux

For PHP application speed, speed up, and then speed! Part 2nd: Analyzing PHP applications to find, diagnose, and accelerate slow-running code

"For PHP application speed, speed up, and then speed!" The 1th part of the series demonstrates how to use XCache (PHP opcode cache) to speed up the entire site. XCache (one of only a handful of cache packs) retains the output of the compilation process, eliminating other redundant work. As long as the page has not changed, the cached page will be able to perform the role of agent. When the page changes, the cached page becomes invalid and replaced.

OpCode caching--and an opcode optimizer, usually provided by the same package--is a low-cost technique for accelerating site responsiveness. Many cache packs are free and open source, benefiting from the need to change any code.

Of course, in some applications, comparing the actual execution time, the time required to translate the PHP source code file into its corresponding opcode is negligible. Connecting to a remote database server, querying with inefficient SQL statements, and other large amounts of work to parse and manipulate data can be cumbersome, resulting in increased overhead and even waste. A good network design and smart database structure can improve the length of time and slow queries, and ask friendly experts for help if needed. However, if your code runs slowly, you may prefer to handle it yourself.

But where do we start? As is generally thought, debugging code before the code is complete is unwise-because the first implementation of the code can be very rapid. The first thing to do is to test or benchmark the performance of the code when it is correct and functional, regardless of whether it appears to be running slowly or actually. Trying to optimize the code without performing such a diagnosis is undoubtedly groping in the dark.

A simple performance metric is the wall clock time (wall clock times), or the actual latency between a page request and a completed rendering. For certain situations-such as WEB servers, databases, and browsers that run locally on your own workstation-wall clock time can provide information. However, wall clock time is not practical for most other situations, such as network latency, active WEB servers, or active databases.

A more precise measurement--or even a measure of how long to run a single source code statement--can take a code analyzer. The parser is typically implemented as an extension of the PHP Run-time engine, recording the delta at which the statement starts and ends, the delta between the start and end of the recording program, and capturing the total time that the response to the requested request was formed. With this verticality, statements, loops, functions, classes, or slow-running libraries can be used as analysis targets. If it is not time but a problem with memory usage, then a good parser can also show the memory footprint of the component.

One of the more popular parsers in PHP is Xdebug, which also provides a server hook for interactively debugging PHP applications. (see "Better Ways to debug" for more information.) Another part of the series will explore advanced interactive debugging. Xdebug is easy to build from source code and is easy to install as a Zend extension. (There are now binary files for some platforms.) When ready, each request based on a PHP page generates a dataset that can be viewed in kcachegrind.

Build and install Xdebug

If you have the PHP utility phpize and Php-config, and you have access to the php.ini configuration file for your system, it takes only a few minutes to install and set up Xdebug. The instructions given below are for linux®, but the installation steps on Mac OS X are actually similar. (You can find Xdebug precompiled versions of microsoft®windows® from the Xdebug Web site.) )

The latest version of Xdebug is V2.0.0RC3 (the final version V2.0.0 may be available when you read this article). Download and unpack the tarball, and then switch to the subdirectory of the source code. Make sure Phpize and Php-config are located in the Shell's PATH, ready to build with Phpize.

Listing 1. Set Xdebug

$ wget http://www.xdebug.org/files/xdebug-2.0.0RC3.tgz
$ tar xzf xdebug-2.0.0RC3.tgz
$ cd xdebug-2.0.0RC3/xdebug-2.0.0RC3
$ phpize
Configuring for:
PHP Api Version:     20020918
Zend Module Api No:   20020429
Zend Extension Api No:  20050606

Phpize's product is a script-named configuration-that configures the rest of the build process. To build the Xdebug, enter the./configure immediately after make.

Listing 2. Building Xdebug

$ ./configure
checking build system type... i686-apple-darwin8.8.1
checking host system type... i686-apple-darwin8.8.1
checking for egrep... grep -E
...
$ make
...
Build complete.
(It is safe to ignore warnings about tempnam and tmpnam).

The make command generates XDEBUG extensions, xdebug.so. The rest of the work is to install using the sudo make install.

$ sudo make install
Installing shared extensions: /usr/lib/php/extensions/no-debug-non-zts-20020429/

Note: If you run the last command in a terminal window, select and copy the directory that was issued in the last step. It will be used in the next step.

Finally, to make the configuration data visible, you must use Kcachegrind and GraphViz. Linux distributions containing K Desktop environment (KDE) are likely to contain Kcachegrind and GraphViz. If not, those versions that are appropriate for the Linux you use are not difficult to find. Debian users can use Advanced packaging Tool (APT) to quickly install Kcachegrind and GraphViz and all package dependencies.

Listing 3. Install Kcachegrind

$ apt-cache search kcachegrind
valgrind-callgrind - call-graph skin for valgrind
kcachegrind - visualisation tool for valgrind profiling output
kcachegrind-converters - format converters for KCachegrind profiling visualisation tool
$ apt-cache search graphviz
graphviz - rich set of graph drawing tools
graphviz-dev - graphviz Libs and Headers against which to build applications
graphviz-doc - additional documentation for graphviz
libdeps-renderer-dot-perl - DEPS renderer plugin using GraphViz/dot
...
$ sudo apt-get install kcachegrind graphviz
...

If KDE is not installed in the system, Kcachegrind, GraphViz, and all the necessary content will consume approximately MB of disk space.

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.