Burning the Bulb Brighter

Whether you're an independent web site operator or the manager of a large web serving farm, you must constantly look for ways to optimize your environment to keep costs low and boost overall performance. Indeed, optimization has recently become something of a crusade for businesses, as many search for ways to yield even the slightest improvements in profit.

Whether you’re an independent web site operator or the manager of a large web serving farm, you must constantly look for ways to optimize your environment to keep costs low and boost overall performance. Indeed, optimization has recently become something of a crusade for businesses, as many search for ways to yield even the slightest improvements in profit.

Typically, when you deploy optimizations — whether it’s code, new systems, or a database — you’re forced to choose two of better, cheaper, and faster. For example, you could choose to deploy more application servers to distribute load across disparate hosts, but the equipment costs would be high. Or, you can rewrite your application code, to take advantage of newer features in your processing engine, but the associated delay and expense might not make the overhaul worthwhile. Then again, you could outsource the entire optimization project to a team of mercenary consultants, albeit at an astronomical price.

Of course, optimizations have theoretical returns that must be considered as well. While deploying additional hosts to handle load and increase performance does net increased performance, the gains are only linear: double the hosts and requisite network gear, and you double the capacity of your system. To achieve increased performance on an exponential scale, you need to resort to code optimization.

Fortunately, if you’re running PHP web applications, there are a number of options to improve application performance very easily and very cheaply. In fact, some improvements are virtually free: free as in cost, and virtually free in effort.

This month, let’s try to burn the “LAMP” a little brighter and look at Zend Optimizer and APC, two of the most popular choices for boosting PHP performance.

The Zend Engine and Zend Optimizer

The Zend Engine is the component of PHP that parses and executes your code. If you’re developing with PHP 4 or are experimenting with one of the PHP 5 release candidates, you are already using the Zend Engine. PHP 4 is based on Zend Engine 1.0 and PHP 5 is based on Zend Engine 2.0.

