【问题标题】:Symfony2 performance tweakingSymfony2 性能调整
【发布时间】:2014-01-11 19:12:48
【问题描述】:

Symfony2 看起来如此有前途、强大且灵活。所以我们打算在我们的一个项目中使用 Symfony2 + mongodb。但它看起来太慢了(Apache/2.2.25 + PHP/5.4.20)。目前该应用程序非常简单。但我注意到,当加载一些简单的页面时,httpd.exe 的 CPU 占用率高达 28%。该页面非常精简 - 只有用户个人资料信息和他的帖子列表。如果性能不会好很多,我什至无法想象如何为数百名用户提供服务(甚至不谈论像 10 万用户这样的数字)。

例如,当打开 ActivationCloud 帐户的繁重“产品”页面(获取大量数据)(PHP+Smarty+SQL)时,CPU 负载为 2%。

查看 Xdebug 输出后,我发现 ClassLoader->loadClass(...) 使用了 20% 的大量时间 - 265 次调用

执行以下步骤后:

*生成的类映射

php composer.phar dump-autoload --optimize

*已安装并启用 APC

    [APC]
    extension=php_apc.dll

    apc.enabled=1
    apc.shm_segments=1

    ;32M per WordPress install
    apc.shm_size=128M

    ;Relative to the number of cached files (you may need to 
watch your stats for a day or two to find out a good number)
    apc.num_files_hint=7000

    ;Relative to the size of WordPress
    apc.user_entries_hint=4096

    ;The number of seconds a cache entry is allowed to idle
 in a slot before APC dumps the cache
    apc.ttl=7200
    apc.user_ttl=7200
    apc.gc_ttl=3600

    ;Setting this to 0 will give you the best performance, as APC will
    ;not have to check the IO for changes. However, you must clear 
    ;the APC cache to recompile already cached files. If you are still
    ;developing, updating your site daily in WP-ADMIN, and running W3TC
    ;set this to 1
    apc.stat=1

    ;This MUST be 0, WP can have errors otherwise!
    apc.include_once_override=0

    ;Only set to 1 while debugging
    apc.enable_cli=0

    ;Allow 2 seconds after a file is created before
 it is cached to prevent users from seeing half-written/weird pages
    apc.file_update_protection=2

    ;Leave at 2M or lower. WordPress does't have any file sizes close to 2M
    apc.max_file_size=2M

    ;Ignore files
    apc.filters = "/var/www/apc.php"

    apc.cache_by_default=1
    apc.use_request_time=1
    apc.slam_defense=0
    apc.mmap_file_mask=/var/www/temp/apc.XXXXXX
    apc.stat_ctime=0
    apc.canonicalize=1
    apc.write_lock=1
    apc.report_autofilter=0
    apc.rfc1867=0
    apc.rfc1867_prefix =upload_
    apc.rfc1867_name=APC_UPLOAD_PROGRESS
    apc.rfc1867_freq=0
    apc.rfc1867_ttl=3600
    apc.lazy_classes=0
    apc.lazy_functions=0

期待奇迹发生,但它没有发生。

*启用 APC 类加载器 - 在 Symfony\web\app.php 中未注释

/*
$loader = new ApcClassLoader('sf2', $loader);
$loader->register(true);
*/

ClassLoader->loadClass(...) 变得更好 'Self' 是 11 而不是 21

坦率地说,我对我在 xdebug 中看到的内容感到震惊 :( 很多重复调用,例如 Container->get(...) -317 调用,DocumentManager->getClassMeataData(...) - 301 调用。完全更多超过 2k 的函数调用。难以置信。

这些包已安装:

class AppKernel extends Kernel
{
    public function registerBundles()
    {
        $bundles = array(
            new Symfony\Bundle\FrameworkBundle\FrameworkBundle(),
            new Symfony\Bundle\SecurityBundle\SecurityBundle(),
            new Symfony\Bundle\TwigBundle\TwigBundle(),
            new Symfony\Bundle\MonologBundle\MonologBundle(),
            new Symfony\Bundle\SwiftmailerBundle\SwiftmailerBundle(),
            new Symfony\Bundle\AsseticBundle\AsseticBundle(),
            new Doctrine\Bundle\DoctrineBundle\DoctrineBundle(),
            new Doctrine\Bundle\MongoDBBundle\DoctrineMongoDBBundle(),
            new Sensio\Bundle\FrameworkExtraBundle\SensioFrameworkExtraBundle(),
            new HWI\Bundle\OAuthBundle\HWIOAuthBundle(),
            new Knp\Bundle\MenuBundle\KnpMenuBundle(),
            ... our bundles ...

        );

        if (in_array($this->getEnvironment(), array('dev', 'test'))) {
            $bundles[] = new Symfony\Bundle\WebProfilerBundle\WebProfilerBundle();
            $bundles[] = new Sensio\Bundle\DistributionBundle\SensioDistributionBundle();
            $bundles[] = new Sensio\Bundle\GeneratorBundle\SensioGeneratorBundle();
        }

        return $bundles;
    }

很遗憾地发现 Symfony2 在其他 php 框架中获得了最差的基准测试结果之一http://www.techempower.com/benchmarks/#section=data-r8&hw=i7&test=json&l=sg

同时,Francois Zaninotto 在他的博客http://symfony.com/blog/who-really-uses-symfony 中表示,雅虎使用 Symfony2 来提供书签服务,并尝试了列表中的一些应用程序 http://trac.symfony-project.org/wiki/ApplicationsDevelopedWithSymfony - 他们在 Quora 上也看起来并不慢http://www.quora.com/Who-is-using-Symfony2-in-production 它说dailymotion也在使用它。

如何让性能可以接受?

【问题讨论】:

  • 不确定他的其余答案,但正如 1ed 所述;确保你打开了所有三个 Doctrine 的缓存,它会产生巨大的差异。另外,您是在 prod 还是 dev 环境中运行这些测试?

标签: apache symfony doctrine-orm


【解决方案1】:

添加

后,Symfony 的工作速度提高了 10 倍
realpath_cache_size = 4096k

到 php.ini

【讨论】:

    【解决方案2】:

    首先你应该使用linux(你提到了https.exe,所以我认为你使用的是windows)。比你应该使用nginx 而不是apache 和php-5.5 和fpm 而不是mod_php。 Opcache 而不是 apc(顺便说一下 apc.stat 应该关闭)。应该打开 Doctrine 缓存,并且应该尽可能使用 http 缓存。 (您可以查看packagist's代码以获得一些提示。)

    【讨论】:

    • 在 linux 下运行速度会快很多吗?我们将坚持使用 windows 进行开发,但生产肯定会在 linux 上运行。
    • 我没有确切的数字,但 linux 肯定更快(在谷歌快速搜索后#1,#2)。
    • BTW mod_php 对于 PHP 内容来说比任何 fastcgi 方法都快。 只是,由于每个 Apache 实例的线程限制,它也会“拖累”其他非 php 请求,并且还会对那些加载 mod_php 的非 php 请求施加更高的内存使用量每次。一种简单的解决方案是使用反向代理(例如 varnish)来缓存静态内容并在它到达 Apache 之前提供它们。
    猜你喜欢
    • 2011-07-07
    • 1970-01-01
    • 2012-03-25
    • 2010-10-03
    • 1970-01-01
    • 1970-01-01
    • 2014-03-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多