PHP performance

technology: 

PHP performance questions pop up every time a Web site jumps from 2 page reads per day to 2000 page reads. Most of the performance limitations in a Web site are nothing to to with PHP but there are a few small things to check before laying the blame elsewhere.

Lack of memory

Lack of memory is the most common Web site performance issue. PHP lets you make great use of memory and gives you more flexibility than some of the competing technologies. You get to choose between reducing memory usage and speed.

Limited memory, limited performance

You can save a little bit on memory and put up with limited performance. Some of the competing technologies will not let you do this. They require a minimum amount of memory and break with anything less. PHP works with a wide range of memory, gives you installation choices to reduce memory usage and gives you programming choices to reduce memory usage.

I could go on and on about the special things PHP does to minimise memory usage. Your main decision points are the installation step, where you switch off any optional modules you are not using, and the programming spots where you bring a lot of data into memory. I will give you one example of limiting data in memory.

You want to list a lot of items in a page and print a variety of summaries in different sequences. The fastest way to process the data is to read it into an array in the sequence you want to print then loop through the array summarising the data into other arrays then sort the summaries arrays then print everything. If you want to save memory in your PHP program, you can skip the array and bring the data into memory pre-sorted and pre-summarised. Your PHP program will use less memory and take longer because it will perform several database requests instead of one. You choose to minimise memory or to get maximum performance.

Lots of memory, lots of performance

I questioned the performance of one Web hosting VPS advertised as guaranteeing 1 GB of memory. The engineer found it was set to 0.3 GB because that was what they used to do a long time ago for that type of account. This was an old VPS account that was switched from 0.3 GB to 1 GB months before then mysteriously slipped back. The difference between 1 GB and 0.3 GB was impressive. Using only 0.3 GB, Web pages frequently timed out then the system had to log the errors, using up more of the limited memory.

I could have spent weeks reinstalling, reconfiguring, and reprogramming all the software to use less memory. Looking at the cost of VPS accounts with the two memory sizes, the extra memory costs less than $10 per month. My cost to reduce the memory usage to fit 0.3 GB would be between $5000 and $20000, requiring somewhere between 41 years and 167 years to save enough money to cover the expense.

When you have plenty of memory, you use some for a PHP opcode cache. This is special software that cuts out some of the repetitive work in PHP. The feature is so useful that the next version of PHP, version 6, will arrive with the APC PHP opcode cache built in. For PHP 5, you have to switch on a PHP opcode cache yourself.

Database

Databases arrive with different ranges of settings. The most common database, MySQL, has settings files configured for performance or limited memory usage. You can read about the options at the MySQL site. MySQL tries to save time by saving indexes and query results in memory for reuse. You do save time by giving MySQL enough memory to save frequently requested data. If you give MySQL too much memory, Apache, PHP, Linux, and everything else starts to slow down because they are all short of the memory they need to perform quickly. As a general rule, you want to tune all the major components together to share memory equally.

The next consideration is the low cost of memory. An extra gigabyte might cost only $20 per month. Tuning all the applications might consume thousands of dollars of your time and not produce a significant result. If you use a VPS, ask your ISP to check the performance and settings. They will occasionally change something that will make a difference. They might move you to a less crowded server if the server is overloaded. Mostly they suggest you move up to the next level account, with more memory and processor speed, because that change is really easy and cures a lot of problems.

Apache alternatives

Your Web site runs under Apache because most of the world's Web sites run under Apache. There are alternatives to Apache. Some of the alternatives use less memory some of the time. Your ISP might recommend an alternative. Do not replace Apache yourself until you have a lot of experience and great performance measuring tools. You can run into problems and not improve performance. You can end up with a configuration that will not let you add some new feature you need.

If your ISP supports an alternative then jump in and depend on them to fix any incompatibilities or limitations. For everyone else, replacing Apache is number 11 performance improvement on the top 10 list of things you should work on. Replacing Apache produces the least impressive performance gains and the gains usually occur only on very large sites.

PHP opcode cache

This is the big improvement you can make for PHP. Every other change is generic and will work across a variety of technologies. An opcode cache is the only PHP specific change.

PHP scripts are compiled to an internal instruction set called opcode. This is the same as Java and a whole lot of other programming languages going back to the 1980s. Instead of recompiling the code for every Web page request, you can keep the opcode in memory and save the compile step. An opcode cache does that exactly that. There are several you can choose. One of the choices will be built into PHP 6.

You might remember stories about the disastrous performance of Java back when Sun controlled Java. Java was interpreted, making it the slowest programming language under the sun. Every line had to be reread and reinterpreted for every use. Nobody has used that approach since the early 1980s. PHP 3 from way back in 1997 already used the far better approach of compiling into memory. In 1997 Microsoft rewrote Java to compile into memory, just like PHP, then in 1999 IBM rewrote their version of Java to do the same, just like PHP, and finally Sun drag the Sun version of Java into the 1980s world of high speed program execution.

