Successfully reported this slideshow.

Northeast PHP - High Performance PHP

25

Share

Loading in …3
×
1 of 65
1 of 65

More Related Content

Related Books

Free with a 14 day trial from Scribd

See all

Related Audiobooks

Free with a 14 day trial from Scribd

See all

Northeast PHP - High Performance PHP

  1. 1. August 11th, 2012 High Performance PHP Northeast PHP 2012 Jonathan Klein jonathan.n.klein@gmail.com @jonathanklein
  2. 2. Agenda Who am I? Why Performance Matters Profiling PHP Applications Code Level Optimizations Big Wins Load Testing Takeaways
  3. 3. Introduction • Web Performance Platform Lead at Wayfair • Boston Web Performance Meetup Group Organizer • Led a team that converted most of Wayfair’s storefront codebase from Classic ASP to PHP. 1. Rewrote ~100,000 lines of code • Wayfair Stats: 1. 450 reqs/sec for dynamic content (PHP pages) 2. 1600 reqs/sec for static content (including CDN) 3. Codebase has > 400,000 lines of PHP (some is 3rd party) 3
  4. 4. The Value of Performance
  5. 5. Real World Examples • Firefox: -2.2 seconds = 15.4% more downloads • Shopzilla: -5 seconds = 7-12% increase in revenue • Google: +400ms = 0.76% fewer searches • Amazon: +100ms = -1% revenue http://www.phpied.com/the-performance-business-pitch/ 6
  6. 6. 80/20 Rule 80% of page load time takes place on the client 7
  7. 7. We’re going to talk about the other 20%
  8. 8. A fast page load is 2 seconds This means you have 400ms to get that HTML off your server
  9. 9. But network time could be ~100ms This means you have 400ms 300ms to build the page
  10. 10. The average length of an eye blink is 100-400 milliseconds Full Page Load – 2 Seconds Base HTML – 400ms Server Generation Time – 300ms
  11. 11. Profiling PHP Applications Monitoring and Tracing
  12. 12. Monitoring/Tracing Tools • Paid: 1. Tracelytics (bought by AppNeta) 2. AppDynamics (building a PHP solution) 3. dynaTrace (building a PHP solution 4. New Relic • Free: 1. StatsD/Graphite 2. Xdebug 3. Nagios 4. Ganglia 13
  13. 13. Monitoring/Tracing Tools • Paid: 1. Tracelytics (bought by AppNeta) 2. AppDynamics (building a PHP solution) 3. dynaTrace (building a PHP solution 4. New Relic • Free: 1. StatsD/Graphite 2. Xdebug 3. Nagios 4. Ganglia 14
  14. 14. StatsD UDP Packets – extremely low overhead <?php $start = microtime(true); …script content… $end = microtime(true); MyStats::timing('foo.bar', $end - $start); ?> http://codeascraft.etsy.com/2011/02/15/measure-anything-measure-everything/ 15
  15. 15. Graphite • Written by Orbitz • Real-time graphing engine for StatsD data (among other things) • http://graphite.wikidot.com/ • Architecture: http://www.aosabook.org/en/graphite.html 16
  16. 16. Xdebug • PHP extension (need to install) - http://xdebug.org/ • Code level tracing 1. Exposes hotspots • Significant overhead, use in DEV only! • Add ?XDEBUG_PROFILE=true to a URL • View results: 1. kcachegrind on linux 2. wincachegrind on Windows 20
  17. 17. Lesson: Profile Your Code
  18. 18. Code Level Optimizations
  19. 19. Writing Efficient PHP • Set max value before loop: $max = count($rows); for ($i = 0; $i < $max; $i++) { echo $i; } • require_once() is slow • Minimize use of define() • Yes, single quotes are slightly faster than double quotes 27
  20. 20. Almost Every Micro-Optimization is Worthless
  21. 21. http://phpbench.com/
  22. 22. http://phpbench.com/
  23. 23. http://phpbench.com/
  24. 24. Writing Efficient PHP • Set the max loop value before the loop: $max = count($rows); for ($i = 0; $i < $max; $i++) { echo $i; } Okay, this one is pretty good 32
  25. 25. So Why Even Mention Micro-Optimizations?
  26. 26. Lesson: Focus on the Big Wins “Premature optimization is the root of all evil” - Donald Knuth
  27. 27. Big Wins
  28. 28. Upgrade PHP 5.3 is ~20% faster than 5.2 http://news.php.net/php.internals/36484 5.4 is ~20-40% faster than 5.3 http://news.php.net/php.internals/57760 Upgrading 5.2  5.4 gives a 45-70% improvement! http://php.net/migration53 http://php.net/migration54 38
  29. 29. PHP 5.4 is 5 Times Faster than PHP4 http://static.zend.com/topics/White-paper-PHP4-PHP5.pdf
  30. 30. Use APC (Correctly) APC – Alternative PHP Cache 40
  31. 31. Opcode Cache Performance • Vanilla settings  30-40% improvement • Turn off APC Stat  additional ~2x improvement! -- Understand what is happening here http://www.slideshare.net/vortexau/improving-php-application-performance-with-apc- presentation 41
  32. 32. APC User Cache <?php $bar = 'Hello World!'; apc_store('foo', $bar); ?> <?php var_dump(apc_fetch('foo')); ?> -------- Output -------- string(12) "Hello World!" 42
  33. 33. APC User Cache Optimizations • Avoid fragmentation - keep utilization under 10% 1. Assign 1GB, only fill 100MB • Compress objects that are > 10KB before storing • Reduce garbage collection in the source of apc_store() • Consider CDB 1. http://engineering.wayfair.com/moving-constants-out-of-apc-and-into-cdb/ 43
  34. 34. Caching • Avoid hitting the database • Cache in APC/Memcached • 1:00 tomorrow: “Caching With Memcached” 44
  35. 35. Fix All Errors • PHP 5.3: E_ALL | E_STRICT • PHP 5.4: E_ALL • Can also do error_reporting(-1); 45
  36. 36. Child Processes • 4-6 processes per CPU core. • Beyond that just add servers. (Test your app) 46
  37. 37. HipHop for PHP • Developed/Open Sourced by Facebook • Transforms PHP source code into highly optimized C++ • Reduces CPU for Facebook’s servers by 50% http://developers.facebook.com/blog/post/2010/02/02/hiphop-for-php--move-fast/ https://github.com/facebook/hiphop-php 47
  38. 38. Load Testing
  39. 39. JMeter • Open Source • Generate load via a GUI or command line • Can watch req/s peak out • Easy to use (just make sure you set the correct path to Java on your computer). 49
  40. 40. Be Careful • JMeter looks a lot like a DOS attack • Make sure you know what is failing • Look at monitoring while test is running • Run in Production • Run a test, make a change, run it again 55
  41. 41. Takeaways
  42. 42. “How Fast Is Your Site?”
  43. 43. This is a terrible question
  44. 44. Why is it terrible? • Lack of context • Are we talking about average or a percentile? • Server side time or client? • Who is measuring it? • When is it being measured? • Real users or synthetic? 59
  45. 45. We still have to answer it
  46. 46. Pick Tight SLAs “The homepage of our site will load in <300ms at the 80th percentile, measured by sampling 10% of our real users over a 24 hour period every day at 8AM.” 61
  47. 47. Pick Tight SLAs “The homepage of our site will load in <300ms at the 80th percentile, measured by sampling 10% of our real users over a 24 hour period every day at 8AM.” 62
  48. 48. Things to Remember 1. Measure and monitor your application 2. Focus on big wins 3. Run the latest (stable) version of PHP 4. Make sure you are using APC correctly 5. It’s always the database: go to “The Proper Care and Feeding of a MySQL Database” talk 6. Caching is your friend: go to the “Caching With Memcached” talk 7. Know what system resources you depend on 63
  49. 49. There is a lot more
  50. 50. Get In Touch http://www.meetup.com/Web-Performance-Boston/ wayfair.com/careers jonathan.n.klein@gmail.com @jonathanklein 65

Editor's Notes

  • StatsD Aggregates things by 5 second intervals by default
  • ×