Appendix A. Performance Optimization

Appendix A. Performance Optimization

This document describes some methods to increase performance in your Zend Framework based applications.

Performance is an important aspect of almost every application. Inefficient SQL queries and costly algorithms can be a show stopper. When designing an app you should keep performance in mind, but not let it overtake you. Pre-optimization is the root of all evil! Remember that you need something that's actually usable before you need performance. There is more than one aspect to performance as external influences such as the network can have a significant effect. Aside from code, webserver configuration does makes a big difference.

Watch those SQL queries as using an ORM can mask inefficient queries. The profiler is your friend. Profile often throughout development to spot any problems that could occur. Caching is always easier to implement during development. Use caching wisely, don't go overboard caching every little thing. Focus on the database or your datasources.

Use an opcode cache! If you aren't using an opcode cache, you probably don't care much about performance. Opcode caches optimizes and caches compiled php code so the same code does not have to be compiled by php every request before execution. There are a variety of opcode caches out there, but the best supported is APC Opcode Cache. APC tuning is also a tricky process, but you'll love the benefits.

A.1. Pre-requiring often used files

Some often used files, like the ViewRenderer, are included inside a class method. On hosts where opcode caches like APC aren't enabled this increases performances. However, on hosts where opcode caches are enabled this actually decreses performance. APC works in two stages of PHP, the first is when files are pre-required and are not inside conditional statements. At this stage, PHP itself has not parsed or executed the current file. APC has the change to perform optimzation of the code and link the required file with the current one. The next stage is when php code is executed, while APC cannot preload resources, it can still cache compile files. So if your using autoloading, you still get most of the benefits. As time passes, this may not be as relevant.

One way of speeding things up when using an opcode cache is by including often used files in you bootstrap. The script below will output all files that are currently used. Run this inside a generic controller in your application to get an idea of the files that your application requires every request. You can then use that list in your bootstrap to pre-require files that may never have APC's optimization due to conditional loading or autoloading.

$classes = array_merge(get_declared_interfaces(), get_declared_classes());
foreach ($classes as $class) {;
    if (strtolower(substr($class, 0, 4)) == 'zend' 
        || strtolower(substr($class, 0, 3)) == 'zym') {
       $file = str_ireplace('_', '/', $class) . '.php';
 
       echo 'require_once \'' . $file . '.php\';' . '<br />';
    }
}