Javascript 如何减少服务器“等待”时间?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10938682/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to reduce server "Wait" time?
提问by alnafie
I am trying to optimize my site's speed and I'm using the great tool at pingdom.com. Right now, over 50% of the time it takes to load the page is "Wait" time as shown in the screenshot below. What can I do to reduce this? Also, how typical is this figure? are there benchmarks on this? Thanks!
我正在尝试优化我网站的速度,我正在使用pingdom.com 上的出色工具。现在,加载页面所需的时间超过 50% 是“等待”时间,如下面的屏幕截图所示。我能做些什么来减少这种情况?另外,这个数字有多典型?有这方面的基准吗?谢谢!
EDIT:Ok.. let me clarify a few things. There are no server side scripts or database calls going on. Just HTML, CSS, JS, and images. I have already done some things like push js to the end of the body tag to get parallel downloads. I am aware that the main.html and templates.html are adding to the overall wait time by being done synchronously after js.js downloads, that's not the problem. I am just surprised at how much "wait" time there is for each request. Does server distance affect this? what about being on a shared server, does that affect the wait time? Is there any low-hanging fruit to remedy those issues?
编辑:好的..让我澄清一些事情。没有服务器端脚本或数据库调用正在进行。只是 HTML、CSS、JS 和图像。我已经做了一些事情,比如将 js 推送到 body 标签的末尾以获得并行下载。我知道 main.html 和 templates.html 通过在 js.js 下载后同步完成而增加了整体等待时间,这不是问题。我只是对每个请求有多少“等待”时间感到惊讶。服务器距离会影响这个吗?在共享服务器上怎么样,这会影响等待时间吗?是否有任何容易解决的问题来解决这些问题?
回答by Mickle Foretic
The most common reason for this in the case of Apache is the usage of DNS Reversal Lookup. What this means is that the server tries to figure out what the name of your machine is, each time you make a request. This can take several seconds, and that explains why you have a long WAIT time and then a very quick load, because the matter is not about bandwidth.
在 Apache 的情况下,最常见的原因是使用 DNS 反向查找。这意味着每次您发出请求时,服务器都会尝试找出您机器的名称。这可能需要几秒钟,这就解释了为什么您有很长的等待时间,然后加载非常快,因为问题与带宽无关。
The obvious solution for this is to disable hostnamelookup in /etc/httpd/conf/httpd.conf
显而易见的解决方案是在 /etc/httpd/conf/httpd.conf 中禁用主机名查找
HostnameLookups Off
However...this is usually NOT enough. The fact is that in many cases, apache still does a reversal lookup even when you have disabled host name lookup, so you need to take a careful look at each line of your apache config. In particular, one of the most common reasons for this are LOGS. By default on many red hat - centos installations, the log format includes %h which stands for "hostname", and requires apache to do a reverse lookup. You can see this here:
然而……这通常是不够的。事实是,在许多情况下,即使您禁用了主机名查找,apache 仍然会执行反向查找,因此您需要仔细查看 apache 配置的每一行。特别是,最常见的原因之一是日志。默认情况下,在许多 red hat - centos 安装中,日志格式包括 %h 代表“主机名”,并且需要 apache 进行反向查找。你可以在这里看到这个:
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
LogFormat "%h %l %u %t \"%r\" %>s %b" common
You should change those %h for %a to solve this problem.
您应该将 %h 更改为 %a 以解决此问题。
回答by jfriend00
If you have multiple server requests which the page is waiting on, you can make sure that those server requests are sent asynchronously in parallel so that you are serializing them.
如果您有页面正在等待的多个服务器请求,您可以确保这些服务器请求是并行异步发送的,以便您对它们进行序列化。
The slowest possible way to fetch multiple requests is to send one request, wait for its response, send the next request, wait for its response, etc... It's usually much faster to send all requests asynchronously and then process all responses as they arrive. This shortens the total wait time to the longest wait time for any single request rather than the cumulative wait time of all requests.
获取多个请求的最慢可能的方法是发送一个请求,等待其响应,发送下一个请求,等待其响应等......异步发送所有请求然后在所有响应到达时处理所有响应通常要快得多. 这将总等待时间缩短为任何单个请求的最长等待时间,而不是所有请求的累积等待时间。
If you are only making one single request, then all you can do on the client-side of things is to make sure that the request is sent to the server as early as possible in the page loading sequence so that other parts of the page can be doing their business while the request is processing, thus getting the initial request started sooner (and thus finishing sooner).
如果您只发出一个请求,那么您在客户端所能做的就是确保在页面加载顺序中尽早将请求发送到服务器,以便页面的其他部分可以在处理请求的同时处理他们的业务,从而更快地启动初始请求(从而更快地完成)。
回答by ddlshack
The wait time, also known as time to first byteis how long it takes for the server to send the first byte from when the connection is initiated. If this is high, it means your server has got to do a lot of work to render the page before sending it. We need more information about what your site is doing to render the page.
等待时间,也称为第一个字节的时间,是从连接开始时服务器发送第一个字节所需的时间。如果这个值很高,则意味着您的服务器必须在发送页面之前做大量工作来呈现页面。我们需要更多关于您的网站为呈现页面所做的工作的信息。
回答by Igal Zeifman
TTFB is directly influenced by "physical" distance between browser and server. CDN proxy is the best way to shorten said distance. This, coupled with native caching capabilities, will help provide swifter response by loading cached object from the nearest POP (point of placement) location.
TTFB 直接受浏览器和服务器之间“物理”距离的影响。CDN 代理是缩短所述距离的最佳方式。这与本机缓存功能相结合,将有助于通过从最近的 POP(放置点)位置加载缓存对象来提供更快速的响应。
The effect will depend on user geo-location and CDN's spread. Still, you can expect significant improvement, 50%-70% or more.
效果将取决于用户地理位置和 CDN 的传播。尽管如此,您仍然可以期待50%-70% 或更多的显着改进。
Speaking from experience, I saw cases in which 90% of content was cached and deliver directly from proxy placed on a different continent, from the other side of the globe.
根据经验,我看到 90% 的内容被缓存并直接从位于地球另一端的不同大陆的代理传送的情况。
回答by Techgration
This is an issue with the server... According to Pingdom, "The web browser is waiting for data from the server" is what defines the "Wait" time.
这是服务器的问题...根据 Pingdom 的说法,“Web 浏览器正在等待来自服务器的数据”是定义“等待”时间的原因。
There isn't much you can do from a javascript or code end to fix this.
您无法从 javascript 或代码端解决此问题。