Symfony2性能调整

时间:2014-01-11 19:12:48

标签: apache symfony doctrine-orm

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

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

在看了Xdebug输出后,我发现ClassLoader-> loadClass(...)使用了20%的gret处理时间 - 265次调用Xdebug Symfony2 app

执行以下步骤后:

*生成类地图

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 Symfony2 app after some tweaking

坦率地说,我对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上看起来也不慢{{3它说日常运动也在使用它。

如何使表现可以接受?

2 个答案:

答案 0 :(得分:5)

添加

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

到php.ini

答案 1 :(得分:0)

首先你应该使用linux(你提到过https.exe所以我认为你正在使用windows)。您应该使用nginx代替apache而php-5.5代替fpm而不是mod_php。 Opcache代替apc(顺便说一下apc.stat应该关闭)。应该打开Doctrine缓存,而不是在任何地方使用http缓存。 (您可以查看packagist's代码以获取一些提示。)