For PHP application speed, speed up, and then speed! , part 1th: Improving performance and throughput using opcode caching software
In a very short time, PHP became a very popular programming language for developing WEB applications. For beginners, PHP is easy to install and learn. For experienced developers, PHP (starting from V5) provides a powerful object-oriented feature. PHP developers have a large group of people, a large number of open source and business libraries and tools to expand the functionality of the core language. PHP can produce visual results quickly, so it is highly praised by everyone.
Similar to other scripting languages for developing Web applications (including Perl, Python, and Ruby), PHP code is parsed and translated every time the HTTP request is invoked (the PHP engine executes the primitive instruction directly, similar to assembly language). In the case of low or negligible requirements, the server appears to be able to perform this complex interpretation process immediately. But once the pages are processed, the explanation-essentially, repetitive work-can be a huge burden on the server. In some cases, "compiling" the PHP code will take far more time than it takes to execute the code. As a result, you often reap the consequences when demand increases, because processing these constantly explained and dynamically generated pages requires more system resources.
If your budget for processors and RAM is not limited, it is not necessary to optimize the application stack (hardware, operating system, database, Web server, and PHP code) to ensure the responsiveness of your site. However, since money is usually the most scarce resource, improving performance is essential. Tuning means adding memory to the system, modifying operating system parameters, speeding up the Web or database server, increasing code efficiency, or some combination of these. Each has its own role to play.
Don't waste a cycle--reuse
Another way to save CPU cycles is to reduce the duplication of effort required to run your PHP application. Of course, there's no need to translate the same PHP code every time. After the PHP code is translated into the opcode, it can be saved and reused-until the original code is modified. Indeed, caching--for saving and reusing PHP opcode--is a mechanism within several PHP accelerators, including open source alternative php Cache (APC), PHP-enabled Turck MMCache, XCache, Eaccelerator, and Commercial Zend Platform. The latter three types of accelerators can cache and optimize bytecode, which provides more speed for the system to improve.
This month, I'll explore how to install, deploy, and configure XCache. XCache is relatively new, but many sites use it with great results. In addition, XCache is easy to build, install, and configure because it is implemented as a PHP extension. Apache and PHP do not need to be recompiled.
This paper is based on XCache V1.2.0. It provides reliable support for earlier versions of PHP V4.3.11 to V4.4.4, PHP v5.1.x to v5.2.x, and PHP V6 (XCache does not support PHP v5.0.x). XCache is compatible with mod_php and FastCGI but does not support Common Gateway Interface (CGI) and command line PHP interpreter. XCache source code can be built on many systems, including FreeBSD, Sun Solaris, linux®, and Mac OS X as shown here. Use the Cygwin UNIX® emulation environment or Visual C to build XCache on the microsoft®windows®. You can also build XCache for Cygwin or native Win32. The latter is compatible with the official Win32 version of PHP.
The demo of this article is based on the V10.4.8 Xcode on the Apache V2.2.3, PHP V5.2.0, XCache V1.2.0 (released in October 2006) and Mac OS X V2.4.1 Tiger. The hardware platform is an Apple MacBook with 2-ghz Intel®core Duo processor and 2 GB RAM.
Basic Steps for building XCache
Before you begin, make sure that PHP is properly installed and verify that Phpize is located under the PATH of the shell. At the same time, you need a C compiler, such as GNU Compiler Collection (GCC), and a set of development tools that contain make and M4. On Mac OS X, the free Xcode Software development environment provides the necessary build tools.
Complete the following steps to build, deploy, and test XCache on Mac OS x. Building XCache on other platforms is similar. If you are using Linux, the distribution may already contain XCache or it has been provided to you in a prepackaged format.
Increase shared memory for Mac OS X
First increase the total amount of shared memory left on Mac OS X. To do this, you create (or edit) a file/etc/sysctl.conf and create the following entries:
Listing 1. Increase the amount of shared memory left on Mac OS X
kern.sysv.shmmax=33554432
kern.sysv.shmmin=1
kern.sysv.shmmni=32
kern.sysv.shmseg=8
kern.sysv.shmall=8192
These settings increase the total amount of shared memory to MB. If you need to further expand shared memory, you can set Kern.sysv.shmall to Kern.sysv.shmmax divided by the hardware page size. The hardware page size can be obtained using sysctl hw.pagesize. For example, suppose you need to 128 MB of shared memory, then set the kern.sysv.shmmax=134217728 and set the kern.sysv.shmall=32768.
Reboot Mac OS X to make the changes take effect. After the reboot, verify that the new settings are in effect and type:
sysctl -a | grep kern.sysv
Build XCache from source code
Next, build XCache from the source code. Download the source code from the http://xcache.lighttpd.net. After downloading, unzip the code and convert to the new directory created by the. tar file.
Listing 2. Build XCache from source code
$ cd /tmp
$ wget http://210.51.190.228/pub/XCache/Releases/xcache-1.2.0.tar.gz
$ tar xzf xcache-1.2.0.tar.gz
$ cd xcache
Run phpize to prepare for compiling XCache.
Listing 3. Run Phpize
$ phpize
Configuring for:
PHP Api Version: 20020918
Zend Module Api No: 20020429
Zend Extension Api No: 20050606
Run configure to create a makefile that is appropriate for the native operating system.
Listing 4. Run Configure create Makefile
$ ./configure --enable-xcache --enable-xcache-coverager
checking build system type... i686-apple-darwin8.8.1
checking host system type... i686-apple-darwin8.8.1
...
creating libtool
configure: creating ./config.status
config.status: creating config.h
Here, the--enable-xcache option contains XCache support, and the--enable-xcache-coverager option contains additional features for measuring accelerator effectiveness. To enable opcode optimization, add--enable-xcache-optimizer.
Of course, the next step is to build and install the code using the Make command line. Run make, and then run make install as the root user.