November 18, 2012 . . Comments
Tags:


Improving Performance in Zend Framework 2


I am going to list options here on how to improve the Performance. This will be an ongoing list which i will keep updated while doing performance improvements.

Autoloader Classmap

As described by Rob Allen on his Blog.

The class map autoloader is a high performance autoloader. It uses class maps, which are simply associative arrays of each classname to the name of the file disk that contains that class.
    
This is a very simple process and takes just a few minutes to generate and include the classmaps for each module you have created.

Create Classmaps

As you can imagine, creating class maps manually would quickly get tiresome. To alleviate this, Zend Framework 2 provides a PHP script, classmap_generator.php in the `bin` directory that will do this for you. This tool will scan the entire directory from the current directory (or that specified via an option) and create a class map file for every class that it finds. It is used like this:

prompt> path/to/zf2/bin/classmap_generator.php -w
Creating class file map for library in '/var/www/project/library'...
Wrote classmap file to '/var/www/project/library/autoload_classmap.php'
    

Template Map

Most people use the 'template_path_stack' during development. As pointed out by the manual, this can introduce a performance expense.

This is a nice solution for rapid application
development, but potentially introduces performance expense in
production due to the number of stat calls necessary.
    

Once development is completed, consider putting your view's into the template_map as described in the manual.

I have now created a templatemap_generator to be found in my Gist

Run this script from the root of your module example module/Album:

$ php ../../vendor/ZF2/bin/templatemap_generator.php
Creating template file map for library in 'zf2-tutorial/module/Album'...
Wrote templatemap file to 'zf2-tutorial/module/Album/template_map.php'

You can then just include this file in your module.config.php as following:

<?php
return array(
    'view_manager' => array(
        'template_map' => include __DIR__  .'/../template_map.php',
    ),
);

You may also include OcraCachedViewResolver to handle template resolving performance through caching instead.

Module Config Cache

As pointed out by Bakura we should also add a module config cache.

Create a modulecache.local.php file in config/autoload as follows.

<?php
return array(
    // Whether or not to enable a configuration cache.
    // If enabled, the merged configuration will be cached and used in
    // subsequent requests.
    'config_cache_enabled' => true,
    // The key used to create the configuration cache file name.
    'config_cache_key' => 'module_config_cache',
    // The path in which to cache merged configuration.
    'cache_dir' =>  './data/cache',
);
 

This is just a quick write up and i will continue to update this with more options. If you feel anything is missing or should be mentioned, please do not hesitate to comment so i can keep this list updated. Thanks for reading!

 

Comments

Please feel free to leave any comments as long as they're related to the topic.