This question already has an answer here:
这个问题在这里已有答案:
- Find out how long an Ajax request took to complete 6 answers
了解Ajax请求完成6个答案需要多长时间
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的信息相比,测试机器上的结果似乎有大约50ms的开销(它几乎是一个恒定值,无论域是什么 - 我猜它是两者之间的处理时间)
4 个解决方案
#1
7
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;
}
});
#2
1
Create a resource that always returns an empty 200 OK response at a url like mysite.net/speedtest. Then something like:
创建一个始终在mysite.net/speedtest等网址上返回空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 your server. 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到其他服务器,那么我对你的目标有点困惑。
#3
0
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.
现在采取不同。
#4
0
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.
要请求的页面应为空,以模拟额外的加载时间。
#1
7
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;
}
});
#2
1
Create a resource that always returns an empty 200 OK response at a url like mysite.net/speedtest. Then something like:
创建一个始终在mysite.net/speedtest等网址上返回空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 your server. 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到其他服务器,那么我对你的目标有点困惑。
#3
0
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.
现在采取不同。
#4
0
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.
要请求的页面应为空,以模拟额外的加载时间。