symfony 性能问题 [英] Problems with symfony performance

查看:97
本文介绍了symfony 性能问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 symfony 2.8.12 在 2 个不同的项目中工作并且遇到相同的问题:性能.

i am working in 2 different projects using symfony 2.8.12 and having the same problem: performance.

加载时间太长(不是所有时间,而是大部分时间),当我查看分析器时,总是有一个有罪的部分":它可以是防火墙、控制器、配置文件侦听器、内核响应......是几秒的执行时间(有时超过 10 秒).

It takes too long to load ( not all the times, but the most part ) and when i look the profiler there is always one "guilty component": it can be the firewall, the controller, the profile listener, the kernel response....being the execution time of several seconds ( sometimes longer than 10 ).

在不同的线程中读取我试图将数据库设置为修复 ip(以防它是 dns 查找问题),修改了 php.ini 中的一些参数,但没有任何改变.这发生在我的本地和远程环境中,甚至启用了 PHP 加速和 OPCache.

Reading in different threads i tried to set the db as a fix ip ( in case it is a dns lookup problem ), modified some parameters in php.ini but nothing changed. This is hapenning both in my local and in the remote environment where it has even PHP acceleration and OPCache enabled.

我没有对我的代码做任何特别的事情,即使我在hello world"页面中停留了很长时间,这还是有点令人沮丧 :)

I am not doing nothing special on my code, even i get long times in "hello world" pages, it's a little frustrating :)

推荐答案

发生这种情况是因为 symfony 在开始打印Hello world"之前要读取数千个文件.事实上,您的硬盘对 symfony 的效率影响最大.幸运的是,只需几个简单的步骤即可达到令人满意的水平.

This is happening because symfony has thousands of files to read before even starting to print "Hello world". In fact your hard drives have greatest impact on efficiency of symfony. Fortunately there is few simple steps to achieve some satisfactory level.

  1. PHP.ini:
    将这两个参数设置为比默认值高得多的值,即
    realpath_cache_size = 4096k
    realpath_cache_ttl = 7200
  2. 转储作曲家自动加载:
    composer dump-autoload --optimize - 使用加载的类创建转储文件
  3. 我不知道您如何使用 opcache,但我鼓励您安装 apcu 模块.之后在 symfony config_prod.yml:
  1. PHP.ini:
    set those two parameters on much higher values than default, i.e.
    realpath_cache_size = 4096k
    realpath_cache_ttl = 7200
  2. dump composer autoload :
    composer dump-autoload --optimize - this creates dump file with loaded classes
  3. I don't know how you use opcache, but I encourage you to install apcu module. After that use metadata cache in symfony config_prod.yml:

doctrine:
orm:
    metadata_cache_driver: apc
    result_cache_driver: apc

  • 与常规行相比,您的 web/app.php 应该看起来有一些额外的行:

  • Your web/app.php should look have some additional lines comparing to regular one:

    use Symfony\Component\HttpFoundation\Request;
    use Symfony\Component\ClassLoader\ApcClassLoader;
    
    $loader = require __DIR__.'/../app/autoload.php';
    include_once __DIR__.'/../app/bootstrap.php.cache';
    
    $apcLoader = new ApcClassLoader(md5($_SERVER['HTTP_HOST']), $loader);
    $loader->unregister();
    $apcLoader->register(true);
    
    require_once __DIR__.'/../app/AppCache.php';
    $kernel = new AppKernel('prod', false);
    $kernel->loadClassCache();
    $kernel = new AppCache($kernel);
    Request::enableHttpMethodParameterOverride();
    $request = Request::createFromGlobals();
    $response = $kernel->handle($request);
    $response->send();
    $kernel->terminate($request, $response);
    

  • 其他但也很重要:

    1. 使用 PHP 7,可显着提高效率,
    2. 将 PHP 与 FPM(FastCGI 进程管理器)结合使用
    3. 不使用 SQL 解决方案来缓存查询,即 Redis、Elasticsearch
    4. 禁用 xdebug - 确保分析器不会显示您使用它.

    这个列表实际上很长,但在最常见的情况下,前 4 点加上第 8 点就可以解决问题.我希望它会有所帮助.

    The list is long in fact, but first 4 points plus the 8th one do the trick in most common cases. I hope it'll help.

    这篇关于symfony 性能问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