Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony 2 performance optimisations

We're looking for a PHP framework to work with in future and are currently testing out things with Symfony 2. For this, we've redesigned our API and implemented it as a bundle in Symfony. It turned out that Symfony seems to be very slow - actually far slower than our old (not even well-designed) system.

We tried to optimise the performance by caching the byte code (using APC for this). While we've noticed a huge improvement in performance (before: about 3 seconds to load the API; after: 0.6 seconds in average (still 0.5 seconds slower than our old system without APC)), we're kind of excited - but still not really pleased with the high loading time of such an easy task like getting one result out of an almost empty database.

I don't know, but I could imagine this is due to Symfony autoloading all classes, even when not needed for a specific bundle.

Now, before we deep-six Symfony, we'd like to look out for further optimisations, possibly a way to exclude unneeded components in a specific bundle, as I personally think this would make a big difference.

I'd be thankful for any ideas on how to further improve the performance, experience reports with using Symfony or anything else that could be helpful for us on the lookout for a framework.

Edit:

Some information about the testing environment:

  • Operating system: Ubuntu 12.04.4 LTS (GNU/Linux 3.8.0-38-generic x86_64)
  • Apache version: Apache/2.2.22 (Ubuntu)
  • PHP version: 5.3.10-1ubuntu3.13
  • Considerable PHP extensions: apc

Also, all tests are done on a local copy of our system, so possible network issues can be excluded.

like image 781
Marcello Mönkemeyer Avatar asked Aug 20 '14 12:08

Marcello Mönkemeyer


2 Answers

These points can optimise your application performance:

  1. Upgrade PHP. The performance gain between PHP 5.3 and PHP 5.4 is very high. PHP 5.5 would be even better, but it's not supported by all distributions, like Debian 7.

  2. NGINX is faster than Apache and the configuration is easier.

  3. Using PHP-FPM with NGINX is a good combination. You can also run your PHP with HHVM which is in average 2x faster than PHP-FPM, provided you replace Symfony/Assetic with Grunt. Caution: HHVM requires more precaution and testing before deploying safely. You can follow these two articles (in French): JoliCode and Big Brains Company

  4. PHP APC extension is deprecated. I think that XCache, Memcached or Redis are better, and they're also most supported at the moment. For PHP >= 5.5, APCu can be used as a replacement for APC.

Additionally, you can read a few articles which talk about Symfony2 optimisation and provide Twig benchmarks.

PHP articles:

  • Script high-performance in French
  • Comparison of PHP 5.3 and PHP 5.6 performance in French
  • Check this article to apprehend the PHP optimisation in French
  • Google's recommendations for optimising PHP applications in English
  • 10 best pratices to optimise PHP in English

Symfony2 and Twig articles:

  • Symfony documentation gives a few tips how to build a performant application in English
  • Template optimisation in French
  • Twig include optimisation in French
  • Use @Cache annotation requests in English
  • Symfony2: Good practices in French
  • Limit the usage of unnecessary bundles
  • Symfony2 Twig performance optimisation in English
  • If you don't want to use the Twig Engine, you can disable it in French
  • Caching in Symfony from it's Cookbook - really impressive!

Others optimisations:

  • Maybe you can use an updated version of Ubuntu.
  • Personally, I prefer using Debian which is also popular for servers because it's very stable.
  • Using a cache proxy like Varnish can be a finishing touch.
  • Varnish requires a developer's implication and maybe a formation. Using NGINX FastCGI Cache to limit FastCGI requests to HHVM, FPM or PHP-NG can resolve speed response.
like image 109
Baptiste Donaux Avatar answered Oct 16 '22 22:10

Baptiste Donaux


Did you look this blog post ? http://symfony.com/blog/push-it-to-the-limits-symfony2-for-high-performance-needs

According to your information, i can advise you to try php 5.5 or 5.6 and NGINX with PHP-FPM, it can 40% faster or more.

like image 29
Fabien MEYNARD Avatar answered Oct 16 '22 22:10

Fabien MEYNARD