4

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

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 ).

example case example case 2

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.

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

yauros
  • 157
  • 1
  • 3
  • 8
  • Are you running it on Windows (notoriously slow)? Have you read http://stackoverflow.com/questions/25405360/symfony-2-performance-optimisations?rq=1 ? – Veve Nov 17 '16 at 09:16
  • 1
    that's really hard to tell - which OS are you running, which PHP version, are you running Symfony as dev or prod, are you running symfony with the built-in cli php server or on an actual webserver (apache, nginx)? does your cache need to warm-up before every request...? how is your logging configured? is some additional debugging enabled? – LBA Nov 17 '16 at 09:35
  • 1
    Thx to all yes i forgot to mention it is running on windows :(, and the realpath_cache_size was causing the problem! – yauros Nov 17 '16 at 13:33
  • @yauros - the best think to do with windows is to install virtual machine with linux :). But seriously, you should also greater the value of `realpath_cache_ttl`. Take a look at my post also, I encourage you to try metadata and query result cache. You can cache those not only with apc, but also other providers - i.e. memcached. – Grzegorz Krauze Nov 17 '16 at 22:25

2 Answers2

15

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:
    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
    
  4. 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);
    

Other but also very important:

  1. Use PHP 7, which has significant efficiency boost,
  2. Use PHP with FPM (FastCGI Proces Manager)
  3. Use No SQL solution to cache queries, i.e. Redis, Elasticsearch
  4. Disable xdebug - make sure that profiler doesn't show you use it.

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.

Grzegorz Krauze
  • 1,130
  • 12
  • 26
  • Do I need to clear APC cache if I use `metadata_cache_driver: apc` or the standard `php app/console cache:clear --env=prod --no-debug` will suffice? – StockBreak Nov 27 '17 at 08:58
  • If APCu is provided with php.ini then you should clear it every time you change PHP file (or TWIG which is also parsed to PHP). I use this tool to clear the APC cache: https://gordalina.github.io/cachetool/ – Grzegorz Krauze Nov 27 '17 at 09:39
3

Do you have xdebug extension enabled in php.ini ?

If it's the case, try disabling it, specially in production environnement.

progg
  • 294
  • 1
  • 4