文章出处:http://yuiblog.com/blog/2007/04/11/performance-research-part-4/
主要内容:
http 1.1规范,建议浏览器在每个域名下,使用两个并发请求。
figure1. Downloading 2 components in parallel
如果这些component分布在两个域名下,整体的响应时间大约快2倍:
figure 2. Downloading 4 components in parallel
每个域名限制使用两个并发请求,是一个指导准则。默认情况下,IE和火狐浏览器都遵循这样的准则,但是你也可以修改这个默认行为,IE浏览器把这个并发数量存储在注册表中,火狐浏览器可以通过访问页面 about:config,设置.http.max-persistent-connections-per-server 这个值来控制并发数。
页面请求的并发数最大化,是有代价的,取决于你的带宽和CPU的速度。太多的并发下载,反而会降低性能。
如果使用额外的域名别名,来增加页面的并发请求,会怎样呢?做一个实验:一个页面中,有20个图片,没有其他内容。实验中,改变域名别名的个数:1,2,4,5,10,从而对应的并发请求数量为2,4,8,10,20。页面获取20个小图片(36 x 36 px)和获取20个中等大小的图片(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.
并发请求数量越多,客户端的CPU负载越大,需要的带宽越多,并且多个并发,可能使用多个域名,就涉及到DNS的解析成本。也就是说,并发请求数量越多,响应时间可能越慢。
Our rule of thumb is to increase the number of parallel downloads by using at least two, but no more than four hostnames. Once again, this underscores the number one rule for improving response times: reduce the number of components in the page.