Symfony2 Slow Initialization Time

PerformanceUbuntuSymfonyVirtualbox

Performance Problem Overview


I have Symfony2 running on an Ubuntu Server 12.04 (64-bit) VM (VirtualBox). The host is a MacBook pro. For some reason I am getting really long request times in development mode (app_dev.php). I know its slower in dev mode, but I'm talking 5-7 seconds per request (sometimes even slower). On my Mac I get request times of 200ms or so in development mode.

After looking at my timeline in the Symfony2 profiler, I noticed that ~95% of the request time is "initialization time". What is this? What are some reasons it could be so slow?

This issue only applies to Symfony2 in dev mode, not any other sites I'm running on the VM, and not even to Symfony2 in production mode.

I saw this (http://stackoverflow.com/questions/11162429/whats-included-in-the-initialization-time-in-the-symfony2-web-profiler), but it doesn't seem to answer my questions.

Performance Solutions


Solution 1 - Performance

I had 5-30 sec responses from Symfony2 by default. Now it's ~500ms in dev environment.

Then I modified the following things in php.ini:

  • set realpath_cache_size = 4M (or more)
  • disabled XDebug completely (test with phpinfo)
  • realpath_cache_ttl=7200
  • enabled and set OPcache (or APC) correctly
  • restarted Apache in order to have php.ini reloaded

And voilá, responses went under 2 secs in dev mode! Hope it helps.

Before: 6779 ms enter image description here

After: 1587 ms

enter image description here

Symfony2 reads classes from thousands of files and that's a slow process. When using a small PHP realpath cache, file paths need to be resolved one by one every time a new request is made in the dev environment if they are not in PHP's realpath cache. The realpath cache is too small by default for Symfony2. In prod this is not a problem of course.

Cache metadata:

Caching the metadata (e.g. mappings) is also very important for further performance boost:

doctrine:
    orm:
        entity_managers:
            default:
                metadata_cache_driver: apc
                query_cache_driver: apc
                result_cache_driver: apc

You need to enable APCu for this. It's APC without bytecode cache, as OPCache already does opcode caching. OPCache is built in since PHP 5.5.

---- After: 467 ms ----

(in prod environment the same response is ~80 ms)

enter image description here

Please note, this is project uses 30+ bundles and has tens of thousands of lines of code, almost hundred own services, so 0.5s is quite good on a local Windows environment using just a few simple optimizations.

Solution 2 - Performance

I figured out the cause of the problem (and its not Symfony2). For some reason on the ubuntu VM, the modification times on certain files are incorrect (ie in the future, etc). When symfony2 checks these times using filemtime() against its registry, it determines that the cache is not longer fresh and it rebuilds the whole thing. I haven't been able to figure out why it is doing that yet.

Solution 3 - Performance

I also needed to disable xdebug (v2.2.21) to debug apache2 max timeout loading on my macbook. It was installed using macports:

sudo port install php54-xdebug.

With xdebug enabled, every page run out max loading time, with a fatal error exceeding max timeout message dispatched. When disabled, everything just loads fine in a reasonable expected time. I came to this using MAMP, no xdebug enabled by default, and apache2 just works fast as usual. I may change for another debugger, that's a pitty, because xdebug worked fine before.

Config:

  • MacOSX 10.6.8
  • macports 2.1.3
  • Apache 2.2.24
  • php 5.4

Solution 4 - Performance

We have the same problem. Here we have 10 second and more for every request. I see if I remove following lines in bootstrap.php.cache all times return in normal state (298 ms).

foreach ($meta as $resource) { 
if (!$resource->isFresh($time)) {
return false;
}
}

It's possible that we have wrong modifications times, but we don't know how to fix. Somebody know a solution?

Solution 5 - Performance

As said at https://stackoverflow.com/a/12967229/6108843 the reason of such behavior might be Ubuntu VM settings. You should to sync date and time between host and guest OS as explained at https://superuser.com/questions/463106/virtualbox-how-to-sync-host-and-guest-time.

File modification date changes to host's value when you upload file to VM via FTP. So that's why filemtime() returns wrong value.

Solution 6 - Performance

You can move APP/var/cache в /dev/shm/YourAppName/var/cache. But it's good to have built container in local files too for IDE autocomplete and code validation. In app/AppKernel.php:

public function getCacheDir()
{
    return $this->getVarOrShmDir('cache/' . $this->getEnvironment());
}

public function getLogDir()
{
    return $this->getVarOrShmDir('logs');
}

private function getVarOrShmDir($dir)
{
    $result = dirname(__DIR__) . '/var/' . $dir;

    if (
        in_array($this->environment, ['dev', 'test'], true) &&
        empty($_GET['warmup']) && // to force using real directory add ?warmup=1 to URL
        is_dir($result) && // first time create real directory, later use shm
        file_exists('/bin/mount') && shell_exec('mount | grep vboxsf') // only for VirtualBox
    ) {
        $result = '/dev/shm/' . 'YourAppName' . '/' . $dir . '/' . $this->getEnvironment();
    }

    return $result;
}

Solution 7 - Performance

I disabled xdebug and it resulted in a decrease loading time from 17s (yea..) to 0.5s.

Solution 8 - Performance

I had problems as well with slow page loads in development, which can extremely frustrating when you're tweaking CSS or something similar.

After a bit of digging I found that for me the problem was caused by Assetic which was recompiling all assets on every page load:

http://symfony.com/doc/current/cookbook/assetic/asset_management.html#dumping-asset-files-in-the-dev-environment

By disabling the use of the Assetic controller I was able to drastically increase my page load. However, as the link above states, this comes at a cost of regenerating your assets whenever you make a change to them (or set a watch on them).

Solution 9 - Performance

In app_dev, all the caches and auto loading is starting from scratch and what I found to be most slow in dev is the orm. I shy away from using orm and focus mainly on dbal because of it, although i probably shouldn't. Orm is used quite a bit in sf2. My guess is orm is what's slowing you down most in dev. Look at the difference between your dev config and prod config. However, some tweaks to your dev config can make development much snappier and enjoyable.. Just try and be aware of what your doing. for example, turning off the twig controller and then modifying a lot of templates will be kind of frustrating. Your need to keep clearing your cache. But like you mentioned, its dev only and when its time to go live, symfony will speed up for you.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionorourkeddView Question on Stackoverflow
Solution 1 - PerformanceDenes PappView Answer on Stackoverflow
Solution 2 - PerformanceorourkeddView Answer on Stackoverflow
Solution 3 - PerformanceJoseph TranView Answer on Stackoverflow
Solution 4 - PerformancecleaversdevView Answer on Stackoverflow
Solution 5 - PerformanceknaView Answer on Stackoverflow
Solution 6 - PerformanceluchaninovView Answer on Stackoverflow
Solution 7 - PerformanceDhofcaView Answer on Stackoverflow
Solution 8 - PerformanceLukeView Answer on Stackoverflow
Solution 9 - PerformanceJustinPView Answer on Stackoverflow