5

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

4
  • Are you running it on Windows (notoriously slow)? Have you read stackoverflow.com/questions/25405360/… ? Commented Nov 17, 2016 at 9: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? Commented Nov 17, 2016 at 9:35
  • 1
    Thx to all yes i forgot to mention it is running on windows :(, and the realpath_cache_size was causing the problem! Commented Nov 17, 2016 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. Commented Nov 17, 2016 at 22:25

2 Answers 2

17

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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?
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: gordalina.github.io/cachetool
3

Do you have xdebug extension enabled in php.ini ?

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

3 Comments

hello, thx for the answer is disabled both in local and remote
Check this post, it might help youhttp://stackoverflow.com/questions/13459157/symfony2-firewall-takes-ages/40618145#40618145
Thx to everyone at the end was the setting realpath_cache_size value. Now looks ok!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.