腾讯大讲堂09 如何建设高性能网站


Published on

腾讯大讲堂09 如何建设高性能网站

Published in: Technology
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide
  • Mysql conf 2005
  • Perlbal + memcached, both developed and shared by LJ team
  • 用了 packet sniffer 以后,下图显示如果用户没有 cache ( empty cache )的情况下读取 www.yahoo.com 的情况,可以看到其实只有 5 % 的时间花在了读取最开始的 html 上( 0.24 ),其他的 90% 时间花在了读取其他组件,例如 image , css 和 scripts 。
  • 我们又对其他大型网站做了测试,发现情况也很类似,大部分的时间都是花在了读取非 html 的其他组件。
  • 目前很多常用的性能优化技巧都集中在 html 生成( generating html )的部分( apache , C++, database , etc )但是实际上用户反应时间( response time )中只有 20% 是和这一部分相关的,所以我们还是将时间花在另外那 80% 的问题吧。
  • 2/8 定律
  • 结论:要让用户感觉网页反应最快, full cache 的优势很大
  • 有多少比例的用户在看主页的时候是 empty cache 有多少比例的 PV 是在 empty cache 的情况下产生的
  • 在 yahoo 的首页增加一个透明的小图 (1x1) , header 设置成 ” Expires: Thu, 15 Apr 2004 20:00:00 GMT , Last-Modified: Wed, 28 Sep 2006 23:49:57 GMT“ 。每天跟踪有多少比例的用户请求的这个小图,以及多少比例的 page view 中请求了这个小图 When the browser saves a component in its cache, it also saves the Expires and Last Modified values. Specifying an Expires date in the past forces the browser to request the image every time the page is viewed (with a few exceptions, such as when users click the browser’s “back” button to return to a page).
  • 从浏览器返回的 response 应该是 200 ( The browser does not have the image in its cache )或者 304 ( The browser has the image in its cache, but needs to verify the last modified date )。
  • On the first day of the experiment, no one had these images cached so the empty cache percentage was 100%. As the days passed more users had the images cached, so the percentages dropped until at some point it reached a constant steady state.
  • However, we found in our study that regardless of usage patterns, the percentage of page views with an empty cache is always ~20%. empty cache 的用户很可能比你想像中要多得多
  • The browser saves the “C” cookie on the user’s computer and sends it back in future requests. The “domain=.yahoo.com” specifies that the browser should include the cookie in future requests within the .yahoo.com domain and all its sub-domains. For example, if the user then visits finance.yahoo.com, the browser includes the “C” cookie in the request. Since an Expires attribute is not included in this example, the cookie expires at the end of the session.
  • Cookies 的大小也会对用户的反应时间造成很大影响,
  • While the data shows that the majority of page views aren’t impacted by a significant delay, it also shows that about 2% of page views have over 1500 bytes of cookies set at the .yahoo.com domain. Although 2% sounds insignificant, at Yahoo! this translates to millions of page views per day,
  • The data in Table 2 reflects only cookies set at the top domain levels to eliminate any cookies that may have been set by ads
  • A cookie set at the .yahoo.com domain impacts the response time for every Yahoo! page in the .yahoo.com domain that a user visits. 尽量减少不必要的 cookies 将 cookie 的 size 缩小 将 cookie 放到 domain level 的时候要谨慎小心,尽量不要影响其他的 sub domain 将 expiration date 合理设置
  • 既然对最终用户反应时间的最大影响来自与有多少个组件要被请求,为什么不能一次同时将他们下载完呢?主要是因为 http/1.1 的规范,浏览器每次只能同时从单个 hostname 下载两个组件
  • 但是如果一个网站将自己的组件平均地分配到两个 hostname 上,就会像下图
  • 那么到底是不是将组件分配到越多 hostname 越好呢,我们也做了测试。 The experiment measured an empty HTML document with 20 images on the page. The images were fetched from the same servers as those used by real Yahoo! pages. We ran the experiment in a controlled environment using a test harness that fetches a set of URLs repeatedly while measuring how long it takes to load the page on DSL.
  • We fetch 20 smaller-sized images (36 x 36 px) and 20 medium-sized images (116 x 61 px). To our surprise, increasing the number of aliases for loading the medium-size images (116 x 61px) worsens the response times using four or more aliases. Increasing the number of aliases by more than two for smaller-sized images (36 x 36px) doesn’t make much of an impact on the overall response time. On average, using two aliases is best. One possible contributor for slower response times is the amount of CPU thrashing on the client caused by increasing the number of parallel downloads. Another issue to consider is that DNS lookup times vary significantly across ISPs and geographic locations.
  • check out this image, used on Yahoo!'s homepage , or this one from Google's .
  • serving compressed HTML, CSS, client-side scripts, and any other type of text content. If you make XMLHttpRequests to services that return XML (or JSON, or plain text), make sure your server gzips as well
  • There's one gotcha when it comes to serving gzipped content: you must make sure that proxies do not get in your way. If an ISP's proxy caches your gzipped content and serves it to all of its customers, chances are that someone with a browser that doesn't support compression will receive your compressed content. To avoid this you can use the Vary: Accept-Encoding response header to tell the proxy to cache this response only for clients that send the same Accept-Encoding request header.
  • due to the nature of the scripts (they could potentially change anything on a page), browsers block all downloads when they encounters a <script> tag. So until a script is downloaded and parsed, no other downloads will be initiated.
  • Look out for the Number One
  • 腾讯大讲堂09 如何建设高性能网站

    1. 1. High performance Web Sites richardchang 腾讯技术大讲堂
    2. 2. 不知大家有没有看过这个 ppt
    3. 3. 以及这个架构图
    4. 4. Yahoo’s Case Performance research and 14 rules
    5. 5. Exceptional Performance <ul><li>started in 2004 </li></ul><ul><li>quantify and improve the performance of all Yahoo! products worldwide </li></ul><ul><li>center of expertise </li></ul><ul><li>build tools, analyze data </li></ul><ul><li>gather, research, and evangelize best practices </li></ul>
    6. 6. Scope <ul><li>performance breaks into two categories </li></ul><ul><ul><li>response time </li></ul></ul><ul><ul><li>efficiency </li></ul></ul><ul><li>current focus is response time </li></ul><ul><li>of web products </li></ul>
    7. 7. The Importance of Front-End Performance
    8. 8. Back-end vs. Front-end percentage of time spent on the front-end Empty Cache Full Cache amazon.com 82% 86% aol.com 94% 86% cnn.com 81% 92% ebay.com 98% 92% google.com 86% 64% msn.com 97% 95% myspace.com 96% 86% wikipedia.org 80% 88% yahoo.com 95% 88% youtube.com 97% 95%
    9. 9. The Performance Golden Rule <ul><li>80-90% of the end-user response time is spent on the front-end. Start there. </li></ul><ul><li>Greater potential for improvement </li></ul><ul><li>Simpler </li></ul><ul><li>Proven to work </li></ul>
    10. 10. <ul><li>Performance Research </li></ul>
    11. 11. http://yuiblog.com/blog/2006/11/28/performance-research-part-1/
    12. 12. 80/20 Performance Rule <ul><li>Vilfredo Pareto: </li></ul><ul><li>80% of consequences come from 20% of causes </li></ul><ul><li>Focus on the 20% that affects 80% of the end-user response time. </li></ul><ul><li>Start at the front-end. </li></ul>
    13. 13. Empty vs. Full Cache 1 user requests www.yahoo.com 2 user requests other web pages 3 user re-requests www.yahoo.com
    14. 14. Empty vs. Full Cache with an empty cache 1 user requests www.yahoo.com 2 user requests other web pages 3 user re-requests www.yahoo.com
    15. 15. Empty vs. Full Cache 1 user requests www.yahoo.com 2 user requests other web pages 3 user re-requests www.yahoo.com
    16. 16. Empty vs. Full Cache Expires header 3 user re-requests www.yahoo.com with a full cache 1 user requests www.yahoo.com 2 user requests other web pages
    17. 17. Empty vs. Full Cache <ul><li> empty cache </li></ul><ul><li>2.4 seconds </li></ul><ul><li> full cache </li></ul><ul><li>0.9 seconds </li></ul><ul><li>83% fewer bytes </li></ul><ul><li>90% fewer HTTP requests </li></ul>
    18. 18. How much does this benefit our users? <ul><li>It depends on how many users have components in cache. </li></ul><ul><li>What percentage of users view a page with an empty cache * ? </li></ul><ul><li>* “Empty cache” means the browser has to request the components instead of pulling them from the browser disk cache. </li></ul><ul><li>What percentage of page views are done with an empty cache * ? </li></ul>
    19. 19. http://yuiblog.com/blog/2007/01/04/performance-research-part-2/
    20. 20. Browser Cache Experiment <ul><li>Add a new image to your page </li></ul><ul><li><img src= &quot; image/blank.gif &quot; height= &quot; 1 &quot; width= &quot; 1 &quot; /> </li></ul><ul><li>with the following response headers: </li></ul><ul><ul><li>Expires: Thu, 15 Apr 2004 20:00:00 GMT </li></ul></ul><ul><ul><li>Last-Modified: Wed, 28 Sep 2006 23:49:57 GMT </li></ul></ul>} 1 px
    21. 21. Browser Cache Experiment <ul><li>Requests from the browser will have one of these response status codes: </li></ul><ul><ul><li>200 – The browser does not have the image in its cache. </li></ul></ul><ul><ul><li>304 – The browser has the image in its cache, but needs to verify the last modified date. </li></ul></ul>
    22. 22. Browser Cache Experiment What percentage of users view with an empty cache? # unique users with at least one 200 response total # unique users What percentage of page views are done with an empty cache? total # of 200 responses # of 200 + # of 304 responses } 1 px
    23. 23. Surprising Results 40-60% ~20% page views with empty cache users with empty cache
    24. 24. Experiment Takeaways <ul><li>Keep in mind the empty cache user experience. It might be more prevalent than you think! </li></ul><ul><li>Use different techniques to optimize full versus empty cache experience. </li></ul>
    25. 25. http://yuiblog.com/blog/2007/03/01/performance-research-part-3
    26. 26. HTTP Quick Review HTTP response header sent by the web server: HTTP/1.1 200 OK Content-Type: text/html; charset=utf-8 Set-Cookie: C=abcdefghijklmnopqrstuvwxyz; domain=.yahoo.com 1 user requests www.yahoo.com
    27. 27. HTTP Quick Review HTTP request header sent by the browser: GET / HTTP/1.1 Host: finance.yahoo.com User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; … Cookie: C=abcdefghijklmnopqrstuvwxyz; 1 user requests www.yahoo.com 2 user requests finance.yahoo.com
    28. 28. HTTP Quick Review HTTP request header sent by the browser: GET / HTTP/1.1 Host: autos.yahoo.com User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; … Cookie: C=abcdefghijklmnopqrstuvwxyz ; 1 user requests www.yahoo.com 3 user requests autos.yahoo.com
    29. 29. HTTP Quick Review HTTP request header sent by the browser: GET / HTTP/1.1 Host: mail.yahoo.com User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; … Cookie: C=abcdefghijklmnopqrstuvwxyz; 1 user requests www.yahoo.com 4 user requests mail.yahoo.com
    30. 30. HTTP Quick Review HTTP request header sent by the browser: GET / HTTP/1.1 Host: tech.yahoo.com User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; … Cookie: C=abcdefghijklmnopqrstuvwxyz; 1 user requests www.yahoo.com 5 user requests tech.yahoo.com
    31. 31. Impact of Cookies on Response Time 80 ms delay dialup users Cookie Size Time Delta 0 bytes 78 ms 0 ms 500 bytes 79 ms +1 ms 1000 bytes 94 ms +16 ms 1500 bytes 109 ms +31 ms 2000 bytes 125 ms +47 ms 2500 bytes 141 ms +63 ms 3000 bytes 156 ms +78 ms keep sizes low
    32. 32. .yahoo.com cookie sizes
    33. 33. Analysis of Cookie Sizes across the Web Total Cookie Size Amazon 60 bytes Google 72 bytes Yahoo 122 bytes CNN 184 bytes YouTube 218 bytes MSN 268 bytes eBay 331 bytes MySpace 500 bytes
    34. 34. Experiment Takeaways <ul><li>eliminate unnecessary cookies </li></ul><ul><li>keep cookie sizes low </li></ul><ul><li>set cookies at appropriate domain level, or hosting assets on a new domain </li></ul><ul><li>set Expires date appropriately </li></ul><ul><ul><li>earlier date or none removes cookie sooner </li></ul></ul>
    35. 35. http://yuiblog.com/blog/2007/04/11/performance-research-part-4/
    36. 36. Parallel Downloads Two components in parallel per hostname HTTP/1.1 GIF GIF GIF GIF GIF GIF
    37. 37. Parallel Downloads Two in parallel Four in parallel Eight in parallel
    38. 38. Maximizing Parallel Downloads response time (seconds) aliases
    39. 39. Maximizing Parallel Downloads response time (seconds) aliases
    40. 40. Maximizing Parallel Downloads response time (seconds) aliases
    41. 41. Maximizing Parallel Downloads response time (seconds) rule of thumb: use at least two but no more than four aliases
    42. 42. Experiment Takeaways <ul><li>consider the effects of CPU thrashing </li></ul><ul><li>DNS lookup times vary across ISPs and geographic locations </li></ul><ul><li>domain names may not be cached </li></ul>
    43. 43. <ul><li>14 Rules </li></ul>
    44. 44. How to optimize? <ul><li>basic optimization rules </li></ul><ul><li>optimizing assets (images, scripts, and styles) </li></ul><ul><li>optimizations specific to scripts </li></ul><ul><li>optimizations specific to styles </li></ul>
    45. 45. Basic Rules <ul><li>Decrease Download Sizes </li></ul><ul><li>Make Fewer HTTP Requests </li></ul><ul><ul><li>Combine scripts and style sheets </li></ul></ul><ul><ul><li>Use image sprites </li></ul></ul><ul><ul><li>Avoid redirects </li></ul></ul><ul><ul><li>Avoid frames </li></ul></ul>
    46. 46. 14 things to check <ul><li>Make fewer HTTP requests </li></ul><ul><li>Use a CDN </li></ul><ul><li>Add an Expires header </li></ul><ul><li>Gzip components </li></ul><ul><li>Put CSS at the top </li></ul><ul><li>Move JS to the bottom </li></ul><ul><li>Avoid CSS expressions </li></ul><ul><li>Make JS and CSS external </li></ul><ul><li>Reduce DNS lookups </li></ul><ul><li>Minify JS </li></ul><ul><li>Avoid redirects </li></ul><ul><li>Remove duplicate scripts </li></ul><ul><li>Turn off ETags </li></ul><ul><li>Make AJAX cacheable and small </li></ul>
    47. 47. Rule 1: Make fewer HTTP requests <ul><li>image maps </li></ul><ul><li>CSS sprites </li></ul><ul><li>inline images </li></ul><ul><li>combined scripts, combined stylesheets </li></ul>
    48. 48. Image maps <ul><li>server-side </li></ul><ul><li><a href=&quot;navbar.cgi&quot;><img ismap src=&quot;imagemap.gif&quot;></a> </li></ul><ul><li>-> http://.../navbar.cgi?127,13 </li></ul><ul><li>client-side – preferred </li></ul><ul><li><img usemap=&quot;#map1&quot; border=0 src=&quot;/images/imagemap.gif&quot;> </li></ul><ul><li><map name=&quot;map1&quot;> </li></ul><ul><li><area shape=&quot;rect&quot; coords=&quot;0,0,31,31&quot; href=&quot;home.html&quot; title=&quot;Home&quot;> </li></ul><ul><li>… </li></ul><ul><li></map> </li></ul><ul><li>drawbacks: </li></ul><ul><ul><li>must be contiguous </li></ul></ul><ul><ul><li>defining area coordinates – tedious, errors </li></ul></ul>http://www.w3.org/TR/html401/struct/objects.html#h-13.6
    49. 49. CSS Sprites – Preferred <ul><li>size of combined image is less </li></ul>CSS Sprites Generator - http://www.csssprites.com/ http://alistapart.com/articles/sprites #rss { background-image: url(sprite.png); background-position: -8px -40px; width: 16px; height: 16px; }
    50. 50. Combined Scripts, Combined Stylesheets Scripts Stylesheets amazon.com 3 1 aol.com 18 1 cnn.com 11 2 ebay.com 7 2 froogle.google.com 1 1 msn.com 9 1 myspace.com 2 2 wikipedia.org 3 1 yahoo.com 4 1 youtube.com 7 3 Average 6.5 1.5
    51. 51. Combined Scripts, Combined Stylesheets <ul><li>combining six scripts into one eliminates five HTTP requests </li></ul><ul><li>challenges: </li></ul><ul><ul><li>develop as separate modules </li></ul></ul><ul><ul><li>number of possible combinations vs. loading more than needed </li></ul></ul><ul><ul><li>maximize browser cache </li></ul></ul><ul><li>one solution: </li></ul><ul><ul><li>dynamically combine and cache </li></ul></ul>
    52. 52. Rule 2: Use a CDN <ul><li>distribute your static content before distributing your dynamic content </li></ul>amazon.com Akamai aol.com Akamai cnn.com ebay.com Akamai, Mirror Image google.com msn.com SAVVIS myspace.com Akamai, Limelight wikipedia.org yahoo.com Akamai youtube.com
    53. 53. Rule 3: Add an Expires header <ul><li>not just for images </li></ul>Images Stylesheets Scripts % Median Age amazon.com 0/62 0/1 0/3 0% 114 days aol.com 23/43 1/1 6/18 48% 217 days cnn.com 0/138 0/2 2/11 1% 227 days ebay.com 16/20 0/2 0/7 55% 140 days froogle.google.com 1/23 0/1 0/1 4% 454 days msn.com 32/35 1/1 3/9 80% 34 days myspace.com 0/18 0/2 0/2 0% 1 day wikipedia.org 6/8 1/1 2/3 75% 1 day yahoo.com 23/23 1/1 4/4 100% n/a youtube.com 0/32 0/3 0/7 0% 26 days
    54. 54. Setting Expires header in Apache <ul><li>ExpiresActive On </li></ul><ul><li>ExpiresByType application/x-javascript &quot;modification plus 2 years&quot; </li></ul><ul><li>ExpiresByType text/css &quot;modification plus 5 years&quot; </li></ul>
    55. 55. When modifing an asset? <ul><li>Modify the asset name (automatically)! </li></ul><ul><ul><li>Append an epoch timestamp to the file name, e.g. img_1185403733.png. </li></ul></ul><ul><ul><li>Use the version number from your source control system (cvs or svn for example), e.g. img_1.1.png. </li></ul></ul><ul><ul><li>Manually increment a number in the file name (e.g. when you see a file named img1.png, simply save the modified image as img2.png). </li></ul></ul>
    56. 56. Rule 4: Gzip components <ul><li>you can affect users' download times </li></ul><ul><li>90%+ of browsers support compression </li></ul>
    57. 57. Gzip: not just for HTML <ul><li>gzip scripts, stylesheets, XML, JSON (not images, PDF) </li></ul>HTML Scripts Stylesheets amazon.com x aol.com x some some cnn.com ebay.com x froogle.google.com x x x msn.com x deflate deflate myspace.com x x x wikipedia.org x x x yahoo.com x x x youtube.com x some some
    58. 58. Gzip Configuration <ul><li>Apache 2.x: mod_deflate </li></ul><ul><ul><li>AddOutputFilterByType DEFLATE text/html text/css application/x-javascript </li></ul></ul><ul><li>HTTP request </li></ul><ul><ul><li>Accept-Encoding: gzip, deflate </li></ul></ul><ul><li>HTTP response </li></ul><ul><ul><li>Content-Encoding: gzip </li></ul></ul><ul><ul><li>Vary: Accept-Encoding </li></ul></ul>needed for proxies
    59. 59. Rule 5: Put CSS at the top <ul><li>stylesheets block rendering in IE </li></ul><ul><ul><li>http://stevesouders.com/examples/css-bottom.php </li></ul></ul><ul><li>solution: put stylesheets in HEAD (per spec) </li></ul><ul><li>avoids Flash of Unstyled Content </li></ul><ul><li>use LINK (not @import) </li></ul>
    60. 60. Slowest is Fastest
    61. 61. Rule 6: Move scripts to the bottom <ul><li>scripts block parallel downloads across all hostnames </li></ul><ul><li>scripts block rendering of everything below them in the page </li></ul><ul><li>IE and FF </li></ul><ul><ul><li>http://stevesouders.com/examples/js-middle.php </li></ul></ul>
    62. 62. Rule 6: Move scripts to the bottom <ul><li>script defer attribute is not a solution </li></ul><ul><ul><li>blocks rendering and downloads in FF </li></ul></ul><ul><ul><li>slight blocking in IE </li></ul></ul><ul><li>solution: move them as low in the page as possible </li></ul>
    63. 63. Rule 7: Avoid CSS expressions <ul><li>used to set CSS properties dynamically in IE </li></ul><ul><ul><li>width: expression( </li></ul></ul><ul><ul><li>document.body.clientWidth < 600 ? </li></ul></ul><ul><ul><li>“ 600px” : “auto” ); </li></ul></ul><ul><li>problem: expressions execute many times </li></ul><ul><ul><li>mouse move, key press, resize, scroll, etc. </li></ul></ul><ul><li>http://stevesouders.com/examples/expression-counter.php </li></ul>
    64. 64. Rule 8: Make JS and CSS external <ul><li>inline: HTML document is bigger </li></ul><ul><li>external: more HTTP requests, but cached </li></ul><ul><li>variables </li></ul><ul><ul><li>page views per user (per session) </li></ul></ul><ul><ul><li>empty vs. full cache stats </li></ul></ul><ul><ul><li>component re-use </li></ul></ul><ul><li>external is typically better </li></ul><ul><ul><li>home pages may be an exception </li></ul></ul>
    65. 65. Post-Onload Download <ul><li>inline in front page </li></ul><ul><li>download external files after onload </li></ul><ul><ul><li>window.onload = downloadComponents; </li></ul></ul><ul><ul><li>function downloadComponents() { </li></ul></ul><ul><ul><li>var elem = document.createElement(&quot;script&quot;); </li></ul></ul><ul><ul><li>elem.src = &quot;http://.../file1.js&quot;; </li></ul></ul><ul><ul><li>document.body.appendChild(elem); </li></ul></ul><ul><ul><li>... </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><li>speeds up secondary pages </li></ul>
    66. 66. Example : Google <ul><li>once the home page has finished loading, there is a request to get a sprite image, which is not actually needed until search results page loads </li></ul>
    67. 67. Example : Yahoo <ul><li>Conditional pre-loading -- waits for the user to start typing in the search box. Once you've begun typing, it's almost guaranteed that you'll submit a search query. </li></ul>
    68. 68. Dynamic Inlining <ul><li>start with post-onload download </li></ul><ul><li>set cookie after components downloaded </li></ul><ul><li>server-side: </li></ul><ul><ul><li>if cookie, use external </li></ul></ul><ul><ul><li>else, do inline with post-onload download </li></ul></ul><ul><li>cookie expiration date is key </li></ul><ul><li>speeds up all pages </li></ul>
    69. 69. Rule 9: Reduce DNS lookups <ul><li>typically 20-120 ms </li></ul><ul><li>block parallel downloads </li></ul><ul><li>OS and browser both have DNS caches </li></ul>
    70. 70. TTL (Time To Live) TTL – how long record can be cached browser settings override TTL www.amazon.com 1 minute www.aol.com 1 minute www.cnn.com 10 minutes www.ebay.com 1 hour www.google.com 5 minutes www.msn.com 5 minutes www.myspace.com 1 hour www.wikipedia.org 1 hour www.yahoo.com 1 minute www.youtube.com 5 minutes
    71. 71. Browser DNS Cache <ul><li>IE </li></ul><ul><ul><li>DnsCacheTimeout: 30 minutes </li></ul></ul><ul><ul><li>KeepAliveTimeout: 1 minute </li></ul></ul><ul><ul><li>ServerInfoTimeout: 2 minutes </li></ul></ul><ul><li>Firefox </li></ul><ul><ul><li>network.dnsCacheExpiration: 1 minute </li></ul></ul><ul><ul><li>network.dnsCacheEntries: 20 </li></ul></ul><ul><ul><li>network.http.keep-alive.timeout: 5 minutes </li></ul></ul><ul><ul><li>Fasterfox: 1 hour, 512 entries, 30 seconds </li></ul></ul>
    72. 72. Reducing DNS Lookups <ul><li>fewer hostnames – 2-4 (what about forums and blogs?) </li></ul><ul><li>keep-alive </li></ul>
    73. 73. Rule 10: Minify JavaScript minify inline scripts, too Minify External? Minify Inline? www.amazon.com no no www.aol.com no no www.cnn.com no no www.ebay.com yes no froogle.google.com yes yes www.msn.com yes yes www.myspace.com no no www.wikipedia.org no no www.yahoo.com yes yes www.youtube.com no no
    74. 74. Minify vs. Obfuscate minify – it's safer http://crockford.com/javascript/jsmin http://dojotoolkit.org/docs/shrinksafe Original JSMin Savings Dojo Savings www.amazon.com 204K 31K (15%) 48K (24%) www.aol.com 44K 4K (10%) 4K (10%) www.cnn.com 98K 19K (20%) 24K (25%) www.myspace.com 88K 23K (27%) 24K (28%) www.wikipedia.org 42K 14K (34%) 16K (38%) www.youtube.com 34K 8K (22%) 10K (29%) Average 85K 17K (21%) 21K (25%)
    75. 75. Rule 11: Avoid redirects <ul><li>3xx status codes – mostly 301 and 302 </li></ul><ul><ul><li>HTTP/1.1 301 Moved Permanently </li></ul></ul><ul><ul><li>Location: http://stevesouders.com/newuri </li></ul></ul><ul><li>add Expires headers to cache redirects </li></ul><ul><li>worst form of blocking </li></ul>http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html
    76. 76. Redirects Redirects www.amazon.com no www.aol.com yes – secondary page www.cnn.com yes – initial page www.ebay.com yes – secondary page froogle.google.com no www.msn.com yes – initial page www.myspace.com yes – secondary page www.wikipedia.org yes – secondary page www.yahoo.com yes – secondary page www.youtube.com no
    77. 77. Avoid Redirects <ul><li>missing trailing slash </li></ul><ul><ul><li>http://astrology.yahoo.com/astrology </li></ul></ul><ul><ul><li>use Alias or DirectorySlash </li></ul></ul><ul><li>mod_rewrite </li></ul><ul><li>CNAMEs </li></ul><ul><li>log referer – track internal links </li></ul><ul><li>outbound links – harder </li></ul><ul><ul><li>beacons – beware of race condition </li></ul></ul><ul><ul><li>XHR – bail at readyState 2 </li></ul></ul>
    78. 78. Rule 12: Remove duplicate scripts <ul><li>hurts performance </li></ul><ul><ul><li>extra HTTP requests (IE only) </li></ul></ul><ul><ul><li>extra executions </li></ul></ul><ul><li>atypical? </li></ul><ul><ul><li>2 of 10 top sites contain duplicate scripts </li></ul></ul><ul><li>team size, # of scripts </li></ul>
    79. 79. Script Insertion Functions <ul><li><?php </li></ul><ul><li>function insertScript($jsfile) { </li></ul><ul><li>if ( alreadyInserted($jsfile) ) { return; } </li></ul><ul><li>pushInserted($jsfile); </li></ul><ul><li>if ( hasDependencies($jsfile) ) { </li></ul><ul><li>$dependencies = getDependencies($jsfile); </li></ul><ul><li>for ( $i = 0; $i < count($dependencies); $i++ ) { </li></ul><ul><li>insertScript($dependencies[$i]); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li>echo '<script type=&quot;text/javascript&quot; src=&quot;' . </li></ul><ul><li>getVersion($jsfile) . '&quot;></script>&quot;; </li></ul><ul><li>} </li></ul><ul><li>?> </li></ul>
    80. 80. Rule 13: Turn off ETags <ul><li>unique identifier returned in response </li></ul><ul><ul><li>ETag: &quot;c8897e-aee-4165acf0&quot; </li></ul></ul><ul><ul><li>Last-Modified: Thu, 07 Oct 2004 20:54:08 GMT </li></ul></ul><ul><li>used in conditional GET requests </li></ul><ul><ul><li>If-None-Match: &quot;c8897e-aee-4165acf0&quot; </li></ul></ul><ul><ul><li>If-Modified-Since: Thu, 07 Oct 2004 20:54:08 GMT </li></ul></ul><ul><li>if ETag doesn't match, can't send 304 </li></ul>
    81. 81. The Problem with ETags <ul><li>ETag for a single entity is always different across servers </li></ul><ul><li>ETag format </li></ul><ul><ul><li>Apache: inode -size-timestamp </li></ul></ul><ul><ul><li>IIS: Filetimestamp: ChangeNumber </li></ul></ul><ul><li>Sites with >1 server return too few 304s </li></ul><ul><ul><li>(n-1)/n </li></ul></ul><ul><li>Remove them </li></ul><ul><ul><li>Apache: FileETag none </li></ul></ul><ul><ul><li>IIS: http://support.microsoft.com/kb/922703/ </li></ul></ul>
    82. 82. Rule 14: Make AJAX cacheable and small <ul><li>XHR, JSON, iframe, dynamic scripts can still be cached, minified, and gzipped </li></ul><ul><li>a personalized response should still be cacheable by that person </li></ul>
    83. 83. AJAX Example: Yahoo! Mail Beta <ul><li>address book XML request </li></ul><ul><ul><li>-> GET /yab/[...]&r=0.5289571053069156 HTTP/1.1 </li></ul></ul><ul><ul><li>Host: us.xxx.mail.yahoo.com </li></ul></ul><ul><ul><li>← HTTP/1.1 200 OK </li></ul></ul><ul><ul><li>Date: Thu, 12 Apr 2007 19:39:09 GMT </li></ul></ul><ul><ul><li>Cache-Control: private,max-age=0 </li></ul></ul><ul><ul><li>Last-Modified: Sat, 31 Mar 2007 01:17:17 GMT </li></ul></ul><ul><ul><li>Content-Type: text/xml; charset=utf-8 </li></ul></ul><ul><ul><li>Content-Encoding: gzip </li></ul></ul><ul><li>address book changes infrequently </li></ul><ul><ul><li>cache it; add last-modified-time in URL </li></ul></ul>
    84. 84. <ul><li>Live Analysis </li></ul>
    85. 85. IBM Page Detailer <ul><li>packet sniffer </li></ul><ul><li>Windows only </li></ul><ul><li>IE, FF, any .exe </li></ul><ul><ul><li>c:windowswd_WS2s.ini </li></ul></ul><ul><ul><li>Executable=(NETSCAPE.EXE),(NETSCP6.EXE),(firefox.exe) </li></ul></ul><ul><li>free trial, $300 license </li></ul>http://alphaworks.ibm.com/tech/pagedetailer
    86. 86. http://alphaworks.ibm.com/tech/pagedetailer
    87. 87. Fasterfox <ul><li>measures load time of pages </li></ul><ul><li>alters config settings for faster loading </li></ul><ul><li>Firefox extension </li></ul><ul><li>free </li></ul>http://fasterfox.mozdev.org/
    88. 88. LiveHTTPHeaders <ul><li>view HTTP headers </li></ul><ul><li>Firefox extension </li></ul><ul><li>free </li></ul>http://livehttpheaders.mozdev.org/
    89. 89. <ul><li>web development evolved </li></ul><ul><li>inspect and edit HTML </li></ul><ul><li>tweak and visualize CSS </li></ul><ul><li>debug and profile JavaScript </li></ul><ul><li>monitor network activity (caveat) </li></ul><ul><li>Firefox extension </li></ul><ul><li>free </li></ul>http://getfirebug.com/
    90. 90. http://getfirebug.com/
    91. 91. YSlow <ul><li>performance lint tool </li></ul><ul><li>grades web pages for each rule </li></ul><ul><li>Firefox extension </li></ul><ul><li>Yahoo! internal tool </li></ul><ul><li>http://developer.yahoo.com/yslow/ </li></ul>
    92. 96. <ul><li>Conclusion </li></ul>
    93. 97. Takeaways <ul><li>focus on the front-end </li></ul><ul><li>harvest the low-hanging fruit </li></ul><ul><li>you do control user response times </li></ul><ul><li>LOFNO – be an advocate for your users </li></ul>