找回密码
 注册
X系列官方授权正版
搜索
查看: 4114|回复: 13

[网站技术] 13个加速网站速度的法则 (e文)

[复制链接]
发表于 2007-8-21 18:07:34 | 显示全部楼层 |阅读模式
Exceptional Performance : Thirteen Simple Rules for Speeding Up Your Web Site



1: Minimize HTTP Requests

80% of the end-user response time is spent on the front-end. Most of this time is tied up in downloading all the components in the page: images, stylesheets, scripts, Flash, etc. Reducing the number of components in turn reduces the number of HTTP requests required to render the page. This is the key to faster pages.
One way to reduce the number of components in the page is to simplify the page's design. But is there a way to build pages with richer content while also achieving fast response times? Here are some techniques for reducing the number of HTTP requests, while still supporting rich page designs.

Image maps combine multiple images into a single image. The overall size is about the same, but reducing the number of HTTP requests speeds up the page. Image maps only work if the images are contiguous in the page, such as a navigation bar. Defining the coordinates of image maps can be tedious and error prone.

CSS Sprites are the preferred method for reducing the number of image requests. Combine all the images in your page into a single image and use the CSS background-image and background-position properties to display the desired image segment.
Inline images use the data: URL scheme to embed the image data in the actual page. This can increase the size of your HTML document. Combining inline images into your (cached) stylesheets is a way to reduce HTTP requests and avoid increasing the size of your pages.
Combined files are a way to reduce the number of HTTP requests by combining all scripts into a single script, and similarly combining all stylesheets into a single stylesheet. It's a simple idea that hasn't seen wide adoption. The ten top U.S. web sites average 7 scripts and 2 stylesheets per page. Combining files is more challenging when the scripts and stylesheets vary from page to page, but making this part of your release process improves response times.

Reducing the number of HTTP requests in your page is the place to start. This is the most important guideline for improving performance for first time visitors. As described in Tenni Theurer's blog Browser Cache Usage - Exposed!, 40-60% of daily visitors to your site come in with an empty cache. Making your page fast for these first time visitors is key to a better user experience.
Discuss Rule 1



2: Use a Content Delivery Network

The user's proximity to your web server has an impact on response times. Deploying your content across multiple, geographically dispersed servers will make your pages load faster from the user's perspective. But where should you start?
As a first step to implementing geographically dispersed content, don't attempt to redesign your web application to work in a distributed architecture. Depending on the application, changing the architecture could include daunting tasks such as synchronizing session state and replicating database transactions across server locations. Attempts to reduce the distance between users and your content could be delayed by, or never pass, this application architecture step.

Remember that 80-90% of the end-user response time is spent downloading all the components in the page: images, stylesheets, scripts, Flash, etc. This is the Performance Golden Rule, as explained in The Importance of Front-End Performance. Rather than starting with the difficult task of redesigning your application architecture, it's better to first disperse your static content. This not only achieves a bigger reduction in response times, but it's easier thanks to content delivery networks.

A content delivery network (CDN) is a collection of web servers distributed across multiple locations to deliver content more efficiently to users. The server selected for delivering content to a specific user is typically based on a measure of network proximity. For example, the server with the fewest network hops or the server with the quickest response time is chosen.

Some large Internet companies own their own CDN, but it's cost-effective to use a CDN service provider, such as Akamai Technologies, Mirror Image Internet, or Limelight Networks. For start-up companies and private web sites, the cost of a CDN service can be prohibitive, but as your target audience grows larger and becomes more global, a CDN is necessary to achieve fast response times. At Yahoo!, properties that moved static content off their application web servers to a CDN improved end-user response times by 20% or more. Switching to a CDN is a relatively easy code change that will dramatically improve the speed of your web site.
Discuss Rule 2



3: Add an Expires Header

Web page designs are getting richer and richer, which means more scripts, stylesheets, images, and Flash in the page. A first-time visitor to your page may have to make several HTTP requests, but by using the Expires header you make those components cacheable. This avoids unnecessary HTTP requests on subsequent page views. Expires headers are most often used with images, but they should be used on all components including scripts, stylesheets, and Flash components.

Browsers (and proxies) use a cache to reduce the number and size of HTTP requests, making web pages load faster. A web server uses the Expires header in the HTTP response to tell the client how long a component can be cached. This is a far future Expires header, telling the browser that this response won't be stale until April 15, 2010.

      Expires: Thu, 15 Apr 2010 20:00:00 GMT

If your server is Apache, use the ExiresDefault directive to set an expiration date relative to the current date. This example of the ExpiresDefault directive sets the Expires date 10 years out from the time of the request.

      ExpiresDefault "access plus 10 years"

Keep in mind, if you use a far future Expires header you have to change the component's filename whenever the component changes. At Yahoo! we often make this step part of the build process: a version number is embedded in the component's filename, for example, yahoo_2.0.6.js.

Using a far future Expires header affects page views only after a user has already visited your site. It has no effect on the number of HTTP requests when a user visits your site for the first time and the browser's cache is empty. The impact of this performance improvement depends, therefore, on how often users hit your pages with a primed cache. (A "primed cache" already contains all of the components in the page.) We measured this at Yahoo! and found the number of page views with a primed cache is 75-85%. By using a far future Expires header, you increase the number of components that are cached by the browser and re-used on subsequent page views without sending a single byte over the user's Internet connection.
Discuss Rule 3



4: Gzip Components

The time it takes to transfer an HTTP request and response across the network can be significantly reduced by decisions made by front-end engineers. It's true that the end-user's bandwidth speed, Internet service provider, proximity to peering exchange points, etc. are beyond the control of the development team. But there are other variables that affect response times. Compression reduces response times by reducing the size of the HTTP response.

Starting with HTTP/1.1, web clients indicate support for compression with the Accept-Encoding header in the HTTP request.

      Accept-Encoding: gzip, deflate

