Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problems with symfony performance

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

like image 804
yauros Avatar asked Dec 03 '22 14:12

yauros


1 Answers

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.

like image 171
Grzegorz Krauze Avatar answered Dec 20 '22 00:12

Grzegorz Krauze