According to the Zend web site (http://www.zend.com), the Zend Engine: parses, compiles, and executes PHP scripts; implements all of the standard PHP data structures; interfaces with extension modules to connect to external resources and protocols, such as SQL, HTTP, and FTP; and provides for integration with Java and .NET. As the Zend Engine is also extensible, it’s possible to “plug-in” third-party modules, such as debuggers, custom loaders, and optimizers.

The free Zend Optimizer is a performance boosting plug-in for the Zend Engine. Zend Optimizer is available for Linux, Windows, FreeBSD, and Solaris, and promises a 40 percent runtime performance gain when executing files encoded by the Zend Encoder. The latest release of the Zend Optimizer, version 2.5.1, supports PHP 4 or later. (Version 2.5.1 for Linux glibc21 was used for this article.)

The Zend Optimizer speeds performance by applying about twenty distinct optimization patterns. It’s loaded as a passive PHP module, and automatically optimizes and loads your PHP script files. You don’t have to change any of your code to employ the Zend Optimizer, and once installed, it works without any further interaction from a system administrator.

(One caveat: if you’re running older, pre-encoded PHP files, make sure that your code and the Zend Optimizer version you’re using are compatible. More information on this is available on the Zend support forum at http://www.zend.com/phorum/list.php?num=5.)

Installing and Configuring the Zend Optimizer

Installing the Zend Optimizer is quite easy, as a simple install script does all the work. Grab the latest version from http://www.zend.com. After you unpack the tarball, cd into the resulting directory.

You might be startled to find very little documentation (the README file in the package directs you to two .pdf documents in the ./data/doc directory) and may be further surprised that there’s no source code. That’s not a mistake. The installation script installs a set of pre-compiled binaries and then alters your php.ini to enable the Zend Optimizer and set a few options.

To install Optimizer, run ./install from the top directory of the package. Read and (decide if you want to) agree to the legal notice, and then select an install path. /usr/local/Zend/ is the default and should work fine in most instances. Find the location of your php.ini file and your Apache web server, and then follow the on-screen instructions to complete the install. Then restart Apache.

(If you have an incompatible version of PHP, the install script will tell you so. In that case, you must either upgrade your PHP installation, or go back to the Zend web site and find an older version of the Optimizer that supports your version of PHP.)

Before completing, the install script creates a backup of your php.ini file, creates a symbolic link from php.ini to /usr/ local/Zend/etc/php.ini (if you accepted the default install directory, for example), and adds a new section to php.ini that’s similar to Listing One.

Listing One: Zend Optimizer’s configuration parameters

zend_extension_manager.optimizer_ts=/usr/local/ Zend/lib/Optimizer_TS -2.5.1

zend_extension=/usr/local/Zend/lib/ZendExtension Manager.so

The most important parameter in Listing One is zend_ optimizer.optimization_level. The value of this option controls the combinations of passes the Zend Optimizer performs when optimizing your code. The default value for zend _optimizer.optimization_level is 15. If you keep zend_optimizer.optimization_level set to 15 and execute phpinfo(), you should see something like Figure One.

Figure One: The Zend Optimizer section of phpinfo()

There’s a healthy discussion on the Optimizer’s mysterious optimization passes on the Zend Optimizer forum at http://www.zend.com/phorum/list.php?num=5.

By the way, if you don’t need to load encoded files, you can disable the Zend Loader by adding zend_optimizer.enable _loader = 0 to the [Zend] section of php.ini. (If you make this change, make sure to restart Apache.)

OK, But Does It Work?

Let’s run a few bits of code with the Zend Optimizer on and off, and use Apache Bench to measure the performance of each configuration. For these sample benchmarks, the host is a VIA EPIA Nehemiah M10000 with a 1 GHz processor, 512 MB of RAM, Linux kernel 2.4.20-8, Apache 1.3.28, PHP 4.3.4, and MySQL 4.0.14-standard. The Zend Loader was disabled.

Rather than write an obscure script, let’s try the benchmark on the popular PHP-based content management system PHP-Nuke. It’s representative, because the index.php page in PHP-Nuke’s root directory contains many elements commonly found in PHP-based web projects, including text manipulation, various SQL statements, and, of course, HTML output. These benchmarks were performed using PHP-Nuke Version 6.9, which can be found at http://www.sourceforge.org.

After you install PHP-Nuke, run the benchmark on the local machine with:

$ /usr/local/apache/bin/ab -n10 -c1 -dkS http://localhost/nuke/index.php

Do that fifteen times with the Zend Optimizer off, and then fifteen more times with the Zend Optimizer on.

On the test machine, the mean requests-per-second with the Zend Optimizer disabled was 4.23. The mean requests-per-second achieved with the Zend Optimizer enabled was 4.45. That performance increase is just under 5 percent.

For the next benchmark, let’s test PHPMyAdmin. After installing PHPMyAdmin, run…

$ /usr/local/apache/bin/ab -n10 -c1 -dkS http://localhost/phpmyadmin/index.php

… fifteen times with the Optimizer off, and another fifteen times with it on. Without the Optimizer, the mean requests-per-second on the test machine was 6.08. With the Optimizer, the mean requests-per-second was 6.70, a performance increase of just over 9 percent.

While cheap performance boosts like installing the Zend Optimizer are typically a good thing, the actual benefits of the Zend product may not be as grand as its web site will have you believe. However, taking a step back, a 5 percent increase in performance on a grand scale is quite acceptable.

Now, if you’re still stuck with PHP 3 code, the performance gains are closer to 25 percent. However, the PHP 3 interpreter was not nearly as efficient as PHP 4′s, hence the differential.

In the end, though, to eke out even more cycles, you’ll have to get down and dirty and clean up your code.

Zend, with its involvement with core PHP development, undoubtedly provides the most popular and, arguably, the most trusted and supported optimization engine. However, while free, the Zend Optimizer is not open source. You can’t delve into it and discover or modify its inner workings. But there are true open source alternatives.

Open Source Options

A rather popular open PHP caching engine is the Alternative PHP Cache (APC), available from http://apc.communityconnect.com. The maintainers of the project, George Schlossnagle and Daniel Cowgill, are emphatically dedicated to providing a flexible, universally accessible, and free PHP optimization engine.

Much like the Zend Optimizer, APC allows PHP scripts to be cached in a compiled state in either System V shared memory or memory-mapped files. When a particular PHP file is called, APC checks to see if it’s cached and loads it; otherwise, it’s compiled and then cached.

Let’s try the same benchmark against phpMyAdmin 2.5.6 as used above, with the Zend Optimizer disabled. As the test host system was running PHP 4.3.4, all tests were performed with the most current version of APC, 2.0.3.

On the test system, the instructions in the INSTALL file compiled an Apache Dynamic Shared Object (DSO) with no errors or problems. Make sure to edit your php.ini file to add the newly-installed object. A line like the following is typical:


Restart Apache and then verify APC’s installation by running phpinfo(). You should see a brand new APC information table with default parameters.

Let’s test APC out. As before, run the benchmark on the local machine with:

$ /usr/local/apache/bin/ab -n10 -c1 -dkS http://localhost/phpmyadmin/index.php

After running this benchmark drill fifteen times with APC enabled, the mean requests-per-second was 20.96. Skeptical of this result, we decided to run the entire drill again without optimization, then with Zend, and then with APC. The results were reproduced. With APC enabled, the mean requests-per-second was 20.96.

What Else Is Out There?

In addition to APC and the free Zend Optimizer, Zend also offers the Zend Performance Suite, which promises a 25 times performance increase in server throughput. This product, with a price starting at just under $2000, may yield similar benchmark results to APC. You should test the claim before buying. See the Zend web site for details.

Another popular, open source PHP performance suite is Turck MMCache, available at http://turck-mmcache.sourceforge.net. This package is an optimizer and a dynamic content caching system for PHP. The Turck MMCache site also includes benchmarking results for several different PHP optimizing packages. The maintainers of Turck claim to edge out all other PHP optimization engines. However, the benchmark results on their homepage are a bit outdated.

Finally, there is the ionCube PHP Accelerator (PHPA). Much like Zend, it’s proprietary software provided as a precompiled shared object for Apache/PHP. The creators of ionCube claim a performance increase between 5 and 10 percent with the free PHPA. The focus of ionCube is PHP encoding for code protection and optimized byte code. ionCube’s standalone PHP Encoder can be had from $199. For more information and to download PHP Accelerator, visit http://www.php-accelerator.co.uk.

Michael Bordash is an integration specialist, writer, and programmer. His company, IP-soft.net, provides managed proprietary and open source services for Fortune 1000 companies. Michael is also the developer and manager of InternetDJ.com. You can reach Michael at michael@bordash.com.

Comments are closed.