If the web server sees this header in the request, it may compress the response using one of the methods listed by the client. The web server notifies the web client of this via the Content-Encoding header in the response.

      Content-Encoding: gzip

Gzip is the most popular and effective compression method at this time. It was developed by the GNU project and standardized by RFC 1952. The only other compression format you're likely to see is deflate, but it's less effective and less popular.

Gzipping generally reduces the response size by about 70%. Approximately 90% of today's Internet traffic travels through browsers that claim to support gzip. If you use Apache, the module configuring gzip depends on your version: Apache 1.3 uses mod_gzip while Apache 2.x uses mod_deflate.

There are known issues with browsers and proxies that may cause a mismatch in what the browser expects and what it receives with regard to compressed content. Fortunately, these edge cases are dwindling as the use of older browsers drops off. The Apache modules help out by adding appropriate Vary response headers automatically.

Servers choose what to gzip based on file type, but are typically too limited in what they decide to compress. Most web sites gzip their HTML documents. It's also worthwhile to gzip your scripts and stylesheets, but many web sites miss this opportunity. In fact, it's worthwhile to compress any text response including XML and JSON. Image and PDF files should not be gzipped because they are already compressed. Trying to gzip them not only wastes CPU but can potentially increase file sizes.

Gzipping as many file types as possible is an easy way to reduce page weight and accelerate the user experience.
Discuss Rule 4



5: Put CSS at the Top

While researching performance at Yahoo!, we discovered that moving stylesheets to the document HEAD makes pages load faster. This is because putting stylesheets in the HEAD allows the page to render progressively.

Front-end engineers that care about performance want a page to load progressively; that is, we want the browser to display whatever content it has as soon as possible. This is especially important for pages with a lot of content and for users on slower Internet connections. The importance of giving users visual feedback, such as progress indicators, has been well researched and documented. In our case the HTML page is the progress indicator! When the browser loads the page progressively the header, the navigation bar, the logo at the top, etc. all serve as visual feedback for the user who is waiting for the page. This improves the overall user experience.

