Javascript 测量服务器响应时间(客户端)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14849314/
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
Measuring server response time (client-side)
提问by Kraken
I would like to implement a script that would measure the response time of my server (= remote url) from a client using a regular browser without any extra plugins (Java etc.).
我想实现一个脚本,该脚本将使用常规浏览器从客户端测量我的服务器(= 远程 url)的响应时间,而无需任何额外的插件(Java 等)。
I'm only looking at the network speed(response time), not the page loading speed.
我只看网络速度(响应时间),而不是页面加载速度。
What would you recommend me to measure? It has to run on tcp/ip, not anything else like ICMP (ping).
你建议我测量什么?它必须在 tcp/ip 上运行,而不是像 ICMP (ping) 这样的其他任何东西。
How would you go around the implementation on the client side? I would prefer to use JavaScript (JQuery).
您将如何解决客户端的实现?我更喜欢使用 JavaScript (JQuery)。
Update:As I need a cross domain check, the ajax calls don't seem to be an option
更新:由于我需要跨域检查,ajax 调用似乎不是一个选项
Furthermore, the ajax method doesn't seem to precise at all. Results seem to have an overhead of about 50ms (it's almost a constant value, no matter what the domain is - I guess it's the processing time in between) on the testing machine in comparison to information from FireBug
此外,ajax 方法似乎根本不精确。与来自 FireBug 的信息相比,测试机器上的结果似乎有大约 50 毫秒的开销(这几乎是一个恒定值,无论域是什么 - 我猜这是两者之间的处理时间)
回答by Robert Fricke
You just need to time a request:
您只需要为请求计时:
var sendDate = (new Date()).getTime();
$.ajax({
//type: "GET", //with response body
type: "HEAD", //only headers
url: "/someurl.htm",
success: function(){
var receiveDate = (new Date()).getTime();
var responseTimeMs = receiveDate - sendDate;
}
});
回答by AmericanUmlaut
Create a resource that always returns an empty 200 OK response at a url like mysite.net/speedtest. Then something like:
创建一个资源,该资源始终在 mysite.net/speedtest 等 url 处返回空的 200 OK 响应。然后是这样的:
var startTime = (new Date()).getTime(),
endTime;
$.ajax({
type:'GET',
url: 'http://mysite.net/speedtest',
async: false,
success : function() {
endTime = (new Date()).getTime();
}
});
alert('Took ' + (endTime - startTime) + 'ms');
That will give you the time it takes to send a request to your server, plus the time it takes your server to load the minimum set of resources it requires to respond to a request.
这将为您提供向服务器发送请求所需的时间,以及服务器加载响应请求所需的最少资源集所需的时间。
New: Your question is now inconsistent with the additional information you've added. You want to get the client's ping to a URL that you don't control - that's quite a different matter than what you initially asked for, getting the response time from yourserver. If you're trying to get a client's ping to your server, an ajax call will do the trick. If you're trying to get their ping to some other server, then I'm a bit confused as to what your goal is.
新:您的问题现在与您添加的附加信息不一致。您想让客户端 ping 到您无法控制的 URL - 这与您最初要求的完全不同,从您的服务器获取响应时间。如果您试图让客户端 ping 到您的服务器,ajax 调用就可以解决问题。如果您试图将他们的 ping 连接到其他服务器,那么我对您的目标是什么感到有些困惑。
回答by Sudip Pal
var startTime = (new Date()).getTime(),
endTime;
$.ajax({
type: "GET",
url: "test.html",
dataType: "html",
success:function(data){
endTime = (new Date()).getTime();
}
});
Now take the difference.
现在来看看差异。
回答by Licson
You can do it very easy.
你可以很容易地做到这一点。
var t = Date.now();
var t2;
$.ajax({
url:'page',
success:function(){
t2 = Date.now();
console.log(t2-t);//the time needed to do the request
}
});
The page to request should be empty to mimic the additional load time.
要请求的页面应为空以模拟额外的加载时间。

