As my first topic I am going to discuss PHP performance boosting utility eAccelerator. I am currently working on a PHP based web project that fires off about 30 XMLHttpRequests in succession. The response of each call is displayed on the website and needed after the completion of all the requests where the responses are added to a database. PHP session variables were a possibility however speed could easily become an issue since PHP would be handling many simultaneous sessions and the values would have the be retrieved from the hard disk. Alternatively a database based session handler might be faster but certainly not practical with so many commands being executed in succession. Cookies would have worked for most users however I do not recommend relying on cookies when the sole functionality of an application depends on them.
I also wanted to store the responses for quick retrieval, which are very small, ranging from a few bytes to half a kilobyte, since it is likely different users will request the same information frequently. With this in mind, neither of the previous options were ideal. Ideally the responses could be stored in memory and the PHP functions have the ability to quickly set and retrieve data. Obviously you would not want to try and store a multitude of very large objects in memory however in my case I could store thousands of key-value pairs using a small block of memory. This is where eAccelerator became a practical match for my application. It is among a few PHP memory caching options available such as XCache and Memcache. Although eAccelerator is first and foremost a PHP cache utility to store compiled scripts it also features the ability use memory as a temporary store for information through a powerful API.
On to the installation process… If you run a dedicated server with CPanel/WHM there is an option in the Easy Apache configuration wizard to compile with eAccelerator. Unfortunately, with the latest version of eAccelerator the put and get functions are not enabled on a default compile so I had to take the installation to command line. Keep in mind that you will need root access to be able to recompile Apache. The process I followed is below.
- Download the latest release 0.9.5.2 from http://eaccelerator.net/wiki/Release-0.9.5.2
- Upload the script to your server and extract the package
- CD to the directory
- Run export PHP_PREFIX=”/usr/lib” where “/usr/lib” is the location of your PHP installation
- Run ‘phpize’ if you are running multiple php branches to bootstrap eAccelerator by executing $PHP_PREFIX/bin/phpize
- Run ./configure –enable-eaccelerator=shared –with-php-config=$PHP_PREFIX/bin/php-config –with-eaccelerator-shared-memory
- There are other configuration options such as session support, a full list of options is available at http://eaccelerator.net/wiki/CompileConfiguration
- Run make
- Run make install
- Locate your php.ini file and locate the eAccelerator configuration section where you can set default garbage collection times, storage options, and memory allocation. You can find more information about the settings at http://eaccelerator.net/wiki/InstallFromSource
- You need to make sure that the cache directory referenced in your php.ini is created and the chmod is set to 0777.
Now you can create a simple test PHP script to put a key-value pair into memory and another to get the value of the key you added. A simple put command would look like “eaccelerator_put($key, $value, $ttl)” where $value is the time period in seconds that the key should be stored before being discarded. To retrieve the key’s value you can simply run the command “eaccelerator_get($key)”. You can find complete API details at http://bart.eaccelerator.net/doc/phpdoc/.
By default if you are in a shared environment this functionality will be extended to everyone but you can disable eAccelerator on a per-domain level in your vhosts file. Extending the get/put methods to all users in a shared environment could potentially be dangerous. If you create a phpinfo() file you can monitor the memory usage, number of cached scripts and number of stored keys.
So, how much does this really help? You should see a nice drop in CPU utilization since PHP scripts will be more efficiently compiled and since they are served from memory as long as you have memory available the response time is noticeably quicker. Overall I didn’t see any real performance compromises by enabling eAccelerator. Obviously you will be using more memory but you have control over the amount that is used for storage. After the max is reached then scripts and keys are stored in the cache directory reference in the php.ini file. If you are running PHP, have extra memory available and have root access then I highly recommend eAccelerator to increase server speed and add valuable functionality to store, retrieve and remove data in memory. There are a number of practical applications for this utility.