5. Exceptional Performance
started in 2004
quantify and improve the performance of
all Yahoo! products worldwide
center of expertise
build tools, analyze data
gather, research, and evangelize best
practices
6. Scope
performance breaks into two categories
– response time
– efficiency
current focus is response time
of web products
8. percentage of time spent on the front-end
Back-end vs. 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. The Performance Golden Rule
80-90% of the end-user response time is
spent on the front-end. Start there.
• Greater potential for improvement
• Simpler
• Proven to work
12. 80/20 Performance Rule
Vilfredo Pareto:
80% of consequences come from 20% of causes
Focus on the 20% that affects 80% of the
end-user response time.
Start at the front-end.
14. 1
user requests
www.yahoo.com
2
user requests
other web pages
3
user re-requests
www.yahoo.com
Empty vs. Full Cache
0 0.5 1 1.5 2 2.5 3
image
stylesheet
script
script
dns lookup
image
image
image
image
image
dns lookup
script
image
image
image
image
image
image
image
image
script
image
image
image
image
image
image
image
image
script
dns lookup
image
image
html
dns lookup
with an empty cache
16. Empty vs. Full Cache
0 0.5 1 1.5 2 2.5 3
image
image
html
Expires header
1
user requests
www.yahoo.com
2
user requests
other web pages
3
user re-requests
www.yahoo.com
with a full cache
17. Empty vs. Full Cache
empty cache
2.4 seconds
full cache
0.9 seconds
83% fewer bytes
90% fewer HTTP requests
18. How much does this benefit our users?
It depends on how many users have
components in cache.
• What percentage of users view a page with
an empty cache*?
* “Empty cache” means the browser has to request the components
instead of pulling them from the browser disk cache.
• What percentage of page views are done
with an empty cache*?
20. Add a new image to your page
<img src="image/blank.gif" height="1" width="1"/>
with the following response headers:
Expires: Thu, 15 Apr 2004 20:00:00 GMT
Last-Modified: Wed, 28 Sep 2006 23:49:57 GMT
}1 px
Browser Cache Experiment
21. Browser Cache Experiment
Requests from the browser will have
one of these response status codes:
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.
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. 0.0%
10.0%
20.0%
30.0%
40.0%
50.0%
60.0%
70.0%
80.0%
90.0%
100.0%
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
day of experiment
percentage
unique users with empty cache
page views with empty cache
Surprising Results
page views with
empty cache
40-60% ~20%
users with
empty cache
24. Experiment Takeaways
Keep in mind the empty cache user
experience. It might be more prevalent
than you think!
Use different techniques to optimize full
versus empty cache experience.
26. 1
user requests
www.yahoo.com
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
27. 1
user requests
www.yahoo.com
HTTP Quick Review
2
user requests
finance.yahoo.com
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;
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
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. Experiment Takeaways
eliminate unnecessary cookies
keep cookie sizes low
set cookies at appropriate domain level,
or hosting assets on a new domain
set Expires date appropriately
– earlier date or none removes cookie sooner
40. Maximizing Parallel Downloads
response time
(seconds)
aliases
0.00
0.20
0.40
0.60
0.80
1.00
1.20
1.40
1 2 4 5 10
average 36 x 36 px (0.9 Kb) 116 x 61 px (3.4 Kb)
41. Maximizing Parallel Downloads
response time
(seconds)
rule of thumb: use at least two but no more than four aliases
0.00
0.20
0.40
0.60
0.80
1.00
1.20
1.40
1 2 4 5 10
average 36 x 36 px (0.9 Kb) 116 x 61 px (3.4 Kb)
42. Experiment Takeaways
consider the effects of CPU thrashing
DNS lookup times vary across ISPs and
geographic locations
domain names may not be cached
44. How to optimize?
• basic optimization rules
• optimizing assets (images, scripts, and
styles)
• optimizations specific to scripts
• optimizations specific to styles
46. 14 things to check
1. Make fewer HTTP requests
2. Use a CDN
3. Add an Expires header
4. Gzip components
5. Put CSS at the top
6. Move JS to the bottom
7. Avoid CSS expressions
8. Make JS and CSS external
9. Reduce DNS lookups
10. Minify JS
11. Avoid redirects
12. Remove duplicate scripts
13. Turn off ETags
14. Make AJAX cacheable and small
51. Combined Scripts,
Combined Stylesheets
combining six scripts into one eliminates
five HTTP requests
challenges:
– develop as separate modules
– number of possible combinations vs. loading
more than needed
– maximize browser cache
one solution:
– dynamically combine and cache
52. Rule 2: Use a CDN
distribute your static content before
distributing your dynamic content
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. Rule 3: Add an Expires header
not just for images
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. Setting Expires header in Apache
ExpiresActive On
ExpiresByType application/x-javascript
"modification plus 2 years"
ExpiresByType text/css "modification plus
5 years"
55. When modifing an asset?
• Modify the asset name (automatically)!
– Append an epoch timestamp to the file
name, e.g. img_1185403733.png.
– Use the version number from your source
control system (cvs or svn for example),
e.g. img_1.1.png.
– 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).
56. Rule 4: Gzip components
you can affect users' download times
90%+ of browsers support compression
57. Gzip: not just for HTML
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
gzip scripts, stylesheets, XML, JSON (not
images, PDF)
59. Rule 5: Put CSS at the top
stylesheets block rendering in IE
http://stevesouders.com/examples/css-bottom.php
solution: put stylesheets in HEAD (per spec)
avoids Flash of Unstyled Content
use LINK (not @import)
61. Rule 6: Move scripts to the bottom
scripts block parallel downloads across all
hostnames
scripts block rendering of everything below
them in the page
IE and FF
http://stevesouders.com/examples/js-middle.php
62. Rule 6: Move scripts to the bottom
script defer attribute is not a solution
– blocks rendering and downloads in FF
– slight blocking in IE
solution: move them as low in the page as
possible
63. Rule 7: Avoid CSS expressions
used to set CSS properties dynamically in IE
width: expression(
document.body.clientWidth < 600 ?
“600px” : “auto” );
problem: expressions execute many times
– mouse move, key press, resize, scroll, etc.
http://stevesouders.com/examples/expression-counter.php
64. Rule 8: Make JS and CSS external
inline: HTML document is bigger
external: more HTTP requests, but cached
variables
– page views per user (per session)
– empty vs. full cache stats
– component re-use
external is typically better
– home pages may be an exception
65. Post-Onload Download
inline in front page
download external files after onload
window.onload = downloadComponents;
function downloadComponents() {
var elem = document.createElement("script");
elem.src = "http://.../file1.js";
document.body.appendChild(elem);
...
}
speeds up secondary pages
66. Example : Google
• 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
67. Example : Yahoo
• 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.
68. Dynamic Inlining
start with post-onload download
set cookie after components downloaded
server-side:
– if cookie, use external
– else, do inline with post-onload download
cookie expiration date is key
speeds up all pages
69. Rule 9: Reduce DNS lookups
typically 20-120 ms
block parallel downloads
OS and browser both have DNS caches
70. TTL (Time To Live)
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
TTL – how long record can be cached
browser settings override TTL
73. Rule 10: Minify JavaScript
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
minify inline scripts, too
75. Rule 11: Avoid redirects
3xx status codes – mostly 301 and 302
HTTP/1.1 301 Moved Permanently
Location: http://stevesouders.com/newuri
add Expires headers to cache redirects
worst form of blocking
http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html
80. Rule 13: Turn off ETags
unique identifier returned in response
ETag: "c8897e-aee-4165acf0"
Last-Modified: Thu, 07 Oct 2004 20:54:08 GMT
used in conditional GET requests
If-None-Match: "c8897e-aee-4165acf0"
If-Modified-Since: Thu, 07 Oct 2004 20:54:08 GMT
if ETag doesn't match, can't send 304
81. The Problem with ETags
ETag for a single entity is always different
across servers
ETag format
– Apache: inode-size-timestamp
– IIS: Filetimestamp:ChangeNumber
Sites with >1 server return too few 304s
– (n-1)/n
Remove them
– Apache: FileETag none
– IIS: http://support.microsoft.com/kb/922703/
82. Rule 14: Make AJAX cacheable
and small
XHR, JSON, iframe, dynamic scripts can
still be cached, minified, and gzipped
a personalized response should still be
cacheable by that person
83. AJAX Example: Yahoo! Mail Beta
address book XML request
→ GET /yab/[...]&r=0.5289571053069156 HTTP/1.1
Host: us.xxx.mail.yahoo.com
← HTTP/1.1 200 OK
Date: Thu, 12 Apr 2007 19:39:09 GMT
Cache-Control: private,max-age=0
Last-Modified: Sat, 31 Mar 2007 01:17:17 GMT
Content-Type: text/xml; charset=utf-8
Content-Encoding: gzip
address book changes infrequently
– cache it; add last-modified-time in URL
85. IBM Page Detailer
packet sniffer
Windows only
IE, FF, any .exe
c:windowswd_WS2s.ini
Executable=(NETSCAPE.EXE),(NETSCP6.EXE),(firefox.exe)
free trial, $300 license
http://alphaworks.ibm.com/tech/pagedetailer
89. web development evolved
inspect and edit HTML
tweak and visualize CSS
debug and profile JavaScript
monitor network activity (caveat)
Firefox extension
free
http://getfirebug.com/
在 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 合理设置
那么到底是不是将组件分配到越多 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.