Zend Framework Performance Optimization

posted in: Zend Framework | 0


 Zend Framework Performance Optimization

What is Application Performance

Application performance is meant by how quickly your script executes, How many requests handling per second.

It is often forgotten that to the end user none of these measurements are in any way relevant. To the end user, the only performance metric that ever matters is how quickly the page they requested loads, and how quickly they can do whatever they want to do.

In this white paper I am describing the techniques to improve the performance of Zend Framework.

Why Zend Framework

Zend Framework is a  open-source software framework for PHP 5 designed to eliminate the tedious details of coding and let you focus on the big picture. Its strength is in its highly-modular MVC design, making your code more reusable and easier to maintain.

Has inbuilt API services  for google, Amazon, yahoo, flicker and etc.

Zend Framework performance optimization

The following are the few ways to optimize your Zend Framework.
1)optimize include_path: Define your Zend Framework include_path as early as passable by using the realpath() function in php. Reduce the number of include paths  as less as passable.

define(‘APPLICATION_PATH’, realpath(dirname(__FILE__)));$paths = array(APPLICATION_PATH . ‘/../library’),



set_include_path(implode(PATH_SEPARATOR, $paths);


2) Eliminate unnecessary require_once statements:Using require_once to load the Zend Framework library classes is a bottleneck. Instead of using the require_once use Zend_Loader_Autoloader, autoloading  is a concept of optimization technique designed to push the expensive operation of loading a class file until the last possible moment — i.e., when instantiating an object of that class, calling a static class method, or referencing a class constant or static property. Add the following code in your bootstrap file.

require_once ‘Zend/Loader/Autoloader.php’;Zend_Loader_Autoloader::getInstance();

However, most benefits you may reap from autoloading are negated if your library code is still performing require_once() calls.  Use the following command to strip require_once statements in Unix systems.

% cd path/to/ZendFramework/library% find . -name ‘*.php’ -not -wholename ‘*/Loader/Autoloader.php’ \  -not -wholename ‘*/Application.php’ -print0 | \  xargs -0 sed –regexp-extended –in-place ‘s/(require_once)/\/\/ \1/g’

3) Optimize Zend translation: Zend Framework provides a variety of translation adapters like array, csv, ini and gettext. Use translation cache to speed up the translation.

$cache = Zend_Cache::factory(‘Core’,’File’,$frontendOptions,



$translate = new Zend_Translate(


‘adapter’ => ‘gettext’,

‘content’ => ‘/path/to/translate.mo’,

‘locale’  => ‘en’



// to clear the cache somewhere later in your code


4) Use zend_cache  to cache your content: Cache the database results using the zend_cache if they are not changing frequently.

    $frontendOptions = array( ‘lifetime’ => 7200, // cache lifetime of 2 hours’automatic_serialization’ => true);

$backendOptions = array(

‘cache_dir’ => ‘./tmp/’ // Directory where to put the cache files


// getting a Zend_Cache_Core object

$cache = Zend_Cache::factory(‘Core’,




By using the above code you can initialized the cache

// see if a cache already exists:

if( ($result = $cache->load(‘myresult’)) === false ) {


// cache miss; connect to the database

//Write code to fetch the db results

} else {

// cache hit! shout so that we know

echo “This one is from cache!\n\n”;






Opcode caching

Every time a PHP script executes, it goes through a number of steps to get from the version you wrote into something the PHP engine can understand and execute as logic called opcodes. This process is expensive from a performance perspective. Simply by introducing an opcode cache into your PHP technology stack, it is possible

to increase the speed of your script executions from anywhere to 50 percent to 200 percent.

Though you optimized your Zend Framework you need to cache your php script.


There are many options available, in those I am using apc to cache my opcodes.

The below code is used to cache the php code in to opcodes.

if (!function_exists(‘apc_compile_file’)) {echo “ERROR: apc_compile_file does not exist!”;exit(1);





* Compile Files for APC

* The function runs through each directory and

* compiles each *.php file through apc_compile_file


function compilefilesToApc($dir)


$dirs = glob($dir . DIRECTORY_SEPARATOR . ‘*’, GLOB_ONLYDIR);

if (is_array($dirs) && count($dirs) > 0) {

while(list(,$v) = each($dirs)) {





$files = glob($dir . DIRECTORY_SEPARATOR . ‘*.php’);

if (is_array($files) && count($files) > 0) {

while(list(,$v) = each($files))









Previous Post
Next Post

Leave a Reply