Web Performance, Scalability, and Testing Techniques<br />Boston PHP Meetup Group – April 2011<br />Jonathan Klein<br />jk...
What We’ll Cover<br />Why Listen to Me?<br />Why Performance Matters<br />Measuring Server Side Performance<br />Speeding ...
What We’ll Cover<br />Why Listen to Me?<br />Why Performance Matters<br />Measuring Server Side Performance<br />Speeding ...
Introduction<br />Senior Software Engineer/Performance Guy at CSN Stores<br />Organizer of the Boston Web Performance Meet...
Currently converting all store code to PHP<br />Windows Server  FreeBSD<br />IIS  Lighttpd<br />~100,000 lines of ASP Cl...
Why Do We Care about Performance?<br />A Faster Website Will Make You More Money<br />
Firefox<br />Firefox reduced the load time of their download page by 2.2 seconds<br />Downloads went up 15.4%<br />This co...
Google<br />Injected a 400ms delay into search<br />0.44% fewer searches/user<br />0.76% after 6 weeks<br />After delay wa...
Yahoo!<br />400ms delay<br />5-9% drop in full-page traffic<br />
Direct Relationship Between Speed and Dollars<br />http://www.phpied.com/the-performance-business-pitch/<br />
Server Side Monitoring<br />Lots of Options:<br />Paid:<br />Coradiant<br />dynaTrace<br />Correlsense<br />http://www.rea...
Server Side Monitoring<br /><?php<br />$start = microtime(true);<br />…script content…<br />$end  = microtime(true);<br />...
WTH is do_stuff()?<br />do_stuff() can do one of:<br />Log to a database (not ideal)<br />Write to a text file (eww)<br />...
StatsD/Graphite<br />
Be Careful What You Watch<br />Averages can be misleading<br />Better to look at percentiles<br />
What Does a Scalability Problem Look Like?<br />
What Does a Scalability Problem Look Like?<br />
What Does a Scalability Problem Look Like?<br />
Ahh, much better!<br />
What can you do?<br />Start with the Database<br />Run a database trace <br />Filter: Queries > 50ms<br />Filter: Reads > ...
Okay, I’ll “Optimize”<br />Look at execution plan<br />Remove calls to remote servers<br />
Database Optimizations<br />Reduce Joins<br />Select * from  Select Foo, Bar, Baz from…<br />Minimize/consolidate subquer...
Example<br />SELECT id, name, salary FROM employee WHERE salary < 25000; <br />NOT<br />SELECT id, name, salary FROM emplo...
Example<br />SELECT id, name, salary FROM employee WHERE salary < 25000; <br />NOT<br />SELECT id, name, salary FROM emplo...
Caching<br />The Fastest DB Query is the One That’s Never Made<br />
Memcached<br />Caching layer between database and webserver<br />Can hold PHP objects and arrays<br />
Memcached<br />$m = new Memcached();<br />$m->pconnect(‘1.2.3.4', 11211);<br />$m->set(‘foo’, $bar, 600);<br />$baz = $m->...
PHP Optimizations<br />
Install APC<br />APC – Alternative PHP Cache<br />Opcode Cache<br />User Cache<br />Awesome!<br />
Opcode cache<br />
User Cache<br /><?php$bar = 'BAR';apc_store('foo', $bar);var_dump(apc_fetch('foo'));?> <br />Outputs…<br />string(3) "BAR"...
PHP Code Optimizations<br />Set the max loop value before the loop:<br />$max = count($rows);<br />for ($i = 0; $i < $max;...
PHP Code Optimizations<br />Could go on and on…<br />http://www.wmtips.com/php/tips-optimizing-php-code.htm<br />Minimal r...
Example…<br />Homepage takes 5 seconds to load<br />Optimize PHP…<br /> Reduce PHP execution time by 50%!<br />But wait!  ...
HipHop for PHP<br />Built by Facebook and Open Sourced<br />Compiles PHP into C++<br />Currently supports PHP 5.2<br />htt...
Webserver Considerations<br />
Webserver Optimizations<br />Pick the right one<br />Lighttpd/Nginx instead of Apache<br />Designed to solve the C10K prob...
Lighttpd Benefits<br />Event driven model:<br />“Unlike traditional servers, Nginx doesn't rely on threads to handle reque...
If you are stuck on Apache…<br />mod_deflate<br />Gzips content for faster transfer times<br />mod_pagespeed<br />Automati...
Load Testing<br />
JMeter<br />
JMeter<br />
JMeter<br />
Frontend Optimization<br />
Client side Optimization is Critical<br />80-90% of load time takes place on the client<br />For mobile  <br />97%<br />
Best Practices<br />Reduce HTTP Requests<br />Combine CSS, JS<br />Use image sprites<br />.classname{<br />     backgroun...
Best Practices<br />Minify CSS/JS<br />Strip comments and whitespace<br />Automate this – YUI Compressor<br />http://devel...
Image Optimization<br />For graphics use PNG8 (256 color limitation)<br />No more .gif (unless animated)<br />JPEGs can be...
Smush Your Images!  - smushit.com<br />
JPEG Quality<br />100%<br />80%<br />182 KB<br />48 KB<br />
Measuring Frontend Performance<br />
How Do You Measure Load Time?<br />Google Webmaster Tools<br />WebPagetest (www.webpagetest.org)<br />Yottaa.com<br />Fire...
CDN – Content Delivery Network<br />
Lots of Options<br />Amazon CloudFront<br />MaxCDN<br />Limelight<br />Level3<br />Akamai<br />Cotendo<br />
Expires Headers<br />Set a far future date on static resources<br />CSS/JS/Images<br />Release new version by changing the...
Google Page Speed<br />
Firebug Net Panel<br />
Webmaster tools<br />
Webmaster tools<br />
Resources<br />http://www.webperformancecentral.com/wiki/WebPagetest/Optimization_Help<br />http://developer.yahoo.com/per...
Conclusion<br />“Speed is the most important feature. If your application is slow, people won’t use it. I see this more wi...
Conclusion<br />“Speed is the most important feature. If your application is slow, people won’t use it. I see this more wi...
?><br />We’re Hiring!<br />www.csnstores.com/careers<br />Get In Touch:<br />www.meetup.com/Web-Performance-Boston/<br />j...
Upcoming SlideShare
Loading in...5
×

Web Performance, Scalability, and Testing Techniques - Boston PHP Meetup

3,426

Published on

I gave this talk on 4/27/11 at the Boston PHP Meetup Group. It covers both server side and client side optimizations, as well as monitoring tools and techniques.

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

No Downloads
Views
Total Views
3,426
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
57
Comments
0
Likes
3
Embeds 0
No embeds

No notes for slide

Web Performance, Scalability, and Testing Techniques - Boston PHP Meetup

  1. 1. Web Performance, Scalability, and Testing Techniques<br />Boston PHP Meetup Group – April 2011<br />Jonathan Klein<br />jklein@csnstores.com <br />@jonathanklein<br />
  2. 2. What We’ll Cover<br />Why Listen to Me?<br />Why Performance Matters<br />Measuring Server Side Performance<br />Speeding up the Server<br />Frontend Optimization<br />Measuring Full Page Load Time<br />Homework<br />
  3. 3. What We’ll Cover<br />Why Listen to Me?<br />Why Performance Matters<br />Measuring Server Side Performance<br />Speeding up the Server<br />Frontend Optimization<br />Measuring Full Page Load Time<br />Homework  Fun Performance Adventure!<br />
  4. 4. Introduction<br />Senior Software Engineer/Performance Guy at CSN Stores<br />Organizer of the Boston Web Performance Meetup Group<br />CSN Stores Stats:<br />~1400 requests/sec for static content<br />~400 requests/sec for dynamic content<br />~10 million unique visitors per month<br />On a typical Monday we serve 75,000,000 static files<br />
  5. 5. Currently converting all store code to PHP<br />Windows Server  FreeBSD<br />IIS  Lighttpd<br />~100,000 lines of ASP Classic  ~50,000 lines of PHP code<br />~5 weeks away from launch<br />
  6. 6. Why Do We Care about Performance?<br />A Faster Website Will Make You More Money<br />
  7. 7.
  8. 8. Firefox<br />Firefox reduced the load time of their download page by 2.2 seconds<br />Downloads went up 15.4%<br />This could drive 60 MILLION yearly downloads<br />
  9. 9. Google<br />Injected a 400ms delay into search<br />0.44% fewer searches/user<br />0.76% after 6 weeks<br />After delay was removed, 0.21% fewer searches<br />
  10. 10. Yahoo!<br />400ms delay<br />5-9% drop in full-page traffic<br />
  11. 11. Direct Relationship Between Speed and Dollars<br />http://www.phpied.com/the-performance-business-pitch/<br />
  12. 12.
  13. 13. Server Side Monitoring<br />Lots of Options:<br />Paid:<br />Coradiant<br />dynaTrace<br />Correlsense<br />http://www.real-user-monitoring.com/ - Free Version<br />Free:<br />Access Logs<br />Nagios<br />Ganglia<br />Hosted WebPagetest<br />Selenium/dynaTrace Ajax Edition<br />
  14. 14. Server Side Monitoring<br /><?php<br />$start = microtime(true);<br />…script content…<br />$end = microtime(true);<br />do_stuff(‘Description’, $end - $start);<br />?><br />
  15. 15. WTH is do_stuff()?<br />do_stuff() can do one of:<br />Log to a database (not ideal)<br />Write to a text file (eww)<br />Make a StatsD call over UDP (good) -http://codeascraft.etsy.com/2011/02/15/measure-anything-measure-everything/<br />
  16. 16. StatsD/Graphite<br />
  17. 17. Be Careful What You Watch<br />Averages can be misleading<br />Better to look at percentiles<br />
  18. 18.
  19. 19.
  20. 20. What Does a Scalability Problem Look Like?<br />
  21. 21. What Does a Scalability Problem Look Like?<br />
  22. 22. What Does a Scalability Problem Look Like?<br />
  23. 23. Ahh, much better!<br />
  24. 24. What can you do?<br />Start with the Database<br />Run a database trace <br />Filter: Queries > 50ms<br />Filter: Reads > 1000<br />Start with the worst ones and optimize<br />
  25. 25. Okay, I’ll “Optimize”<br />Look at execution plan<br />Remove calls to remote servers<br />
  26. 26. Database Optimizations<br />Reduce Joins<br />Select * from  Select Foo, Bar, Baz from…<br />Minimize/consolidate subqueries<br />Add indexes where needed<br />We added one index: Procedure dropped from 3.5 sec & 4500 reads to .06 sec and 130 reads!<br />Be careful with where clauses (don’t calculate stuff in them)<br />
  27. 27. Example<br />SELECT id, name, salary FROM employee WHERE salary < 25000; <br />NOT<br />SELECT id, name, salary FROM employee WHERE salary + 10000 < 35000; <br />
  28. 28. Example<br />SELECT id, name, salary FROM employee WHERE salary < 25000; <br />NOT<br />SELECT id, name, salary FROM employee WHERE salary + 10000 < 35000; <br />
  29. 29. Caching<br />The Fastest DB Query is the One That’s Never Made<br />
  30. 30. Memcached<br />Caching layer between database and webserver<br />Can hold PHP objects and arrays<br />
  31. 31. Memcached<br />$m = new Memcached();<br />$m->pconnect(‘1.2.3.4', 11211);<br />$m->set(‘foo’, $bar, 600);<br />$baz = $m->get(‘foo’);<br />
  32. 32. PHP Optimizations<br />
  33. 33. Install APC<br />APC – Alternative PHP Cache<br />Opcode Cache<br />User Cache<br />Awesome!<br />
  34. 34. Opcode cache<br />
  35. 35. User Cache<br /><?php$bar = 'BAR';apc_store('foo', $bar);var_dump(apc_fetch('foo'));?> <br />Outputs…<br />string(3) "BAR"<br />
  36. 36. PHP Code Optimizations<br />Set the max loop value before the loop:<br />$max = count($rows);<br />for ($i = 0; $i < $max; $i++) { <br /> echo $i;<br />}<br />require_once() is slow<br />Minimize use of define()<br />Yes, single quotes are slightly faster than double quotes<br />
  37. 37. PHP Code Optimizations<br />Could go on and on…<br />http://www.wmtips.com/php/tips-optimizing-php-code.htm<br />Minimal returns<br />Find the hotspots in your application and fix them<br />
  38. 38. Example…<br />Homepage takes 5 seconds to load<br />Optimize PHP…<br /> Reduce PHP execution time by 50%!<br />But wait! PHP execution was only taking 100ms<br />Saves you 50ms in load time<br />1% of total page load<br />
  39. 39. HipHop for PHP<br />Built by Facebook and Open Sourced<br />Compiles PHP into C++<br />Currently supports PHP 5.2<br />http://developers.facebook.com/blog/post/358/<br />https://github.com/facebook/hiphop-php<br />
  40. 40. Webserver Considerations<br />
  41. 41. Webserver Optimizations<br />Pick the right one<br />Lighttpd/Nginx instead of Apache<br />Designed to solve the C10K problem<br />Lighttpd Used By:<br />Youtube<br />Wikipedia<br />Meebo<br />
  42. 42. Lighttpd Benefits<br />Event driven model:<br />“Unlike traditional servers, Nginx doesn't rely on threads to handle requests. Instead it uses a much more scalable event-driven (asynchronous) architecture. This architecture uses small, but more importantly, predictable amounts of memory under load.”<br /><ul><li>http://wiki.nginx.org/</li></ul>FastCGI+ spawn-fcgi<br />PHP Process Management<br />Many child processes – scale out application tier. <br />
  43. 43. If you are stuck on Apache…<br />mod_deflate<br />Gzips content for faster transfer times<br />mod_pagespeed<br />Automatic performance improvements<br />KeepAlives on<br />Server won’t create a new connection for every resource<br />
  44. 44. Load Testing<br />
  45. 45. JMeter<br />
  46. 46. JMeter<br />
  47. 47. JMeter<br />
  48. 48.
  49. 49. Frontend Optimization<br />
  50. 50. Client side Optimization is Critical<br />80-90% of load time takes place on the client<br />For mobile  <br />97%<br />
  51. 51. Best Practices<br />Reduce HTTP Requests<br />Combine CSS, JS<br />Use image sprites<br />.classname{<br /> background: url(sprite.png) no-repeat 0 -432px;<br />}<br />
  52. 52. Best Practices<br />Minify CSS/JS<br />Strip comments and whitespace<br />Automate this – YUI Compressor<br />http://developer.yahoo.com/yui/compressor/<br />Gzip all text<br />HTML<br />CSS<br />JS<br />Optimize Images…<br />
  53. 53. Image Optimization<br />For graphics use PNG8 (256 color limitation)<br />No more .gif (unless animated)<br />JPEGs can be saved at lower quality (75%-80%)<br />Smush all images<br />
  54. 54. Smush Your Images! - smushit.com<br />
  55. 55. JPEG Quality<br />100%<br />80%<br />182 KB<br />48 KB<br />
  56. 56. Measuring Frontend Performance<br />
  57. 57. How Do You Measure Load Time?<br />Google Webmaster Tools<br />WebPagetest (www.webpagetest.org)<br />Yottaa.com<br />Firebug<br />YSlow<br />PageSpeed<br />Dynatrace Ajax Edition<br />
  58. 58.
  59. 59.
  60. 60.
  61. 61. CDN – Content Delivery Network<br />
  62. 62. Lots of Options<br />Amazon CloudFront<br />MaxCDN<br />Limelight<br />Level3<br />Akamai<br />Cotendo<br />
  63. 63.
  64. 64. Expires Headers<br />Set a far future date on static resources<br />CSS/JS/Images<br />Release new version by changing the filename<br />Benefits repeat visitors and repeat page views<br />
  65. 65. Google Page Speed<br />
  66. 66.
  67. 67. Firebug Net Panel<br />
  68. 68.
  69. 69.
  70. 70.
  71. 71.
  72. 72. Webmaster tools<br />
  73. 73. Webmaster tools<br />
  74. 74.
  75. 75.
  76. 76.
  77. 77. Resources<br />http://www.webperformancecentral.com/wiki/WebPagetest/Optimization_Help<br />http://developer.yahoo.com/performance/<br />http://code.google.com/speed/<br />High Performance Websites (Book)<br />Even Faster Websites (Book)<br />
  78. 78.
  79. 79. Conclusion<br />“Speed is the most important feature. If your application is slow, people won’t use it. I see this more with mainstream users than I do with power users...If something is slow, they’re just gone.”<br /> - Fred Wilson (10 Golden Principles of Web Apps)<br />
  80. 80. Conclusion<br />“Speed is the most important feature. If your application is slow, people won’t use it. I see this more with mainstream users than I do with power users...If something is slow, they’re just gone.”<br /> - Fred Wilson (10 Golden Principles of Web Apps)<br />
  81. 81. ?><br />We’re Hiring!<br />www.csnstores.com/careers<br />Get In Touch:<br />www.meetup.com/Web-Performance-Boston/<br />jklein@csnstores.com<br />@jonathanklein<br />
  1. A particular slide catching your eye?

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

×