Making Zend-Framework run faster
Asked Answered
V

4

6

What are the best ways to make Zend-Framwork run faster besides Zend Optimizer?

If I remember correctly, parsing .ini files in PHP takes a long time. Therefor I cache it (the file won't change during a request)

Are there any other ways to improve ZF's performance?

Varanasi answered 25/10, 2010 at 16:5 Comment(0)
V
8

I cache my application.ini Like this:

Make sure you have the following directory (your cache dir): /application/data/cache

I extend Zend_Application with My_Application, see code:

<?php
require_once 'Zend/Application.php';

class My_Application extends Zend_Application
{

    /**
     * Flag used when determining if we should cache our configuration.
     */
    protected $_cacheConfig = false;

    /**
     * Our default options which will use File caching
     */
    protected $_cacheOptions = array(
        'frontendType' => 'File',
        'backendType' => 'File',
        'frontendOptions' => array(),
        'backendOptions' => array()
    );

    /**
     * Constructor
     *
     * Initialize application. Potentially initializes include_paths, PHP
     * settings, and bootstrap class.
     *
     * When $options is an array with a key of configFile, this will tell the
     * class to cache the configuration using the default options or cacheOptions
     * passed in.
     *
     * @param  string                   $environment
     * @param  string|array|Zend_Config $options String path to configuration file, or array/Zend_Config of configuration options
     * @throws Zend_Application_Exception When invalid options are provided
     * @return void
     */
    public function __construct($environment, $options = null)
    {
        if (is_array($options) && isset($options['configFile'])) {
            $this->_cacheConfig = true;

            // First, let's check to see if there are any cache options
            if (isset($options['cacheOptions']))
                $this->_cacheOptions =
                    array_merge($this->_cacheOptions, $options['cacheOptions']);

            $options = $options['configFile'];
        }
        parent::__construct($environment, $options);
    }

    /**
     * Load configuration file of options.
     *
     * Optionally will cache the configuration.
     *
     * @param  string $file
     * @throws Zend_Application_Exception When invalid configuration file is provided
     * @return array
     */
    protected function _loadConfig($file)
    {
        if (!$this->_cacheConfig)
            return parent::_loadConfig($file);

        require_once 'Zend/Cache.php';
        $cache = Zend_Cache::factory(
            $this->_cacheOptions['frontendType'],
            $this->_cacheOptions['backendType'],
            array_merge(array( // Frontend Default Options
                'master_file' => $file,
                'automatic_serialization' => true
            ), $this->_cacheOptions['frontendOptions']),
            array_merge(array( // Backend Default Options
                'cache_dir' => APPLICATION_PATH . '/data/cache'
            ), $this->_cacheOptions['backendOptions'])
        );

        $config = $cache->load('Zend_Application_Config');
        if (!$config) {
            $config = parent::_loadConfig($file);
            $cache->save($config, 'Zend_Application_Config');
        }

        return $config;
    }
}

And I change my index.php (in the public root) to:

<?php

// Define path to application directory
defined('APPLICATION_PATH')
    || define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../application'));

// Define application environment
defined('APPLICATION_ENV')
    || define('APPLICATION_ENV', (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV') : 'production'));

// Ensure library/ is on include_path
set_include_path(implode(PATH_SEPARATOR, array(
    realpath(APPLICATION_PATH . '/../library'),
    get_include_path(),
)));

/** My_Application */
require_once 'My/Application.php';

// Create application, bootstrap, and run
$application = new My_Application(
    APPLICATION_ENV,
    array(
            'configFile' => APPLICATION_PATH . '/configs/application.ini'
    )
);
$application->bootstrap()
            ->run();

Reload the page and you see the ini file cached. Good luck.

Varanasi answered 25/10, 2010 at 16:27 Comment(3)
What is the performance increase when the application.ini is cached? Do you have benchmarks for this?Fayalite
It depends on the size of your ini file.. See it as caching your CSS file and loading it with or without cache.Varanasi
@Jurian 2-5% on my (ini-heavy) application.Shortcake
O
5

Parsing .ini files may be a little slow, but I wouldn't expect that to be anywhere near the slowest part of a typical ZF application. Without seeing any results, it seems like including a bunch of files (Zend_Cache_*) may in some cases be even slower than parsing a simple .ini file. Anyway, that's just one area...

ZF has published a good guide on optimization: http://framework.zend.com/manual/en/performance.classloading.html

In short,

  1. Utilize caching where it matters: database queries / complex operations, full page cache, etc.
  2. Strip require_once calls in favor of auto-loading as per the documentation.
  3. Cache PluginLoader file/class map

If you want to get a bit more into it,

  1. Skip using Zend_Application component
  2. Enable some sort of op-code cache
  3. Do other typical PHP optimization methods (profiling, memory caching, etc.)
Osteopath answered 17/11, 2010 at 17:26 Comment(4)
Once the Zend_Cache_* are in the opcode cache, they are no longer slow. If you don't cache the parsed INI file, it will always be slow. In my application, before we started optimising, parsing INI files was the vast majority (75-90%) of a request. Great for ease of configuration, not so hot for speed.Shortcake
75-90% of your request ? You're serious ?Braided
Sorry that should read the bootstrapping process. 70% was the whole Zend_Application / bootstrap process. Zend_Config was actually quite slow, but definitely not 70%.Osteopath
The link in your answer appears to be dead now. I'm guessing it was this: framework.zend.com/manual/1.12/en/performance.classloading.htmlUlane
I
0

see also http://www.kimbs.cn/2009/06/caching-application-ini-for-zend-framework-apps/

Independence answered 17/11, 2010 at 7:45 Comment(1)
This is more of a comment than answer. At least provide some explanation in case the link dies in the future.Ulane
S
0

Why did you deleted your last question? I had a good link for you:

I heard things like this before, but the combination is often related to migration from one platform to other.

Check at this link:

http://devblog.policystat.com/php-to-django-changing-the-engine-while-the-c

Synn answered 18/11, 2010 at 16:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.