PHP was the speed leader for a while due to the compile into memory approach. By the time the biggest competitor, Java, caught up, PHP already had an opcode cache to make PHP faster. An opcode cache makes typical PHP based Web site code 10 to 20 times faster. A large content management system using an opcode cache is typically limited by the speed of the database. Without an opcode cache, the code might slow down to slightly slower than the database, making PHP look like the cause of all the delays.

Installing a PHP opcode cache is easy. Most ISPs provide PHP preconfigured because it saves them processor cycles. If you use a hosting account or a VPS with something like cPanel, there is a selection box you can tick to install APC or one of the other PHP opcode caches. Check your account management pages now. If you cannot find the setting, ask your hosting company.

measured speed improvements

50 times faster

A program to build an image from components ran 50 times faster with an opcode cash. The program was one I mentioned in a presentation about graphics to PHP International in Frankfurt. The program read an instruction file and assembled the image using the instructions in the file. The process used hundreds of objects and looped through 100000 commands from the command file. The image assembly program was used on several Web sites and the 50 times speed up occurred when the operating system had enough memory to cache all the component files in memory.

The same speed up occurs in pure PHP code applied to analysing logs and similar activities. The bulk of the overall processing time might be in the database, not the PHP code, and the speed up applies only to the PHP code.

20 times faster

PHP scripts producing Web pages perform 20 times faster. If all the CSS files, Javascript files, and images are cached in the Web browser plus you access the Web page locally, you see a 20 times improvement.

10 times faster

The PHP scripts producing Web pages 20 times faster, when viewed over broadband, arrive 10 times faster.

No difference

The PHP scripts producing Web pages 20 times faster, when viewed over dialup, arrive in the same time because the download is the biggest time consumer, not the server processing time. The same happens with content management systems when you switch on database intensive options, the database chews up all the time, not PHP. As a comparison, using a popular CMS, a regular page requires 10 to 20 database accesses but a page producing a fresh word cloud display can require 100000 or more database accesses. Some Web sites have their word cloud displays set to rebuild for every page instead of using a background cron task to rebuild each hour or day.

Green

An opcode cache is environmentally green because you use fewer CPU resources to deliver the same Web pages, reducing electricity use and green house gas emission. If you run your computer on solar power, you can still contribute because your power saving gives you more green electricity you can send to the grid for use by other people. What difference might it make? A 125 watt processor might power up to only 25 watts when delivering a web page. Your total computer room might save 300 watts because other components, including the air-conditioner, can wind down when your processor is not burning up the environment.

PHP opcode caches

APC

APC, the Alternative PHP Cache, is the one built into PHP 6. You might as well use it now if APC is an available choice in cPanel, Webmin, or whatever you use to manage your Web site. Some of the other choices have special features you might occasionally need for some Web sites. Read about APC at php.net/manual/en/book.apc.php.

eAccelerator

eAccelerator, from http://eaccelerator.net/, is the only option presented by some ISPs in cPanel and equivalents. You need the latest version of eAccelerator for the latest version of PHP and your ISP will usually provide the right version. using the default settings, I cannot find any performance differences between eAccelerator and APC.

NuSphere PhpExpress

NuSphere PhpExpress is similar to all the other PHP opcode caches and offers one extra feature if used with NuSphere NuCoder, the NuSphere PHP Encoder.

The encoder is a way to distribute the opcode encoded so that you do not distribute the source code. This encoding completes with an encoder from Zend. Your customers are more likely to use Zend products than NuSphere products with the NuSphere PhpExpress having one advantage, it is free. You could buy both nuSpere NuCoder and the Zend equivalent then send encrypted code to everyone.

Zend

Visit Zend, www.zend.com/products/server/, to see their range of products. Like NuSphere, Zend products are an advantage when used together. They opcode cache does nothing special by itself. When you develop with Zend products, you can encrypt your opcode with a Zend product then sell the encrypted code to people who use the Zend server. The Zend opcode cache does not currently provide significant performance improvements over the other cache products.

Xcache

Xcache is popular and was the flavour of the month a while back. Xcache is not offered by any of the hosting companies I use or my customers use. Again I recommend those without years of experience should choose whatever is supplied and supported by their Web host.

Windows Cache Extension for PHP

Right, Microsoft get into the act. This is an attempt to make PHP effective under Microsoft's IIS. Use Apache, not IIS. You can use Apache, PHP, MySQL, PostgreSQL, and a whole bunch of other stuff on Windows. Windows is not a problem when you use the Pro version with NTFS. IIS is the big problem. IIS has some really nice features but it was history a long time ago and those really nice features are popping up in the open source alternatives to IIS.

Windows with NTFS gets a lot of its performance advantages from smart caching of the file system, which means you need a lot of memory. You might survive with slightly less memory using Linux and Apache because both Linux and Apache install far fewer features by default.

Conclusion

Use a PHP opcode cache. Use whatever your Web host recommends. If you have a choice and do not need a special feature of another cache, use APC because that is the one you will use in PHP 6.