The problem with putting stylesheets near the bottom of the document is that it prohibits progressive rendering in many browsers, including Internet Explorer. Browsers block rendering to avoid having to redraw elements of the page if their styles change. The user is stuck viewing a blank white page. Firefox doesn't block rendering, which means when the stylesheet is done loading it's possible elements in the page will have to be redrawn, resulting in the [url=http://developer.yahoo.com/performance/%20http://weblogs.mozillazine.org/hyatt/archives/2004_05.html#005496]flash of unstyled content[/url] problem.

The HTML specification clearly states that stylesheets are to be included in the HEAD of the page: "Unlike A, [LINK] may only appear in the HEAD section of a document, although it may appear any number of times." Neither of the alternatives, the blank white screen or flash of unstyled content, are worth the risk. The optimal solution is to follow the HTML specification and load your stylesheets in the document HEAD.
Discuss Rule 5



6: Move Scripts to the Bottom

Rule 5 described how stylesheets near the bottom of the page prohibit progressive rendering, and how moving them to the document HEAD eliminates the problem. Scripts (external JavaScript files) pose a similar problem, but the solution is just the opposite: it's better to move scripts from the top to as low in the page as possible. One reason is to enable progressive rendering, but another is to achieve greater download parallelization.

With stylesheets, progressive rendering is blocked until all stylesheets have been downloaded. That's why it's best to move stylesheets to the document HEAD, so they get downloaded first and rendering isn't blocked. With scripts, progressive rendering is blocked for all content below the script. Moving scripts as low in the page as possible means there's more content above the script that is rendered sooner.

The second problem caused by scripts is blocking parallel downloads. The HTTP/1.1 specification suggests that browsers download no more than two components in parallel per hostname. If you serve your images from multiple hostnames, you can get more than two downloads to occur in parallel. (I've gotten Internet Explorer to download over 100 images in parallel.) While a script is downloading, however, the browser won't start any other downloads, even on different hostnames.

In some situations it's not easy to move scripts to the bottom. If, for example, the script uses document.write to insert part of the page's content, it can't be moved lower in the page. There might also be scoping issues. In many cases, there are ways to workaround these situations.

An alternative suggestion that often comes up is to use deferred scripts. The DEFER attribute indicates that the script does not contain document.write, and is a clue to browsers that they can continue rendering. Unfortunately, Firefox doesn't support the DEFER attribute. In Internet Explorer, the script may be deferred, but not as much as desired. If a script can be deferred, it can also be moved to the bottom of the page. That will make your web pages load faster.
Discuss Rule 6



7: Avoid CSS Expressions

CSS expressions are a powerful (and dangerous) way to set CSS properties dynamically. They're supported in Internet Explorer, starting with version 5. As an example, the background color could be set to alternate every hour using CSS expressions.

      background-color: expression( (new Date()).getHours()%2 ? "#B8D4FF" : "#F08A00" );

As shown here, the expression method accepts a JavaScript expression. The CSS property is set to the result of evaluating the JavaScript expression. The expression method is ignored by other browsers, so it is useful for setting properties in Internet Explorer needed to create a consistent experience across browsers.

The problem with expressions is that they are evaluated more frequently than most people expect. Not only are they evaluated when the page is rendered and resized, but also when the page is scrolled and even when the user moves the mouse over the page. Adding a counter to the CSS expression allows us to keep track of when and how often a CSS expression is evaluated. Moving the mouse around the page can easily generate more than 10,000 evaluations.

One way to reduce the number of times your CSS expression is evaluated is to use one-time expressions, where the first time the expression is evaluated it sets the style property to an explicit value, which replaces the CSS expression. If the style property must be set dynamically throughout the life of the page, using event handlers instead of CSS expressions is an alternative approach. If you must use CSS expressions, remember that they may be evaluated thousands of times and could affect the performance of your page.
Discuss Rule 7



8: Make JavaScript and CSS External

Many of these performance rules deal with how external components are managed. However, before these considerations arise you should ask a more basic question: Should JavaScript and CSS be contained in external files, or inlined in the page itself?

Using external files in the real world generally produces faster pages because the JavaScript and CSS files are cached by the browser. JavaScript and CSS that are inlined in HTML documents get downloaded every time the HTML document is requested. This reduces the number of HTTP requests that are needed, but increases the size of the HTML document. On the other hand, if the JavaScript and CSS are in external files cached by the browser, the size of the HTML document is reduced without increasing the number of HTTP requests.

The key factor, then, is the frequency with which external JavaScript and CSS components are cached relative to the number of HTML documents requested. This factor, although difficult to quantify, can be gauged using various metrics. If users on your site have multiple page views per session and many of your pages re-use the same scripts and stylesheets, there is a greater potential benefit from cached external files.

Many web sites fall in the middle of these metrics. For these properties, the best solution generally is to deploy the JavaScript and CSS as external files. The only exception I've seen where inlining is preferable is with home pages, such as Yahoo!'s front page (http://www.yahoo.com) and My Yahoo! (http://my.yahoo.com). Home pages that have few (perhaps only one) page view per session may find that inlining JavaScript and CSS results in faster end-user response times.

For front pages that are typically the first of many page views, there are techniques that leverage the reduction of HTTP requests that inlining provides, as well as the caching benefits achieved through using external files. One such technique is to inline JavaScript and CSS in the front page, but dynamically download the external files after the page has finished loading. Subsequent pages would reference the external files that should already be in the browser's cache.
Discuss Rule 8



9: Reduce DNS Lookups

The Domain Name System (DNS) maps hostnames to IP addresses, just as phonebooks map people's names to their phone numbers. When you type www.yahoo.com into your browser, a DNS resolver contacted by the browser returns that server's IP address. DNS has a cost. It typically takes 20-120 milliseconds for DNS to lookup the IP address for a given hostname. The browser can't download anything from this hostname until the DNS lookup is completed.

DNS lookups are cached for better performance. This caching can occur on a special caching server, maintained by the user's ISP or local area network, but there is also caching that occurs on the individual user's computer. The DNS information remains in the operating system's DNS cache (the "DNS Client service" on Microsoft Windows). Most browsers have their own caches, separate from the operating system's cache. As long as the browser keeps a DNS record in its own cache, it doesn't bother the operating system with a request for the record.

Internet Explorer caches DNS lookups for 30 minutes by default, as specified by the DnsCacheTimeout registry setting. Firefox caches DNS lookups for 1 minute, controlled by the network.dnsCacheExpiration configuration setting. (Fasterfox changes this to 1 hour.)

When the client's DNS cache is empty (for both the browser and the operating system), the number of DNS lookups is equal to the number of unique hostnames in the web page. This includes the hostnames used in the page's URL, images, script files, stylesheets, Flash objects, etc. Reducing the number of unique hostnames reduces the number of DNS lookups.

Reducing the number of unique hostnames has the potential to reduce the amount of parallel downloading that takes place in the page. Avoiding DNS lookups cuts response times, but reducing parallel downloads may increase response times. My guideline is to split these components across at least two but no more than four hostnames. This results in a good compromise between reducing DNS lookups and allowing a high degree of parallel downloads.
Discuss Rule 9




10: Minify JavaScript

Minification is the practice of removing unnecessary characters from code to reduce its size thereby improving load times. When code is minified all comments are removed, as well as unneeded white space characters (space, newline, and tab). In the case of JavaScript, this improves response time performance because the size of the downloaded file is reduced. The most popular tool for minifying JavaScript code code is [url=]JSMin[/url], developed by Douglas Crockford, a fellow Yahoo!.

Obfuscation is an alternative optimization that can be applied to source code. Like minification, it removes comments and white space, but it also munges the code. As part of munging, function and variable names are converted into smaller strings making the code more compact as well as harder to read. This is typically done to make it more difficult to reverse engineer the code. But munging can help performance because it reduces the code size beyond what is achieved by minification. The tool-of-choice is less clear in the area of JavaScript obfuscation. Dojo Compressor (ShrinkSafe) is the one I've seen used the most.

Minification is a safe, fairly straightforward process. Obfuscation, on the other hand, is more complex and thus more likely to generate bugs as a result of the obfuscation step itself. Obfuscation also requires modifying your code to indicate API functions and other symbols that should not be munged. It also makes it harder to debug your code in production. Although I've never seen problems introduced from minification, I have seen bugs caused by obfuscation. In a survey of ten top U.S. web sites, minification achieved a 21% size reduction versus 25% for obfuscation. Although obfuscation has a higher size reduction, I recommend minifying JavaScript code because of the reduced risks and maintenance costs.

In addition to minifying external scripts, inlined script blocks can and should also be minified. Even if you gzip your scripts, as described in Rule 4, minifying them will still reduce the size by 5% or more. As the use and size of JavaScript increases, so will the savings gained by minifying your JavaScript code.
Discuss Rule 10




11: Avoid Redirects

Redirects are accomplished using the 301 and 302 status codes. Here's an example of the HTTP headers in a 301 response:

      HTTP/1.1 301 Moved Permanently
      Location: http://example.com/newuri
      Content-Type: text/html

The browser automatically takes the user to the URL specified in the Location field. All the information necessary for a redirect is in the headers. The body of the response is typically empty. Despite their names, neither a 301 nor a 302 response is cached in practice unless additional headers, such as Expires or Cache-Control, indicate it should be. The meta refresh tag and JavaScript are other ways to direct users to a different URL, but if you must do a redirect, the preferred technique is to use the standard 3xx HTTP status codes, primarily to ensure the back button works correctly.

The main thing to remember is that redirects slow down the user experience. Inserting a redirect between the user and the HTML document delays everything in the page since nothing in the page can be rendered and no components can start being downloaded until the HTML document has arrived.
One of the most wasteful redirects happens frequently and web developers are generally not aware of it. It occurs when a trailing slash (/) is missing from a URL that should otherwise have one. For example, going to http://astrology.yahoo.com/astrology results in a 301 response containing a redirect to http://astrology.yahoo.com/astrology/ (notice the added trailing slash). This is fixed in Apache by using Alias or mod_rewrite, or the DirectorySlash directive if you're using Apache handlers.

Connecting an old web site to a new one is another common use for redirects. Others include connecting different parts of a website and directing the user based on certain conditions (type of browser, type of user account, etc.). Using a redirect to connect two web sites is simple and requires little additional coding. Although using redirects in these situations reduces the complexity for developers, it degrades the user experience. Alternatives for this use of redirects include using Alias and mod_rewrite if the two code paths are hosted on the same server. If a domain name change is the cause of using redirects, an alternative is to create a CNAME (a DNS record that creates an alias pointing from one domain name to another) in combination with Alias or mod_rewrite.
Discuss Rule 11



12: Remove Duplicate Scripts

It hurts performance to include the same JavaScript file twice in one page. This isn't as unusual as you might think. A review of the ten top U.S. web sites shows that two of them contain a duplicated script. Two main factors increase the odds of a script being duplicated in a single web page: team size and number of scripts. When it does happen, duplicate scripts hurt performance by creating unnecessary HTTP requests and wasted JavaScript execution.
Unnecessary HTTP requests happen in Internet Explorer, but not in Firefox. In Internet Explorer, if an external script is included twice and is not cacheable, it generates two HTTP requests during page loading. Even if the script is cacheable, extra HTTP requests occur when the user reloads the page.
In addition to generating wasteful HTTP requests, time is wasted evaluating the script multiple times. This redundant JavaScript execution happens in both Firefox and Internet Explorer, regardless of whether the script is cacheable.

One way to avoid accidentally including the same script twice is to implement a script management module in your templating system. The typical way to include a script is to use the SCRIPT tag in your HTML page.

      script type="text/javascript" src="menu_1.0.17.js"></script>

An alternative in PHP would be to create a function called insertScript.

      <?php insertScript("menu.js") ?>

In addition to preventing the same script from being inserted multiple times, this function could handle other issues with scripts, such as dependency checking and adding version numbers to script filenames to support far future Expires headers.
Discuss Rule 12



13: Configure ETags


Entity tags (ETags) are a mechanism that web servers and browsers use to determine whether the component in the browser's cache matches the one on the origin server. (An "entity" is another word for what I've been calling a "component": images, scripts, stylesheets, etc.) ETags were added to provide a mechanism for validating entities that is more flexible than the last-modified date. An ETag is a string that uniquely identifies a specific version of a component. The only format constraints are that the string be quoted. The origin server specifies the component's ETag using the ETag response header.

      HTTP/1.1 200 OK
      Last-Modified: Tue, 12 Dec 2006 03:03:59 GMT
      ETag: "10c24bc-4ab-457e1c1f"
      Content-Length: 12195

Later, if the browser has to validate a component, it uses the If-None-Match header to pass the ETag back to the origin server. If the ETags match, a 304 status code is returned reducing the response by 12195 bytes for this example.

       GET /i/yahoo.gif HTTP/1.1
      Host: us.yimg.com
      If-Modified-Since: Tue, 12 Dec 2006 03:03:59 GMT
      If-None-Match: "10c24bc-4ab-457e1c1f"
      HTTP/1.1 304 Not Modified

The problem with ETags is that they typically are constructed using attributes that make them unique to a specific server hosting a site. ETags won't match when a browser gets the original component from one server and later tries to validate that component on a different server, a situation that is all too common on Web sites that use a cluster of servers to handle requests. By default, both Apache and IIS embed data in the ETag that dramatically reduces the odds of the validity test succeeding on web sites with multiple servers.

The ETag format for Apache 1.3 and 2.x is inode-size-timestamp. Although a given file may reside in the same directory across multiple servers, and have the same file size, permissions, timestamp, etc., its inode is different from one server to the next.

IIS 5.0 and 6.0 have a similar issue with ETags. The format for ETags on IIS is Filetimestamp:ChangeNumber. A ChangeNumber is a counter used to track configuration changes to IIS. It's unlikely that the ChangeNumber is the same across all IIS servers behind a web site.

The end result is ETags generated by Apache and IIS for the exact same component won't match from one server to another. If the ETags don't match, the user doesn't receive the small, fast 304 response that ETags were designed for; instead, they'll get a normal 200 response along with all the data for the component. If you host your web site on just one server, this isn't a problem. But if you have multiple servers hosting your web site, and you're using Apache or IIS with the default ETag configuration, your users are getting slower pages, your servers have a higher load, you're consuming greater bandwidth, and proxies aren't caching your content efficiently. Even if your components have a far future Expires header, a conditional GET request is still made whenever the user hits Reload or Refresh.

If you're not taking advantage of the flexible validation model that ETags provide, it's better to just remove the ETag altogether. The Last-Modified header validates based on the component's timestamp. And removing the ETag reduces the size of the HTTP headers in both the response and subsequent requests. This Microsoft Support article describes how to remove ETags. In Apache, this is done by simply adding the following line to your Apache configuration file:

       FileETag none

[ 本帖最后由 白衣 于 2007-8-21 18:26 编辑 ]

评分

参与人数 1UCC +54 收起 理由
mike521zx + 54 有价值信息

查看全部评分

发表于 2007-8-21 18:10:12 | 显示全部楼层
排版,排版是很重要的
那个中间的http request...太乱
回复

使用道具 举报

 楼主| 发表于 2007-8-21 18:12:50 | 显示全部楼层
處理中....

汗~~ 貼上來後全亂了套
回复

使用道具 举报

 楼主| 发表于 2007-8-21 18:26:57 | 显示全部楼层
嗯,搞定~~
回复

使用道具 举报

发表于 2007-8-21 18:41:50 | 显示全部楼层

13个加速网站速度的法则 (Google文)

此为Google翻译文,不懂请对照1楼原文

1 :减少http请求

80 %的终端用户响应时间,是用在前端.大部分时间,这是捆绑在下载的所有部件中页:图像,字体,脚本,闪光灯等,减少元件数量,从而降低了一些http请求须提供页面.这是关键,以更快的页面.
一个办法是减少元件数量,在首页,是为了简化网页的设计.但有一种方法建立网页内容更丰富,同时又实现快速响应时间?这里有一些小窍门减少数量的http请求,同时还支持丰富的页面设计.

影像地图结合,将多个图象整合成一个单一的形象.总体规模大约相同,但数量减少http请求加快页.影像地图只工作,如果图像是在毗连的网页,如导航栏.确定座标图像地图可以繁琐和容易出错.

css的小仙是首选方法,为减少一些形象的要求.结合所有的画面在你的页面成为一个单一的形象和使用css的背景图象和背景定位性能展示理想的图像部分.
直列使用图像数据: url方案嵌入到图像数据,在实际的页面.这可以扩大市场规模,你的html文件.结合图像内嵌到你(缓存)样式的是一个方法,以减少http请求,并避免增加大小你的页面.
档案相结合的一个途径,以减少一些http请求结合所有剧本成一个单一脚本,也同样结合所有空格成一个单一样式.它的一个简单的想法,还没有见过广泛采用.十大美国网站的平均7脚本和样式2每页.结合档案更具有挑战性时,脚本和样式不同,从一页页,但如何使这部分你释放的过程,缩短了反应时间.

数量减少http请求,在你的页面是地方做起.这是最重要的方针,为提高业绩首次参观.描述滕尼叛乱的博客浏览器的缓存使用-曝光! , 40-60 % ,每日访客到您的网站来,在同一个空的缓存.令你的网页速度,为这些第一次参观者主要是为了更好的用户体验.
讨论规则1



2 :使用内容配送网络

用户的靠近你的web server已经冲击了反应时间.部署你的内容在多个地理上分散的服务器将会使你的页面加载速度,从用户的角度.但应该要从哪里开始呢?
作为第一步实施地理上分散的内容,并不试图重新web应用工作,在一个分布式网络体系.取决于应用,改变结构可以包括艰巨的任务,如同步会话状态复制和交易数据库服务器的位置. Attempts to reduce the distance between users and your content could be delayed by, or never pass, this application architecture step.

记得80-90 %的终端用户响应时间都花在下载的所有部件中页:图像,字体,脚本,闪光灯等,这是表现金科玉律,如上重要性前端性能.而非起点与艰巨的任务,重新规划你的应用架构,它的,不如先驱散你的静态内容.这不仅实现了较大的下降响应时间,但它的容易,这应该归功于内容传送网络.

内容分发网络(币)是一个集网络服务器分布在多个地点,以提供内容更有效地给用户.服务器选定内容提供给特定的用户通常是基于衡量网络接近.举例来说,服务器最少啤酒花网络或服务器与最快的反应时间是雀屏中选.

一些大型互联网公司自己的亲人,但它的成本效益,用一个币服务提供商,如akamai技术,镜像互联网,或曝光网络.创业公司和私人网址,成本上cdn服务可以望而却步,但由于你的目标观众愈大,并成为更具有全球性,一个币,要做到快速反应时间.雅虎!物业感动静态内容过自己的网络应用服务器上cdn改善终端用户响应时间由20 %或以上.切换到欧元是一个比较容易的代码改变,将大大提高速度,你的网站.
讨论规则2



3 :加满头

网页设计越来越富裕,这意味着更多的脚本,样式,图片和flash在首页.第一次来到你的页面可能要作出一些http请求,但用头届满时,你使这些组件cacheable .这样做可以避免不必要的http请求,对以后的页面浏览量.截至头是最常用的图像,但他们应该用在所有组成部分,包括脚本,样式和闪存组件.

浏览器(和代理人)使用一个缓存,以减少数量和规模上的http请求,使网页的加载速度.网络服务器用满箱http响应告诉客户多久的一个组成部分,可缓存.这是一个遥远的未来只剩头,告诉浏览器这种反应将不会因循,直到2010年4月15日.

      截至:周四, 2010年4月15日格林尼20:00:00

如果你的服务器是apache ,用exiresdefault指令集到期相对当前日期.这个例子的expiresdefault指令规定日期届满10年,从时间的请求.

       expiresdefault通道"再加上10年"

记住,如果你使用一个遥远的未来只剩头,你要改变组件的文件时成分的变化.雅虎!我们常走这一步的一部分,构建过程:一个版本是嵌入在组件的文件,例如yahoo_2.0.6.js .

用一个遥远的未来只剩头影响页面浏览量只有当用户已经访问你的站点.它没有对一些http请求的,当用户访问你的网站,对于首次和浏览器的缓存是空的.这方面的影响,业绩改善依赖,因此,如何对用户往往打你的页面与催芽缓存. ( "引缓存"已经包含了所有的部件都在页) .我们衡量这个在雅虎!发现页数与催芽缓存75-85 % .用一个遥远的未来只剩头,你的数目增加组件缓存由浏览再用其后页面浏览量发送单字节以上的用户的互联网连接.
讨论第3



4 : gzip的部件

时间需转让一个http请求和响应整个网络可大大降低所作的决定前端工程师.它的真实,终端用户的带宽,速度,互联网服务提供商,靠近连接交换点等,都是无法控制的开发团队.但还有其他变数影响响应时间.压缩缩短反应时间,缩小http响应.

开始http/1.1标准,网上客户在表示支持压缩与接受编码头在http请求.

      接受编码: gzip ,可以嚣张

如果网络服务器没有看到这个头在请求中,它可以压缩脉冲响应方法之一,所列举的客户.网络服务器通知该网站的客户通过这一内容编码头中的反应.

      内容编码:一个gzip

gzip的,是最受欢迎和最有效的压缩方法,在这个时候.它是由gnu项目和规范化的rfc 1952 .只有其它的压缩格式,你可能看到的是嚣张,但它的效果较差,并不受欢迎.

gzipping普遍降低反应大小约70 % .大约90 % ,今天的互联网流量穿越浏览器声称支持gzip .如果你使用apache ,模块配置一个gzip取决于你的版本: 1.3阿帕奇利用mod_gzip ,而阿帕奇2.x mod_deflate都利用.

已知有问题,与浏览器及代理人可能造成错配的是什么浏览器的期望和要求是什么,它得到关于压缩内容.幸运的是,这些优势的情况下逐渐减少,由于使用旧的浏览器脱落. apache模块帮忙添加适当的反应不同,自动头.

服务器选择什么gzip的基于文件类型,但通常都过于有限,什么都得压缩.大多数网站gzip的,他们的html文件.它的,也值得gzip的脚本和样式,但在许多网站上放过这个机会.事实上,它的有价值压缩文本任何反应,包括xml和对应.形象和pdf文件不应该经过gzip ,因为他们已经被压缩.试图gzip的,他们不仅浪费cpu的,但可能会增加文件的大小.

gzipping很多类型的文件可能是一个简单的方法来减轻体重页并加速用户体验.
讨论第4



5 :把css的大楼顶部

而研究表现在雅虎!我们发现,字体移动到文件头,使页面加载更快.这是因为把空格中头部,让网页提供循序渐进的方式进行.

前端工程师关心的表现,希望有一页逐步负荷;也就是说,我们想要浏览器显示的内容,不管它尽快.这是特别重要的页面了很多内容和用户对慢上网.必须给用户提供视觉反馈的,如进度指标,已很好地研究和记载.我们遇到的html网页是时代进步的指标!当浏览器加载逐步页头,导航栏,标识上部,等均担任视觉反馈,为用户谁是等待页面.这样就提高了整体用户体验.

问题是把空格附近的底部文件是禁止进绘制许多浏览器,包括internet explorer .浏览座渲染,以避免过重的内容页,如果他们改变风格.用户是看贴一张白页. firefox并不块渲染,即当样式做装载它的内容可能在页面将要重写,以致中的[ url = http://developer.yahoo.com/performance/ % 20http : / / weblogs.mozillazine.org/hyatt/archives/2004_05.html # 005496 ]闪光unstyled内容[ / url ]今天的问题.

html源规范清楚说明,样式都将被列入主管页说: "不像, [链接]可能仅出现在头部的第一个文件,虽然它可能出现了多次"没有替代品,空白的白幕或闪光unstyled内容,是值得冒险.最佳解决方案是遵循规范的html和负载你的样式文件头.
讨论第5



6 :谨剧本底部

第5叙述样式附近页底部逐步禁止令,以及如何迁移到文件头,消除这个问题.剧本(外部javascript文件)的构成类似问题,但解决问题刚好相反:它的好剧本动议,从上层为低,在页越好.原因之一是使进绘制,但另一个原因是,以实现更大的并行下载.

与样式,逐步使阻塞,直到所有的样式都被下载.这就是为什么最好的动议样式到文件头,因此,他们首先下载和渲染是不能阻挡的.剧本的,渐进渲染是阻断所有内容如下脚本.移动剧本那样低下页可能意味着有更多的内容,上面的脚本变得越快.

第二个问题所造成的脚本块并行下载. http/1.1规范显示浏览下载,没有两个以上元件并联每个主机.如果您为您服务形象,从多个主机,你可以得到两个以上的下载发生在平行. (一凯伊internet explorer下载超过100图像平行) .而剧本下载的,但浏览器不会启动任何其它的下载,甚至在不同的主机.

在某些情况下,这并不容易动议脚本的底部.举例说,假如该脚本使用document.write插入部分的页的内容,可以不迁较低页.也可能存在问题的范围.在许多情况下,有办法要绕过这些情况.

另一种建议是,也常常是为了推迟使用脚本.推迟的属性显示脚本不包含document.write ,是一个线索浏览他们可以继续绘制.不幸的是, firefox不支持延迟属性.在ie脚本可能会延迟,但并没有像预期的.如果脚本可以推迟,也可以移到页面底部.这将使你的网页加载速度.
讨论第6



7 :避免css的表现

css的词句,是一个强有力的(危险)的方式来设定css的动态性能.他们支持在ie开始,第5版.为例,底色可以设定为候补每小时用css的词句.

      背景颜色:表达( (新日期( ) ) . gethours ( ) % 2 ? " # b8d4ff " : " # f08a00 " ) ;

所示,在这里,表达方法接受一个javascript表达式. 184属性设置结果评价javascript的表达.表达方法,是忽略了其他浏览器,所以这是有益的性能定在ie需要创造一个体验跨浏览器.

问题与表现,是他们的评价更加频繁,比大多数人预期的.他们不仅评价时,首页是译作,并加粗,而且当页面滚动,甚至当用户移动鼠标多页.加上柜184表达,使我们掌握什么时候以及一个css的表现作了评价.鼠标移页面周围容易产生超过10000评价.

一个办法是减少多少次你的css表现的评价是使用一次性的词句,并在第一时间表达,是评价套风格财产明示值,它取代了184的表达.如果风格财产必须是一套动态的整个生命的一页,利用事件处理不是css的表现是另一种做法.如果你必须使用css的表情,记住,他们可评估几千倍,并可能影响业绩,你的页面.
讨论第7



8 : javascript , css等外部

许多这些表现规则处理如何与外部元件的管理.然而,在这些因素出现,你要问一个更基本的问题:应该javascript , css等载入外部档案,或者在inlined页本身?

使用外部文件,在现实世界中普遍产生更快,因为页的javascript和css文件缓存由浏览器. javascript , css等都是inlined在html文件下载得到每次html文件要求.这一数量减少http请求的,有需要的,但增幅大小html文档.就另一方面来说,假如javascript , css等都是在外部文件缓存由浏览器,大小html文档是减少的数目增加http请求.

关键因素,则是9560万,其中外来的javascript和css组件缓存相对人数html文档的要求.这一因素,尽管很难量化,可衡量,用不同的衡量标准.如果用户对你的网站有多个页面的看法,每一届,你的许多页面再使用同样的脚本和样式,有一个更大的潜力,有利于从外部存储的文件.

许多网站属于夹在中间的这些度量.这些性能,最好的办法通常是部署javascript , css等外部文件.唯一的例外,我见过那里递归是最好的是网页,如雅虎!美国头版( http://www.yahoo.com )和我的雅虎! ( http://my.yahoo.com ) .主页,有几个(也许只有一个) ,网页浏览,每一届可能发觉递归javascript , css等成果更快终端用户响应时间.

为头版,通常第一页有很多意见,有很多技术的杠杆减少http请求递归规定,以及缓存取得的收益,通过使用外部档案.其中一种技巧是直列javascript , css等,在头版,但动态下载外部文件后,页面已装载完毕.随后页面将参考外部文件应该已经在浏览器的缓存.
讨论第8



9 :减少域名查找

域名系统( dns )映射到主机地址,就像地图电话簿的人的姓名,电话号码.当你输入www.yahoo.com到你的浏览器,一个域名解析器接触,由浏览器返回服务器的ip地址.域名是有代价的.它通常需要20-120毫秒域名解析为ip地址的某个主机名.浏览下载不了的事情,从这个主机,直到域名解析完成.

域名查找缓存更好的表现.这个缓存可以发生在一个特殊的缓存服务器,由用户的互联网或局域网,但也存在着缓存发生对个人用户的电脑.域名信息依然在操作系统中的dns缓存( "域名服务客户" ,对微软视窗) .大多数浏览器都有自己的高速缓存,分别从操作系统的缓存.只要浏览器保存的dns记录在其自己的高速缓存,它并不理会操作系统的请求纪录.

因特网探索者暗藏域名查找, 30分钟,默认情况下,指定由夹注册表设置.火狐暗藏域名查找,一分钟后,控制由network.dnscacheexpiration配置设置. ( fasterfox变化,这1个小时) .

当客户端的dns缓存是空的(无论浏览器和操作系统) ,数字域名系统查找数目相等独特的主机中的网页.这包括主机用在网页的url ,图片,脚本文件,样式,闪光物体等,减少了一些独特的主机数量减少域名查找.

减少一些独特的主机有可能减少金额并行下载的是发生在该页面.避免域名查找削减响应时间,但减少并行下载可能会增加响应时间.我的方针是,分裂这些组件全国至少有两个,但不超过4个主机.这一成果是一个很好的妥协之间减少域名查找,并允许高度并行下载.
讨论第9




10 :缩小了的javascript

微细的做法是去除不必要的字符码,以减少其体积,从而提高负荷倍.当典金评论全部拆除,以及不需要空白字符(空间,换行符,和tab ) .在本案的javascript ,这样就提高了反应时间表现,因为大小的下载文件,是减少.最常用的工具,为缩小javascript代码代码的[ url = ] jsmin [ / url ]今天,发达的道格拉斯克罗克福德,一位小伙子雅虎! .

迷惑的是另一种优化,可用于源代码.像缩,它消除了评论和白色空间,但它也客栈守则.一部分munging ,函数和变量的名字转换成小串,使代码更加紧凑以及难以阅读.这是典型的做更难逆向代码.但munging可以帮助表现,因为这样做可以减少代码大小,不光是达到缩.工具-选择是那么明确,在该区javascript的迷惑.试压缩机( shrinksafe )是一个我所见过用最.

缩是一种安全,使用相当简单的过程.迷惑,另一方面,是比较复杂的,因此更容易产生缺陷由于迷惑了自己的一步.困惑,还需要修改你的代码,以显示api函数和其它符号,不应该munged .它也使得更难调试你的代码生产.虽然我从来没有见过的问题,介绍了从微细化,我看到有缺陷所造成的混乱.在调查的十大美国网站,缩达到21 %的面积减少为25 %混乱.虽然困惑,有较高的尺寸缩小,我建议缩小javascript代码,因为在降低风险和维护费用.

除了缩小对外剧本,剧本inlined座可以,也应该是金.即使你gzip的脚本中所描述的那样在第4 ,缩小他们仍会缩小,由百分之五或以上.由于使用和大小的javascript增加,因此将节省所得缩小你的javascript代码.
讨论第10条




11 :避免重定向

重定向通过使用301和302状态码.这里的一个例子http头一个301回应:

       http/1.1标准301永久性搬到
      地点: http://example.com/newuri
      内容类型:文本/的html

浏览器会自动把用户该url指定的位置.所有必要的信息重定向是在头.身体的反应是典型的空洞.尽管他们的名字,既不是301或302的回应是缓存在实践中,如果没有额外的头信息,如到期或缓存控制,表明它应该的.元刷新标签和javascript等方式直接向用户不同的url ,但如果你必须做一个调整,首选方法是用标准的3xx http状态码,主要是为了确保后退按钮工程是否正确.

主要的是要记住骗至慢的用户体验.插入一个重定向至用户和html文件拖延一切页面因为没有在首页可以做出任何组件可以开始下载到html文件已经到来.
其中最浪费重定向发生频繁和网络开发者通常不知道有这回事.它发生在一个落后的斜线( / )失踪,从一个url ,否则应该有一个.譬如去http://astrology.yahoo.com/astrology结果,在301名回应含有重定向到http://astrology.yahoo.com/astrology/ (公告补充耙削减) .这是固定在apache使用别名或mod_rewrite ,或者directoryslash指令,如果你使用apache的默契.

连接旧网站的一个新的是另一种常用的重定向.其他设施包括连接不同部位的一个网站,并引导用户基于某些条件(类型的浏览器类型,用户帐号等等) .使用重定向来连接两个网站是简单,几乎不需要额外的编码.虽然使用重定向在这种情况下降低复杂性,为开发商,它降低了用户的体验.选择这种使用重定向包括使用化名和mod_rewrite如果两个代码路径都设在同一台服务器.如果一个域名变化,是导致使用重定向,另一种是创造的cname ( dns的纪录,创造了一个别名指向一个域名为另一种) ,结合别名或mod_rewrite .
讨论第11条



12 :删除重复的脚本

痛业绩,包括同样的javascript文件两次在一页.这并非不寻常,正如你想象的.审查的十大美国网站显示,其中两个含有重复脚本.主要有两个因素增加了赔率,剧本被重复在一个单一的网页:团队的规模和数量上的字体.当它发生,重复伤害剧本演出造成不必要的http请求和浪费的javascript执行.
不必要http请求发生在ie ,而不是才怪呢.在internet explorer中,如果一个外部脚本包含两次,是不是cacheable ,它生成两个http请求,在页面加载.即使脚本cacheable额外http请求发生时,用户刷新页面.
除了发电浪费http请求的,时间是浪费评价脚本成倍增长.这多余的javascript处决发生在firefox和ie浏览器,不论是剧本cacheable .

方法之一,以避免无意中包括同一脚本两次,是落实脚本管理模块在你的模板体系.典型的方法包括一个脚本是用脚本标签,在你的html网页.

      剧本式= "文/ javascript的而" src = " menu_1.0.17.js " > < /剧本>

另一种在php将创建一个称为功能insertscript .

       < ? php insertscript ( " menu.js " ) ? >

此外,为了防止同一脚本被插入多次,这个功能,可以处理其他问题的剧本,如扶养检查,并加入版本号码脚本文件,以支持对遥远的未来只剩头.
讨论第12条



13 :配置etags

实体标签( etags )是一种很好的机制,网络服务器和浏览器的使用,以确定是否构成在浏览器的缓存赛事之一,起源服务器. (一个"实体"是另一个字就是我一直在呼吁的"分量" :图像,文字,字体等) , etags加上提供一种机制,为验证实体,是一个更灵活,比去年改造日期.一个的etag是一串的独特识别特定版本的一个组成部分.唯一形式限制是字符串引用.起源服务器指明组件的使用的etag的etag响应头.

       http/1.1标准200好
      最后的修饰:周二, 2006年12月12日格林威治时间3时03分59秒
      的etag : " 10c24bc - 4ab - 457e1c1f "
      内容长度: 12195

稍后,如果浏览器已经验证的一个组成部分,它使用了,如果无的匹配头合格的etag回起源服务器.如果etags比赛中,一个304状态码返回减少回应12195个字节这个例子.

       得到/我/ yahoo.gif http/1.1标准
      主持人: us.yimg.com
      如果修饰的,因为:周二, 2006年12月12日格林威治时间3时03分59秒
      如果无的匹配: " 10c24bc - 4ab - 457e1c1f "
       304 http/1.1标准不修饰

问题etags是他们通常的建筑用属性,使它们特有的服务器主办的一个网站. etags不会比赛中,当浏览器获得的原始成分,从一台服务器,后来试图核实这一成分对不同的服务器,这种情况是很常见的网站上使用了服务器集群处理请求.默认情况下,双方的apache和iis嵌入数据的etag大幅降低赔率有效性试验成功对网站多台服务器.

的etag格式阿帕奇1.3和2.x的inode是尺寸时钟.虽然某一文件可能住在同一目录跨越多台服务器,并有相同的文件大小,权限,时间戳等,它的inode不同,是从一台服务器到另一阶段.

iis 5.0和6.0也有类似问题etags .格式etags对非法入境者是filetimestamp : changenumber .一changenumber是一个计数器用来跟踪配置更改为非法入境者.它不可能因为changenumber是同各iis服务器背后的一个网站.

最终结果是生成etags使用apache和iis完全相同的成分,不会配合,从一个服务器到另一个.如果etags不相匹配,用户不接受小,快304反应etags设计;相反,他们将得到一个200的正常反应,随着各项数据的组成部分.如果你请客,你的网站就只是一台服务器,这不是一个问题.但如果你有多个服务器接待你的网站,你使用apache或iis与默认的etag配置,你的用户越慢页面,你的服务器有较高的负荷,你消耗更多的带宽,而代理人总是一件'笔缓存内容,你的效率.即使你已经组成了一个遥远的未来只剩头,有条件get请求还是每当用户点击刷新或更新.

如果你不利用灵活的验证模型etags提供,它的好,只是移除的etag共有.最后修饰头验证基于组件的时间戳.取下的etag面积减少http头均反应和随后的请求.微软的这一支持,文章描述如何去除etags .在apache中,这样做的,只需添加下面一行到你的apache配置文件:

       无fileetag

评分

参与人数 1UCC +54 收起 理由
mike521zx + 54 有价值信息

查看全部评分

回复

使用道具 举报

发表于 2007-8-21 18:44:58 | 显示全部楼层
估计不用翻...
愿意看这个的人,应该都是可以看懂的......
回复

使用道具 举报

发表于 2007-8-22 01:09:12 | 显示全部楼层
不错。。。。。。。。:D
回复

使用道具 举报

 楼主| 发表于 2007-8-22 10:00:38 | 显示全部楼层
我暈,看到LS貼的那篇用機器翻譯的大作............

5: Put CSS at the Top  --> 5 :把css的大樓頂部  (汗~~)
應該是   5. 把CSS置於文件頂部
回复

使用道具 举报

发表于 2007-8-22 10:20:37 | 显示全部楼层
google translate还处在beta那...
再说beta完的其他语言翻的也......
回复

使用道具 举报

发表于 2007-8-22 13:47:09 | 显示全部楼层
~!@#$%^&*()_+~!@#$%^&*()_!@#*(_P{!@#$%^&OP}{][":';?>,],[./ :cry2:
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

关闭

站长推荐上一条 /1 下一条

Archiver|手机版|小黑屋|DeepTimes.NET 太空游戏站