Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
PHP Go VROOOM!    How to take a site from slow to go
   This talk is for beginners   This is not “how to profile PHP”   This is not “how to use HipHop”   This is not how t...
PHP is NOT your problem!**95.99% of the time
   Client Side                 IO   Server Side                 Database                                Frameworks   ...
Client SideDo you want some Expires with that?
 http://developer.yahoo.com/yslow/ http://code.google.com/speed/page-  speed/ http://tools.pingdom.com/ http://www.fid...
   Make it external   One file – minimized and compressed    (minify)   Put it at the top   Use sprites for images   ...
   Make it external   One file – minimized and compressed    (minify)   Put it at the bottom   Or defer the loading co...
   Use the right format – png, gif or jpg   Compress that image – lots of    compression tools available   Keep them sm...
 Keep them small Keep them very domain specificCookies (om nom nom)
   Compress, compress, compress   Use a CDN and/or media server   Reduce DNS Lookups   No 404‟s, empty img/hrefs   Av...
   Use cache control headers!    ◦   Expires    ◦   Last-Modified    ◦   (best)    ◦   Cache-Control    ◦   Etag    ◦   (...
 http://developer.yahoo.com/performance/  rules.html http://code.google.com/speed/page-  speed/docs/rules_intro.html ht...
Server SetupHave you hugged your sysadmin today?
 Apache Bench Siege Http_Load JMeterProfile, Don’t Guess!
 Security Speed Some Pain (worth it) Compile your own – it‟s faster then the  distros!    ◦ Drop your debug data    ◦ ...
“memcache does precisely f**k all for opcode caching” – Michael MacleanThis is the #1 best way to speed up your PHP siteIn...
   Simple servers for static content   Cache full pages and bypass PHP   Dynamic images and CSS are a pain, find    ano...
Pick the right Tools
 Add more CPUs or more cores Add more RAM Use RAID or Solid State drives Network can definitely be a bottleneck –  but...
 http://assets.en.oreilly.com/1/event/44/S  calable%20Internet%20Architectures%20  Presentation%202.pdf http://ilia.ws/f...
DatabaseUse indexes people!
 Mysql – has profiler built in Postgresql – turn on logging and use  analyzer on logs SQL Server – built in profiler M...
   Do not guess   Add indexes where needed   Sometimes it‟s faster to do the work in    PHP   Log on the PHP side, cou...
   Query Caching    ◦ PHP side – cache generated queries    ◦ Server side – query cache   Results Caching    ◦ Memcached...
 Use transactions as appropriate Normalize your data (or use nosql) Minimize connections to the db Foreign keys can be...
 http://dev.mysql.com/doc/refman/5.0/en/  optimization.html http://wiki.postgresql.org/wiki/Performan  ce_Optimization ...
PHP Optimizationecho not print… are you kidding me?
 XhProf Xdebug DBG (free and pay versions) APD – older but serviceable Inclued Memtrack Scream (also in xdebug)Prof...
“We should forget about small efficiencies,say about 97% of the time: premature  optimization is the root of all evil.”- D...
 In-memory sessions are faster than disk  or database equivalents. (mm, apc,  wincache or memcache) Cache pages partiall...
   Gearman it   Fork it   Cron it   Exec it   Parallelization needs communication    though!Farm it out
 Reflection is nice – but it is VERY slow and  memory heavy This kind of job is much better when  handed off to C (yaml ...
   Avoid lots of stats   Use an opcode cache (yes, I‟m saying it    AGAIN)   Try not to have too many files in a    dir...
“Im saying that if your autoloader is a  significant fraction of the work your app  does, then you are either Doing It  Wr...
 NFS is of the devil – do not use it Errors are always created, even if you  don‟t see them    ◦ error_reporting(-1);  ...
   „‟ is a wee bit faster than “”   echo not print   Don‟t use magic methods   ++$i not $i++   $_SERVER[REQUEST_TIME]...
   http://xdebug.org   https://github.com/jokkedk/webgrind   https://github.com/facebook/xhprof   http://pecl.php.net/...
FrameworksSure you wrote the code faster…Trick them into running faster…
“You are trading „development speed‟ for  „processing speed‟ … all you do is waste all day  reading docs to produce a half...
   Remove require/include calls and use    autoloader or include_all files   Avoid lots of configuration in parseable   ...
   Switch to another system (lots of importers)   Use a cache plugin – or two, or three   Keep wordpress up to date   ...
   Install a Cache          Install a Cache    plugin                    plugin   Limit other Plugins      Limit other...
   http://groups.drupal.org/high-performance   http://codex.wordpress.org/WordPress_Opti    mization   http://www.jooml...
Write it in CWhen PHP actually IS the problem
   http://emsmith.net   http://joind.in/3422   auroraeosrose@gmail.com   IRC – freenode – auroraeosrose   #php-gtk #c...
Upcoming SlideShare
Loading in …5
×

Php go vrooom!

2,652 views

Published on

Given at php tek 2011, a frontend to server setup tour of how to squeeze performance out of your PHP powered website.

Published in: Technology
  • Be the first to comment

  • Be the first to like this

Php go vrooom!

  1. 1. PHP Go VROOOM! How to take a site from slow to go
  2. 2.  This talk is for beginners This is not “how to profile PHP” This is not “how to use HipHop” This is not how to profile your network Velma would be bored (you might be too) I will no doubt rantDisclaimer
  3. 3. PHP is NOT your problem!**95.99% of the time
  4. 4.  Client Side  IO Server Side  Database  Frameworks  Hardware Let the Lecture Begin!Why your app is slow…
  5. 5. Client SideDo you want some Expires with that?
  6. 6.  http://developer.yahoo.com/yslow/ http://code.google.com/speed/page- speed/ http://tools.pingdom.com/ http://www.fiddler2.com/fiddler2/Profile – Don’t Guess!
  7. 7.  Make it external One file – minimized and compressed (minify) Put it at the top Use sprites for images Avoid expressions No @import Use conditional tags for IE issues, not css hacks Specific Selectors Give images dimensionsCSS
  8. 8.  Make it external One file – minimized and compressed (minify) Put it at the bottom Or defer the loading completely Use a well maintained framework document.write is a bad bad thingJavascript
  9. 9.  Use the right format – png, gif or jpg Compress that image – lots of compression tools available Keep them small Scale them on the server, not with html size/width Have a small, cacheable favicon.ico Use sprites for layoutsImages
  10. 10.  Keep them small Keep them very domain specificCookies (om nom nom)
  11. 11.  Compress, compress, compress Use a CDN and/or media server Reduce DNS Lookups No 404‟s, empty img/hrefs Avoid redirects Parallelize downloads Watch external script use! (fb, google, social anything)HTTP Requests
  12. 12.  Use cache control headers! ◦ Expires ◦ Last-Modified ◦ (best) ◦ Cache-Control ◦ Etag ◦ (harder to get right) Be kind to proxies as wellClient Side Caching
  13. 13.  http://developer.yahoo.com/performance/ rules.html http://code.google.com/speed/page- speed/docs/rules_intro.html http://www.w3.org/Protocols/rfc2616/rfc2 616.html Use the GoogleReference
  14. 14. Server SetupHave you hugged your sysadmin today?
  15. 15.  Apache Bench Siege Http_Load JMeterProfile, Don’t Guess!
  16. 16.  Security Speed Some Pain (worth it) Compile your own – it‟s faster then the distros! ◦ Drop your debug data ◦ Tune to your architecture ◦ At the very least do PHP, Server, DatabaseUpgrade your Software
  17. 17. “memcache does precisely f**k all for opcode caching” – Michael MacleanThis is the #1 best way to speed up your PHP siteInstall APC (or something)
  18. 18.  Simple servers for static content Cache full pages and bypass PHP Dynamic images and CSS are a pain, find another solution CDN solutions are more affordable then ever before run a reverse proxy-cacheDon’t Use PHP and Use a CDN
  19. 19. Pick the right Tools
  20. 20.  Add more CPUs or more cores Add more RAM Use RAID or Solid State drives Network can definitely be a bottleneck – but like PHP it‟s the last thing to fix - hire a really smart guyHardware
  21. 21.  http://assets.en.oreilly.com/1/event/44/S calable%20Internet%20Architectures%20 Presentation%202.pdf http://ilia.ws/files/Moscow2010_Optimizat ion_Mistakes.pdf http://people.apache.org/~sctemme/Apco nEU2006/FR4/FR4_Performance_Up.pdf http://omniti.com/surge/2011Resources
  22. 22. DatabaseUse indexes people!
  23. 23.  Mysql – has profiler built in Postgresql – turn on logging and use analyzer on logs SQL Server – built in profiler MongoDB – built in profilerProfile – Don’t Guess!
  24. 24.  Do not guess Add indexes where needed Sometimes it‟s faster to do the work in PHP Log on the PHP side, count on the PHP side… then do it on the server side too Always try to do fewer queriesAnalyze your Queries
  25. 25.  Query Caching ◦ PHP side – cache generated queries ◦ Server side – query cache Results Caching ◦ Memcached is your friend  Libmemcached = php‟s memcached (faster, non- portable)  php sockets = php‟s memcache (slower, completely portable) ◦ File caching – sometimes IO is faster then dbCache Everything
  26. 26.  Use transactions as appropriate Normalize your data (or use nosql) Minimize connections to the db Foreign keys can be slower, use cautiouslyBest Practices are “Best” for aReason
  27. 27.  http://dev.mysql.com/doc/refman/5.0/en/ optimization.html http://wiki.postgresql.org/wiki/Performan ce_Optimization http://msdn.microsoft.com/en- us/library/ff647793.aspxResources
  28. 28. PHP Optimizationecho not print… are you kidding me?
  29. 29.  XhProf Xdebug DBG (free and pay versions) APD – older but serviceable Inclued Memtrack Scream (also in xdebug)Profile – Don’t Guess
  30. 30. “We should forget about small efficiencies,say about 97% of the time: premature optimization is the root of all evil.”- Donald Knuth“Knowing when optimization is premature defines the difference between the master engineer and the apprentice.”- Theo SchlossnagleThe Boss knows Best
  31. 31.  In-memory sessions are faster than disk or database equivalents. (mm, apc, wincache or memcache) Cache pages partially, or fully – store in memcache, have apache bypass PHP Cache any and everything Cache in layers – browser, full page, partial page, data, query results, application settingsCache
  32. 32.  Gearman it Fork it Cron it Exec it Parallelization needs communication though!Farm it out
  33. 33.  Reflection is nice – but it is VERY slow and memory heavy This kind of job is much better when handed off to C (yaml extension, for example) Writing extra code is worth the performance improvement Just because you CAN do something in PHP just like $lang doesn‟t mean that‟s the best solutionAvoid Runtime Introspection
  34. 34.  Avoid lots of stats Use an opcode cache (yes, I‟m saying it AGAIN) Try not to have too many files in a directory include_path as short as possible Use absolute pathsFile and I/O
  35. 35. “Im saying that if your autoloader is a significant fraction of the work your app does, then you are either Doing It Wrong™, or really unlikely, Doing It Very Well Indeed™ I know which I favour” -Michael MacleanBalance Speed and MaintainabilityAutoload and Includes
  36. 36.  NFS is of the devil – do not use it Errors are always created, even if you don‟t see them ◦ error_reporting(-1); @ is terrible! Set and restore error handler if you know something will spit, it‟s STILL fasterVery Bad Things
  37. 37.  „‟ is a wee bit faster than “” echo not print Don‟t use magic methods ++$i not $i++ $_SERVER[REQUEST_TIME] not time() static methods are faster $function() not call_user_func(„function‟) arrays are faster then objectsMicro Optimizations (Here be Evil)
  38. 38.  http://xdebug.org https://github.com/jokkedk/webgrind https://github.com/facebook/xhprof http://pecl.php.net/package/inclued https://github.com/eexit/Inclued http://pecl.php.net/package/scream http://pecl.php.net/package/memtrackResources
  39. 39. FrameworksSure you wrote the code faster…Trick them into running faster…
  40. 40. “You are trading „development speed‟ for „processing speed‟ … all you do is waste all day reading docs to produce a half-assed project”– Bob Majdak Jr“ORMs are great for automating the sort of query that an idiot could write, and useless for anything non-trivial.”– Greg Chiassonhttp://highscalability.com/blog/2011/5/2/stack- overflow-makes-slow-pages-100x-faster-by- simple-sql-tu.htmlORMS
  41. 41.  Remove require/include calls and use autoloader or include_all files Avoid lots of configuration in parseable files (yaml, ini, etc) Turn off Debug features Use any sort of database caching available Watch your “dispatch loops” – write something custom if you need to Watch Plugin loading of all typesFrameworks
  42. 42.  Switch to another system (lots of importers) Use a cache plugin – or two, or three Keep wordpress up to date Don‟t use a lot of plugins Don‟t use the wp-db-abstraction plugin or any other plugin doing crazy things to the core It‟s just PHP, do all the PHP things too!Wordpress
  43. 43.  Install a Cache  Install a Cache plugin plugin Limit other Plugins  Limit other Plugins in use in use Disable anonymous  Make sure sessions debugging is off in Use a simple theme production All the other PHP  Turn off statistics stuff  All the other PHP stuffDrupal, Joomla
  44. 44.  http://groups.drupal.org/high-performance http://codex.wordpress.org/WordPress_Opti mization http://www.joomlaperformance.com/ http://www.brandonsavage.net/micro- optimizations-that-dont-matter/ http://weierophinney.net/matthew/archives/ 245-Autoloading-Benchmarks.html http://framework.zend.com/manual/en/perfo rmance.html http://www.endyourif.com/optimizing- cakephp-websites/ http://trac.symfony- project.org/wiki/OptimizationResources
  45. 45. Write it in CWhen PHP actually IS the problem
  46. 46.  http://emsmith.net http://joind.in/3422 auroraeosrose@gmail.com IRC – freenode – auroraeosrose #php-gtk #coappQuestions?

×