Symfony 2 性能优化

我们正在寻找一个可以在未来使用的 PHP 框架,目前正在使用 Symfony 2 进行测试.为此,我们重新设计了我们的 API 并将其作为一个包在 Symfony 中实现.事实证明,Symfony 似乎非常慢 - 实际上远比我们旧的(甚至不是精心设计的)系统慢.

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.

我们尝试通过缓存字节码来优化性能(为此使用 APC).虽然我们注意到性能有了巨大的提升(之前:加载 API 大约需要 3 秒;之后:平均为 0.6 秒(仍然比我们的旧系统没有 APC 慢 0.5 秒)),我们'有点兴奋 - 但仍然对如此简单的任务的高加载时间感到不满意,比如从几乎空的数据库中获取一个结果.

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.

我不知道,但我可以想象这是由于 Symfony 自动加载所有类,即使特定包不需要.

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

现在,在我们对 Symfony 进行深度 6 之前,我们希望寻找进一步的优化,可能是一种在特定包中排除不需要的组件的方法,因为我个人认为这会产生很大的不同.

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.

如果您有任何关于如何进一步提高性能的想法、使用 Symfony 的经验报告或其他任何可能对我们寻找框架有所帮助的想法,我将不胜感激.

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.

关于测试环境的一些信息:

Some information about the testing environment:

  • 操作系统:Ubuntu 12.04.4 LTS(GNU/Linux 3.8.0-38-generic x86_64)
  • Apache 版本:Apache/2.2.22 (Ubuntu)
  • PHP 版本:5.3.10-1ubuntu3.13
  • 相当多的 PHP 扩展:apc

此外,所有测试都在我们系统的本地副本上完成,因此可以排除可能的网络问题.

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

推荐答案

这些点可以优化你的应用性能:

These points can optimise your application performance:

  1. 升级 PHP.PHP 5.3 和 PHP 5.4 之间的性能提升非常高.PHP 5.5 会更好,但并非所有发行版都支持它,例如 Debian 7.

  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.

NGINX 比 Apache 更快,配置更简单.

NGINX is faster than Apache and the configuration is easier.

将 PHP-FPM 与 NGINX 结合使用是一个很好的组合.你也可以用 HHVM 运行你的 PHP,它比 PHP-FPM 平均快 2 倍,前提是你用 Grunt 替换 Symfony/Assetic.警告: HHVM 在安全部署之前需要更多的预防措施和测试.您可以关注这两篇文章(法语):JoliCode 和 大大脑公司

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

不推荐使用 PHP APC 扩展.我认为 XCache、Memcached 或 Redis 更好,它们目前也最受支持.对于 PHP >= 5.5,APCu 可以作为 APC 的替代品.

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.

此外,您可以阅读一些关于 Symfony2 优化并提供 Twig 基准的文章.

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

PHP 文章:

  • 脚本高性能法语
  • PHP 5.3 与PHP 5.6 性能 法语版
  • 检查这篇文章来理解 PHP 优化 法语
  • Google 关于优化 PHP 应用程序的建议 英文版s>
  • 10 个优化 PHP 的最佳实践 英文李>
  • 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 和 Twig 文章:

Symfony2 and Twig articles:

  • Symfony 文档提供了一些如何构建高性能应用程序的技巧英语
  • 模板优化法语
  • Twig 包含优化法语
  • 使用英文@Cache注解请求李>
  • Symfony2:良好实践法语
  • 限制使用不必要的捆绑包
  • Symfony2 Twig 性能优化 英文版李>
  • 如果您不想使用 Twig 引擎,您可以禁用它 法语版
  • 在 Symfony 中缓存来自它的 Cookbook - 真的令人印象深刻!
  • 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!

其他优化:

  • 也许您可以使用 Ubuntu 的更新版本.
  • 就我个人而言,我更喜欢使用在服务器中也很流行的 Debian,因为它非常稳定.
  • 使用 Varnish 之类的缓存代理可以起到画龙点睛的作用.
  • Varnish 需要开发者的暗示,也许还需要一种形式.使用 NGINX FastCGI Cache 将 FastCGI 请求限制为 HHVM、FPM 或 PHP-NG 可以解决速度响应问题.

相关文章