PERFORMANCE TIPS 
FOR SYMFONY2 & PHP 
Minsk PHP User Group Meetup #10
Intro 
 Max Romanovsky 
 37 Slides 
 Questions after presentation 
 Out of scope: HW & OS tuning, VM vs HW 
servers 
 “Premature optimization is the root of all evil” 
Knuth’s quote is missing 
 No cats, boobs or other images in slides  
2
Quiz 
3 
 What to optimize? 
 Architecture 
 Code 
 Services
Agenda 
 Overview 
 Measure 
 Services 
 Architecture 
 Code 
 Symfony2 tips 
4
5 Overview
How to start? 
 Forget about microoptimizations for now 
 Курочка по зернышку – весь двор в говне 
 Make it scalable 
 What is cheaper? To scale or to optimize? 
6
Tune 
 Tune services 
 Tune architecture 
 Consider tuning code 
7
Quiz 
8 
 What was the sloooooowest? 
 DB queries 
 Network communication 
 PHP configuration 
 VM/HW server 
 Stupid code
What could be slow 
9 
 Network communication (with client, inside 
LAN) 
 VM/HW server 
 OS & runtime itself 
 Communication with services 
 Code (finally!)
10 Measure
Stop! Measure 
11 
 Measure before 
 Measure during 
 Measure after 
 Measure periodically 
 measure 
 Measure 
 MEASURE!!1
Quiz 
12 
 What do you use? 
 Symfony Profiler 
 symfony/stopwatch 
 Из говна и палок 
 Xhprof 
 Xdebug 
 Zend Profiler
Tools 
13 
 Xhprof / uprofiler 
 Xdebug  
 Framework tools  
  = overhead & irrelevant results
Question 
14 
 Can we compare xhprof & xdebug results? 
 Are they relevant?
Overhead & irrelevant results? 
15 
 Runtime (i.e. code execution) is slower with 
profiler enabled 
 Communication with services is not slower 
 Mess in results 
 You are analyzing DIFFERENT results
Use lightweight profiler 
16 
 XHProf 
 XHgui (saves runs to RDBMS) 
 Uprofiler (fork?) 
 https://github.com/FriendsOfPHP/uprofiler
Measure performance in real 
world 
17 
 Turn on profiler periodically 
 For 1% of requests 
 For 1 app server 
 Record & replay user activity 
 Use Jmeter to record scenarios 
 Use good old ab, siege, http_load
Tune 
18 
 Tune 1 factor at once 
 Store configuration & measuring results in 
VCS 
 Measure after tuning
19 Services
Why? 
20 
 Receive profit without changing a single line of 
code
Question 
21 
 Do you know what is an Opcache? 
 Do you use it?
PHP 
22 
 Don’t use the latest PHP version, please! 
 Use PHP-FPM (or even HHVM?) 
 Opcache or APC 
 APCu 
 Compile stable libraries as PHP extensions 
 Twig 
 Redis 
 AMQP
Composer 
23 
 composer install --optimize-autoloader
24 Architecture
Architecture 
 Make it scalable. Period. 
 Use PAAS services 
 CDN 
 File storage 
 Choose efficient protocols & frameworks 
 Apache Thrift 
 Google Protocol Buffers 
25
Cache 
26 
 Raw data 
 Computed data 
 View layer
Cache 
27 
 Raw data = DB/Data Source 
 MySQL query cache 
 HTTP API reverse proxy cache
Cache 
28 
 Computed data = Key-value 
 Persistent? 
 Consistent? 
 Check your DB features
Cache 
29 
 View layer = HTTP cache 
 Nginx 
 Varnish
30 Code
Why not to optimize 
31 
 Improve architecture 
 Man, it’s PHP! It is designed to be SLOOOOW 
 Optimize code for people, not for machines 
 Курочка по зернышку?.. 
 Well… OK. But profile
What to optimize. Boooring 
32 
 DB 
 Move calculations to DB 
 Move aggregation to DB 
 MOVE LIMIT TO DB!!1 
 Move calculations outside the loop 
 Don’t concatenate string insanely 
 Don’t pass huge arrays by value to functions 
 Don’t use arrays anyway 
33 Symfony2 tips
As seen on TV 
34 
 http://symfony.com/doc/current/book/performa 
nce.html 
 Use a Byte Code Cache 
 Composer's Class Map 
 Caching the Autoloader with APC 
 Use Bootstrap Files
Tipzzz 
35 
 Services 
 Lazy with ocramius/proxy-manager 
 Controllers 
 Console commands 
 Move app/cache to tmpfs 
 Twig C extension 
 Don’t use router:dump. It’s deprecated 
 https://github.com/symfony/symfony/issues/9652 
 Don’t use Symfony Profiler for profiling 
More? 
36 
 http://symfony.com/blog/push-it-to-the-limits-symfony2- 
for-high-performance-needs 
 https://www.youtube.com/watch?v=b8ewqVGu 
77w 
 https://profiler.sensiolabs.com/ - still waiting for 
