[英]How to test latency from user to server in browser using javascript?
I want to gather some information using the visitors of my websites.我想使用我网站的访问者收集一些信息。
What I need is for each visitor to ping 3 different hostnames and then save the following info into a DB.我需要的是让每个访问者 ping 3 个不同的主机名,然后将以下信息保存到数据库中。
Visitor IP, latency 1,latency 2, latency 3
Of course everything has to be transparent for the visitor without interrupting him in any way.当然,一切都必须对访问者透明,而不能以任何方式打扰他。 Is this possible?这可能吗? Can you give me an example?你可以给我一个例子吗? Are there any plugins for jQuery or something to make it easier是否有任何 jQuery 插件或使其更容易的东西
EDIT编辑
This is what I have so far jsfiddle.net/dLVG6 but the data is too random.这是我到目前为止jsfiddle.net/dLVG6 所拥有的,但数据太随机了。 It jumps from 50 to 190它从 50 跃升至 190
This is going to be more of a pain that you might think.这会比你想象的更痛苦。
Your first problem is that Javascript doesn't have ping.您的第一个问题是 Javascript 没有 ping。 Mostly what Javascript is good at is HTTP and a few cousin protocols. Javascript 擅长的主要是 HTTP 和一些表亲协议。
Second problem is that you can't just issue some ajax requests and time the results (that would be way too obvious).第二个问题是你不能只发出一些 ajax 请求并对结果进行计时(这太明显了)。 The same origin policy will prevent you from using ajax to talk to servers other than the one the page came from.同源策略将阻止您使用 ajax 与页面来自的服务器以外的服务器通信。 You'll need to use JSONP, or change the src of an image tag, or something else more indirect.您需要使用 JSONP,或更改图像标签的 src,或其他更间接的方法。
Your third problem is that you don't want to do anything that will result in a lot of data being returned.您的第三个问题是您不想做任何会导致返回大量数据的事情。 You don't want data transfer time or extensive server processing to interfere with measuring latency.您不希望数据传输时间或大量服务器处理干扰测量延迟。
Fourth, you can't ask for URLs that might be cached.第四,您不能要求可能缓存的 URL。 If the object happened to be in the cache, you would get really low "latency" measurements but it wouldn't be meaningful.如果对象碰巧在缓存中,您将获得非常低的“延迟”测量值,但这没有意义。
My solution was to use an image tag with no src
attribute.我的解决方案是使用没有src
属性的图像标签。 On document load, set the src
to point to a valid server but use an invalid port.在文档加载时,将src
设置为指向有效服务器但使用无效端口。 Generally, it is faster for a server to simply reject your connection than to generate a proper 404 error response.通常,服务器简单地拒绝您的连接比生成正确的 404 错误响应更快。 All you have to do then is measure how long it takes to get the error event from the image.您所要做的就是测量从图像中获取错误事件所需的时间。
From The Filddle :来自小提琴:
var start = new Date().getTime();
$('#junkOne').attr('src', 'http://fate.holmes-cj.com:8886/').error(function () {
var end = new Date().getTime();
$('#timer').html("" + (end - start) + "ms");
});
The technique could probably be improved.这项技术可能会得到改进。 Here's some ideas:这里有一些想法:
使用jQuery你可以: $.ajax(url,settings)
( http://api.jquery.com/jQuery.ajax/ ),并采取从时间beforeSend
和complete
通过Date.now()
减去那些时代- >然后你有时间请求(虽然不完全是“Ping”)
2021: 2021 年:
Tried this again for a React app I'm building.为我正在构建的 React 应用程序再次尝试了这个。 I don't think the accuracy is too great.我不认为准确性太大。
const ping = () => {
var start = new Date().getTime();
api.get('/ping').then((res) => {
console.log(res)
var end = new Date().getTime();
console.log(`${end-start} ms`)
}, (err) => {
console.log(err)
})
};
Wrote my own little API, but I suppose there's just way too much going on during the request.编写了我自己的小 API,但我想在请求期间发生的事情太多了。
In terminal, I get about 23ms ping to my server.. using this it shoots up to like 200-500ms.在终端中,我对我的服务器的 ping 时间大约为 23 毫秒。使用它可以达到 200-500 毫秒。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.