August 11th, 2012High Performance PHP               Northeast PHP 2012                      Jonathan Klein        jonathan...
Agenda  Who am I?  Why Performance Matters  Profiling PHP Applications  Code Level Optimizations  Big Wins  Load Testing  ...
Introduction• Web Performance Platform Lead at Wayfair• Boston Web Performance Meetup Group Organizer• Led a team that con...
The Value of Performance
Real World Examples• Firefox: -2.2 seconds = 15.4% more downloads• Shopzilla: -5 seconds = 7-12% increase in revenue• Goog...
80/20 Rule  80% of page load time takes place on the                  client                                             7
We’re going to talk about the other 20%
A fast page load is 2 secondsThis means you have 400ms to get that HTML off                 your server
But network time could be ~100msThis means you have 400ms 300ms to build the                   page
The average length of an eye blink     is 100-400 milliseconds         Full Page Load – 2 Seconds      Base HTML – 400ms  ...
Profiling PHP Applications        Monitoring and Tracing
Monitoring/Tracing Tools• Paid:   1. Tracelytics (bought by AppNeta)   2. AppDynamics (building a PHP solution)   3. dynaT...
Monitoring/Tracing Tools• Paid:   1. Tracelytics (bought by AppNeta)   2. AppDynamics (building a PHP solution)   3. dynaT...
StatsDUDP Packets – extremely low overhead<?php$start = microtime(true);…script content…$end      = microtime(true);MyStat...
Graphite• Written by Orbitz• Real-time graphing engine for StatsD data (among other things)• http://graphite.wikidot.com/•...
Xdebug• PHP extension (need to install) - http://xdebug.org/• Code level tracing   1. Exposes hotspots• Significant overhe...
Lesson: Profile Your Code
Code Level Optimizations
Writing Efficient PHP• Set max value before loop:  $max = count($rows);  for ($i = 0; $i < $max; $i++) {     echo $i;  }• ...
Almost Every Micro-Optimization is           Worthless
http://phpbench.com/
http://phpbench.com/
http://phpbench.com/
Writing Efficient PHP• Set the max loop value before the loop:  $max = count($rows);  for ($i = 0; $i < $max; $i++) {     ...
So Why Even MentionMicro-Optimizations?
Lesson: Focus on the Big Wins“Premature optimization is the root of all evil”              - Donald Knuth
Big Wins
Upgrade PHP       5.3 is ~20% faster than 5.2          http://news.php.net/php.internals/36484      5.4 is ~20-40% faster ...
PHP 5.4 is 5 Times Faster than PHP4      http://static.zend.com/topics/White-paper-PHP4-PHP5.pdf
Use APC (Correctly)           APC – Alternative PHP Cache                                         40
Opcode Cache Performance• Vanilla settings  30-40% improvement• Turn off APC Stat  additional ~2x  improvement!      -- ...
APC User Cache<?php$bar = Hello World!;apc_store(foo, $bar);?><?phpvar_dump(apc_fetch(foo));?>-------- Output --------stri...
APC User Cache Optimizations• Avoid fragmentation - keep utilization under 10%   1. Assign 1GB, only fill 100MB• Compress ...
Caching• Avoid hitting the database• Cache in APC/Memcached• 1:00 tomorrow: “Caching With Memcached”                      ...
Fix All Errors• PHP 5.3: E_ALL | E_STRICT• PHP 5.4: E_ALL• Can also do error_reporting(-1);                               ...
Child Processes• 4-6 processes per CPU core.• Beyond that just add servers.                 (Test your app)               ...
HipHop for PHP• Developed/Open Sourced by Facebook• Transforms PHP source code into highly optimized C++• Reduces CPU for ...
Load Testing
JMeter• Open Source• Generate load via a GUI or command line• Can watch req/s peak out• Easy to use (just make sure you se...
Be Careful• JMeter looks a lot like a DOS attack• Make sure you know what is failing• Look at monitoring while test is run...
Takeaways
“How Fast Is Your Site?”
This is a terrible question
Why is it terrible?• Lack of context• Are we talking about average or a percentile?• Server side time or client?• Who is m...
We still have to answer it
Pick Tight SLAs“The homepage of our site will load in<300ms at the 80th percentile, measuredby sampling 10% of our real us...
Pick Tight SLAs“The homepage of our site will load in<300ms at the 80th percentile, measuredby sampling 10% of our real us...
Things to Remember1. Measure and monitor your application2. Focus on big wins3. Run the latest (stable) version of PHP4. M...
There is a lot more
Get In Touchhttp://www.meetup.com/Web-Performance-Boston/               wayfair.com/careers            jonathan.n.klein@gm...
Northeast PHP - High Performance PHP
Northeast PHP - High Performance PHP
Northeast PHP - High Performance PHP
Northeast PHP - High Performance PHP
Northeast PHP - High Performance PHP
Northeast PHP - High Performance PHP
Northeast PHP - High Performance PHP
Northeast PHP - High Performance PHP
Northeast PHP - High Performance PHP
Northeast PHP - High Performance PHP
Northeast PHP - High Performance PHP
Northeast PHP - High Performance PHP
Northeast PHP - High Performance PHP
Northeast PHP - High Performance PHP
Northeast PHP - High Performance PHP
Upcoming SlideShare
Loading in...5
×

Northeast PHP - High Performance PHP

9,655

Published on

This is a talk that I gave at the Northeast PHP conference in Boston, MA on 8/11/12.

Published in: Technology
0 Comments
22 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
9,655
On Slideshare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
137
Comments
0
Likes
22
Embeds 0
No embeds

No notes for slide
  • StatsD Aggregates things by 5 second intervals by default
  • Northeast PHP - High Performance PHP

    1. 1. August 11th, 2012High 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% revenuehttp://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 secondsThis means you have 400ms to get that HTML off your server
    9. 9. But network time could be ~100msThis 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. StatsDUDP 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 MentionMicro-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 herehttp://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);?><?phpvar_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, measuredby sampling 10% of our real users over a24 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, measuredby sampling 10% of our real users over a24 hour period every day at 8AM.” 62
    48. 48. Things to Remember1. Measure and monitor your application2. Focus on big wins3. Run the latest (stable) version of PHP4. Make sure you are using APC correctly5. It’s always the database: go to “The Proper Care and Feeding of a MySQL Database” talk6. Caching is your friend: go to the “Caching With Memcached” talk7. Know what system resources you depend on 63
    49. 49. There is a lot more
    50. 50. Get In Touchhttp://www.meetup.com/Web-Performance-Boston/ wayfair.com/careers jonathan.n.klein@gmail.com @jonathanklein 65
    1. A particular slide catching your eye?

      Clipping is a handy way to collect important slides you want to go back to later.

    ×