invite
The End 
37 
 max.romanovsky@gmail.com 
 http://maxromanovsky.com

Performance tips for Symfony2 & PHP

  • 1.
    PERFORMANCE TIPS FORSYMFONY2 & PHP Minsk PHP User Group Meetup #10
  • 2.
    Intro  MaxRomanovsky  37 Slides  Questions after presentation  Out of scope: HW & OS tuning, VM vs HW servers  “Premature optimization is the root of all evil” Knuth’s quote is missing  No cats, boobs or other images in slides  2
  • 3.
    Quiz 3 What to optimize?  Architecture  Code  Services
  • 4.
    Agenda  Overview  Measure  Services  Architecture  Code  Symfony2 tips 4
  • 5.
  • 6.
    How to start?  Forget about microoptimizations for now  Курочка по зернышку – весь двор в говне  Make it scalable  What is cheaper? To scale or to optimize? 6
  • 7.
    Tune  Tuneservices  Tune architecture  Consider tuning code 7
  • 8.
    Quiz 8 What was the sloooooowest?  DB queries  Network communication  PHP configuration  VM/HW server  Stupid code
  • 9.
    What could beslow 9  Network communication (with client, inside LAN)  VM/HW server  OS & runtime itself  Communication with services  Code (finally!)
  • 10.
  • 11.
    Stop! Measure 11  Measure before  Measure during  Measure after  Measure periodically  measure  Measure  MEASURE!!1
  • 12.
    Quiz 12 What do you use?  Symfony Profiler  symfony/stopwatch  Из говна и палок  Xhprof  Xdebug  Zend Profiler
  • 13.
    Tools 13 Xhprof / uprofiler  Xdebug   Framework tools    = overhead & irrelevant results
  • 14.
    Question 14 Can we compare xhprof & xdebug results?  Are they relevant?
  • 15.
    Overhead & irrelevantresults? 15  Runtime (i.e. code execution) is slower with profiler enabled  Communication with services is not slower  Mess in results  You are analyzing DIFFERENT results
  • 16.
    Use lightweight profiler 16  XHProf  XHgui (saves runs to RDBMS)  Uprofiler (fork?)  https://github.com/FriendsOfPHP/uprofiler
  • 17.
    Measure performance inreal world 17  Turn on profiler periodically  For 1% of requests  For 1 app server  Record & replay user activity  Use Jmeter to record scenarios  Use good old ab, siege, http_load
  • 18.
    Tune 18 Tune 1 factor at once  Store configuration & measuring results in VCS  Measure after tuning
  • 19.
  • 20.
    Why? 20 Receive profit without changing a single line of code
  • 21.
    Question 21 Do you know what is an Opcache?  Do you use it?
  • 22.
    PHP 22 Don’t use the latest PHP version, please!  Use PHP-FPM (or even HHVM?)  Opcache or APC  APCu  Compile stable libraries as PHP extensions  Twig  Redis  AMQP
  • 23.
    Composer 23 composer install --optimize-autoloader
  • 24.
  • 25.
    Architecture  Makeit scalable. Period.  Use PAAS services  CDN  File storage  Choose efficient protocols & frameworks  Apache Thrift  Google Protocol Buffers 25
  • 26.
    Cache 26 Raw data  Computed data  View layer
  • 27.
    Cache 27 Raw data = DB/Data Source  MySQL query cache  HTTP API reverse proxy cache
  • 28.
    Cache 28 Computed data = Key-value  Persistent?  Consistent?  Check your DB features
  • 29.
    Cache 29 View layer = HTTP cache  Nginx  Varnish
  • 30.
  • 31.
    Why not tooptimize 31  Improve architecture  Man, it’s PHP! It is designed to be SLOOOOW  Optimize code for people, not for machines  Курочка по зернышку?..  Well… OK. But profile
  • 32.
    What to optimize.Boooring 32  DB  Move calculations to DB  Move aggregation to DB  MOVE LIMIT TO DB!!1  Move calculations outside the loop  Don’t concatenate string insanely  Don’t pass huge arrays by value to functions  Don’t use arrays anyway 
  • 33.
  • 34.
    As seen onTV 34  http://symfony.com/doc/current/book/performa nce.html  Use a Byte Code Cache  Composer's Class Map  Caching the Autoloader with APC  Use Bootstrap Files
  • 35.
    Tipzzz 35 Services  Lazy with ocramius/proxy-manager  Controllers  Console commands  Move app/cache to tmpfs  Twig C extension  Don’t use router:dump. It’s deprecated  https://github.com/symfony/symfony/issues/9652  Don’t use Symfony Profiler for profiling 
  • 36.
    More? 36 http://symfony.com/blog/push-it-to-the-limits-symfony2- for-high-performance-needs  https://www.youtube.com/watch?v=b8ewqVGu 77w  https://profiler.sensiolabs.com/ - still waiting for invite
  • 37.
    The End 37  max.romanovsky@gmail.com  http://maxromanovsky.com