简体   繁体   English

Symfony2慢速初始化时间

[英]Symfony2 Slow Initialization Time

I have Symfony2 running on an Ubuntu Server 12.04 (64-bit) VM (VirtualBox). 我在一个Ubuntu Server 12.04(64位)VM(VirtualBox)上运行Symfony2。 The host is a MacBook pro. 主持人是MacBook专业版。 For some reason I am getting really long request times in development mode (app_dev.php). 出于某种原因,我在开发模式(app_dev.php)中获得了非常长的请求时间。 I know its slower in dev mode, but I'm talking 5-7 seconds per request (sometimes even slower). 我知道它在开发模式下速度较慢,但​​每个请求我说5-7秒(有时甚至更慢)。 On my Mac I get request times of 200ms or so in development mode. 在我的Mac上,我在开发模式下获得200ms左右的请求时间。

After looking at my timeline in the Symfony2 profiler, I noticed that ~95% of the request time is "initialization time". 在Symfony2分析器中查看我的时间线后,我注意到~95%的请求时间是“初始化时间”。 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. 此问题仅适用于开发模式下的Symfony2,而不适用于我在VM上运行的任何其他站点,甚至不适用于生产模式下的Symfony2。

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. 我看到了这一点(http://stackoverflow.com/questions/11162429/whats-included-in-the-initialization-time-in-the-symfony2-web-profiler),但它似乎没有回答我的问题。

I had 5-30 sec responses from Symfony2 by default. 默认情况下,我有来自Symfony2的5-30秒响应。 Now it's ~500ms in dev environment. 现在它在开发环境中约为500毫秒

Then I modified the following things in php.ini : 然后我在php.ini修改了以下内容:

  • set realpath_cache_size = 4M (or more) 设置realpath_cache_size = 4M (或更多)
  • disabled XDebug completely (test with phpinfo ) 完全禁用XDebug (用phpinfo测试)
  • realpath_cache_ttl=7200 realpath_cache_ttl = 7200
  • enabled and set OPcache (or APC) correctly 启用并正确设置OPcache (或APC)
  • restarted Apache in order to have php.ini reloaded 重新启动Apache以便重新加载php.ini

And voilá, responses went under 2 secs in dev mode! 而且,在开发模式下,响应不到2秒! Hope it helps. 希望能帮助到你。

Before: 6779 ms 之前: 6779毫秒 在此输入图像描述

After: 1587 ms 之后: 1587毫秒

在此输入图像描述

Symfony2 reads classes from thousands of files and that's a slow process. Symfony2从数千个文件中读取类,这是一个缓慢的过程。 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. 当使用小型PHP实时路径缓存时,如果它们不在PHP的实际路径缓存中,则每次在开发环境中发出新请求时,都需要逐个解析文件路径。 The realpath cache is too small by default for Symfony2. Symfony2默认情况下,实际路径缓存太小。 In prod this is not a problem of course. 在产品中,这当然不是问题。

Cache metadata: 缓存元数据:

Caching the metadata (eg 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. 您需要为此启用APCu It's APC without bytecode cache, as OPCache already does opcode caching. 这是没有字节码缓存的APC ,因为OPCache已经进行了操作码缓存。 OPCache is built in since PHP 5.5. OPCache自PHP 5.5开始内置。

---- After: 467 ms ---- ---- 之后: 467 ms ----

(in prod environment the same response is ~80 ms) (在prod环境中相同的响应是~80 ms)

在此输入图像描述

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. 请注意,这个项目使用30多个捆绑包,并且拥有数万行代码,几乎有100个自己的服务,所以0.5s在本地Windows环境中使用一些简单的优化就相当不错。

I figured out the cause of the problem (and its not Symfony2). 我找出了问题的原因(而不是Symfony2)。 For some reason on the ubuntu VM, the modification times on certain files are incorrect (ie in the future, etc). 由于某些原因,在ubuntu VM上,某些文件的修改时间不正确(即将来等)。 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. 当symfony2使用filemtime()针对其注册表检查这些时间时,它确定缓存不再是新鲜的并且它重建整个事物。 I haven't been able to figure out why it is doing that yet. 我还没弄清楚为什么会这样做。

I also needed to disable xdebug (v2.2.21) to debug apache2 max timeout loading on my macbook. 我还需要禁用xdebug (v2.2.21)来调试我的macbook上的apache2 max timeout loading。 It was installed using macports: 它是使用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. 启用xdebug后,每个页面都会耗尽最大加载时间,并且会发送致命错误,超出调度的最大超时消息。 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. 我使用MAMP来实现这一点,默认情况下没有启用xdebug,并且apache2像往常一样快速运行。 I may change for another debugger, that's a pitty, because xdebug worked fine before. 我可能会换另一个调试器,这很糟糕,因为xdebug以前工作正常。

Config: 配置:

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

We have the same problem. 我们有同样的问题。 Here we have 10 second and more for every request. 在这里,每个请求我们有10秒或更多。 I see if I remove following lines in bootstrap.php.cache all times return in normal state (298 ms). 我看看我是否删除了bootstrap.php.cache中的以下行,所有时间都返回正常状态(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? 有人知道解决方案吗?

As said at https://stackoverflow.com/a/12967229/6108843 the reason of such behavior might be Ubuntu VM settings. https://stackoverflow.com/a/12967229/6108843所述,此类行为的原因可能是Ubuntu VM设置。 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 . 您应该按照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. 通过FTP将文件上载到VM时,文件修改日期将更改为主机的值。 So that's why filemtime() returns wrong value. 这就是为什么filemtime()返回错误的值。

You can move APP/var/cache в /dev/shm/YourAppName/var/cache . 您可以移动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. 但是,对于IDE自动完成和代码验证,在本地文件中构建容器也是很好的。 In app/AppKernel.php : 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;
}

我禁用了xdebug,它导致加载时间从17秒(是啊......)减少到0.5秒。

I had problems as well with slow page loads in development, which can extremely frustrating when you're tweaking CSS or something similar. 我在开发过程中遇到了慢速页面加载的问题,当你调整CSS或类似东西时,这会非常令人沮丧。

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: 经过一番挖掘后,我发现对我来说问题是由Assetic引起的,它正在重新编译每个页面加载的所有资产:

http://symfony.com/doc/current/cookbook/assetic/asset_management.html#dumping-asset-files-in-the-dev-environment 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. 通过禁用Assetic控制器,我能够大幅增加页面负载。 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). 但是,正如上面的链接所述,无论何时对资产进行更改(或对其进行设置),都需要重新生成资产。

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. 在app_dev中,所有的缓存和自动加载都是从头开始的,我发现开发中最慢的是orm。 I shy away from using orm and focus mainly on dbal because of it, although i probably shouldn't. 我回避使用orm并主要关注dbal因为它,尽管我可能不应该。 Orm is used quite a bit in sf2. orm在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. 看看你的dev config和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. 但是,对你的dev配置进行一些调整可以使开发变得更加快捷和愉快。只需尝试并注意你的行为。 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. 但是就像你提到的那样,只有它的开发时间和它的上线时间,symfony会为你加速。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM