Upgrade to Pro — share decks privately, control downloads, hide ads and more …

High Performance PHP

High Performance PHP

A talk I gave at PHPDay 2013 in Verona, Italy.

Jonathan Klein

May 18, 2013
Tweet

More Decks by Jonathan Klein

Other Decks in Technology

Transcript

  1. Some Etsy Stats • 1.4 billion page views/month • Almost

    $1B in sales last year • Over 1M lines of PHP Saturday, May 18, 13
  2. Agenda • Why Performance Matters • Profiling PHP Applications •

    Code Level Optimizations • Big Wins • Load Testing • Takeaways Saturday, May 18, 13
  3. Real World Examples • Firefox: -2.2 seconds = 15.4% more

    downloads http://www.phpied.com/the-performance-business-pitch/ Saturday, May 18, 13
  4. Real World Examples • Firefox: -2.2 seconds = 15.4% more

    downloads • Shopzilla: -5 seconds = 7-12% increase in revenue http://www.phpied.com/the-performance-business-pitch/ Saturday, May 18, 13
  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 http://www.phpied.com/the-performance-business-pitch/ Saturday, May 18, 13
  6. 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/ Saturday, May 18, 13
  7. A fast page load is 2 seconds This means you

    have 400ms to get that HTML off your server Saturday, May 18, 13
  8. But network time could be ~100ms This means you have

    400ms 300ms to build the page Saturday, May 18, 13
  9. < 100ms feels instant < 1 sec feels like flow

    < 10 sec to keep user’s attention http://www.nngroup.com/articles/response-times-3-important-limits/ Saturday, May 18, 13
  10. < 100ms feels instant < 1 sec feels like flow

    < 10 sec to keep user’s attention Full Page Load – 2 Seconds http://www.nngroup.com/articles/response-times-3-important-limits/ Saturday, May 18, 13
  11. < 100ms feels instant < 1 sec feels like flow

    < 10 sec to keep user’s attention Full Page Load – 2 Seconds Base HTML – 400ms http://www.nngroup.com/articles/response-times-3-important-limits/ Saturday, May 18, 13
  12. < 100ms feels instant < 1 sec feels like flow

    < 10 sec to keep user’s attention Full Page Load – 2 Seconds Base HTML – 400ms Server Generation Time – 300ms http://www.nngroup.com/articles/response-times-3-important-limits/ Saturday, May 18, 13
  13. Monitoring/Tracing • Paid: • Tracelytics (bought by AppNeta) • AppDynamics

    (building a PHP solution) • dynaTrace (building a PHP solution) • New Relic (has a free option) • Free: • StatsD/Graphite • xhprof Saturday, May 18, 13
  14. Monitoring/Tracing • Paid: • Tracelytics (bought by AppNeta) • AppDynamics

    (building a PHP solution) • dynaTrace (building a PHP solution) • New Relic • Free: • StatsD/Graphite • xhprof Saturday, May 18, 13
  15. StatsD (UDP packets) $start = microtime(true); /* script content */

    $end = microtime(true); StatsD::timing('foo.bar', $end - $start); More Info: http://goo.gl/LbDPE Saturday, May 18, 13
  16. 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 Saturday, May 18, 13
  17. xhprof • PHP Extension (need to install) • http://pecl.php.net/package/xhprof •

    Code level tracing • Significant overhead, use in DEV only! • Add ?xhprof=1 to URL • Results in browser Saturday, May 18, 13
  18. 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, but... Saturday, May 18, 13
  19. Writing Efficient PHP Set max value before loop: $max =

    count($rows); for ($i = 0; $i < $max; $i++) { echo $i; } Okay, this one is pretty good Saturday, May 18, 13
  20. 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 Saturday, May 18, 13
  21. 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 Saturday, May 18, 13
  22. 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! Saturday, May 18, 13
  23. 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 Saturday, May 18, 13
  24. 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 Saturday, May 18, 13
  25. 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 Saturday, May 18, 13
  26. Opcode Cache 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 Saturday, May 18, 13
  27. APC User Cache <?php $foo = "Hello, World!"; apc_store('some_key', $foo);

    ?> <?php var_dump(apc_fetch('some_key')); ?> -------- Output -------- string(12) "Hello World!" Saturday, May 18, 13
  28. APC User Cache • Avoid fragmentation - keep utilization under

    10% • Assign 1GB, only fill 100MB Saturday, May 18, 13
  29. APC User Cache • Avoid fragmentation - keep utilization under

    10% • Assign 1GB, only fill 100MB • Compress objects that are > 10KB before storing Saturday, May 18, 13
  30. APC User Cache • Avoid fragmentation - keep utilization under

    10% • Assign 1GB, only fill 100MB • Compress objects that are > 10KB before storing • Reduce garbage collection in the source of apc_store() Saturday, May 18, 13
  31. APC User Cache • Avoid fragmentation - keep utilization under

    10% • Assign 1GB, only fill 100MB • Compress objects that are > 10KB before storing • Reduce garbage collection in the source of apc_store() • Consider CDB Saturday, May 18, 13
  32. APC User Cache • Avoid fragmentation - keep utilization under

    10% • Assign 1GB, only fill 100MB • Compress objects that are > 10KB before storing • Reduce garbage collection in the source of apc_store() • Consider CDB • http://engineering.wayfair.com/moving-constants-out-of-apc- and-into-cdb/ Saturday, May 18, 13
  33. Memcached • Usually a separate server • In-memory key-value store

    • Extremely simple and fast • http://memcached.org/ Saturday, May 18, 13
  34. APC vs. Memcached APC User Cache Memcached Local to the

    Server Shared Network Resource Good for small objects Large or small objects Good for mostly read workloads Can read and write quickly Only one instance Can be clustered Saturday, May 18, 13
  35. Fix All Errors • PHP 5.3: E_ALL | E_STRICT •

    PHP 5.4: E_ALL • Can also do error_reporting(-1); Saturday, May 18, 13
  36. Child Processes 4-6 processes per CPU core. Beyond that just

    add servers. (Test your app) Saturday, May 18, 13
  37. HipHop for PHP • Developed/Open Sourced by Facebook • Now

    a VM + JIT compilation • 5x improvement in throughput over PHP 5.2 • http://developers.facebook.com/blog/post/2010/02/02/ hiphop-for-php--move-fast/ • https://www.facebook.com/notes/facebook-engineering/ speeding-up-php-based-development-with-hiphop-vm/ 10151170460698920 • https://github.com/facebook/hiphop-php Saturday, May 18, 13
  38. JMeter • Open Source • Generate load via a GUI

    or command line • Can watch req/s degrade with more users • Easy to use Saturday, May 18, 13
  39. Be Careful • JMeter looks a lot like a DOS

    attack Saturday, May 18, 13
  40. Be Careful • JMeter looks a lot like a DOS

    attack • Make sure you know what is failing Saturday, May 18, 13
  41. Be Careful • JMeter looks a lot like a DOS

    attack • Make sure you know what is failing • Look at monitoring while test is running Saturday, May 18, 13
  42. 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 Saturday, May 18, 13
  43. 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 Saturday, May 18, 13
  44. Why is it terrible? • Lack of context • Are

    we talking about average or a percentile? Saturday, May 18, 13
  45. Why is it terrible? • Lack of context • Are

    we talking about average or a percentile? • Server side time or client? Saturday, May 18, 13
  46. Why is it terrible? • Lack of context • Are

    we talking about average or a percentile? • Server side time or client? • Who is measuring it? Saturday, May 18, 13
  47. 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? Saturday, May 18, 13
  48. 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? Saturday, May 18, 13
  49. 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.” Saturday, May 18, 13
  50. 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.” Saturday, May 18, 13
  51. Things to Remember • Measure and monitor your application •

    Focus on big wins • Run the latest (stable) version of PHP Saturday, May 18, 13
  52. Things to Remember • Measure and monitor your application •

    Focus on big wins • Run the latest (stable) version of PHP • Make sure you are using APC correctly Saturday, May 18, 13
  53. Things to Remember • Measure and monitor your application •

    Focus on big wins • Run the latest (stable) version of PHP • Make sure you are using APC correctly • It’s always the database (stay in this room) Saturday, May 18, 13
  54. Things to Remember • Measure and monitor your application •

    Focus on big wins • Run the latest (stable) version of PHP • Make sure you are using APC correctly • It’s always the database (stay in this room) • Caching is your friend Saturday, May 18, 13
  55. Things to Remember • Measure and monitor your application •

    Focus on big wins • Run the latest (stable) version of PHP • Make sure you are using APC correctly • It’s always the database (stay in this room) • Caching is your friend • Know what system resources you depend on Saturday, May 18, 13