简体   繁体   English

如何使用javascript在浏览器中测试从用户到服务器的延迟?

[英]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:这里有一些想法:

  1. Use IP address instead of DNS host name.使用 IP 地址而不是 DNS 主机名。
  2. Do the "ping" multiple times, throw out the highest and lowest scores, then average the rest.多次“ping”,扔掉最高和最低的分数,然后平均其余的分数。
  3. If your web page has a lot heavy processing going on, try to do the tests when you think the UI load is lightest.如果您的网页正在进行大量繁重的处理,请尝试在您认为 UI 负载最轻时进行测试。

使用jQuery你可以: $.ajax(url,settings)http://api.jquery.com/jQuery.ajax/ ),并采取从时间beforeSendcomplete通过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.

相关问题 如何使用Javascript检测最近的服务器以测试延迟/ ping速度? - How to detect nearest server to test latency/ping speed using Javascript? 浏览器的延迟/ Ping测试 - Latency / Ping test from Browser 如何通过浏览器确定远程服务器的延迟 - How to determine latency of a remote server through the browser 如何在浏览器中确定和记录远程服务器的延迟 - How to determine and record latency of a remote server in the browser 如何在浏览器中使用来自 JavaScript 的 SOCKS5 协议连接到服务器? - How to connect to server using SOCKS5 protocol from JavaScript in browser? 如果用户允许在浏览器中下载多个文件,如何使用 JavaScript 进行测试? - How to test with JavaScript if a user has allowed multiple file downloads in the browser? 如何使用gulp test和karma在浏览器中调试Javascript测试文件 - How to debug Javascript test files in the browser using gulp test and karma 如何使用javascript在浏览器中创建http服务器? - How to create http server in browser using javascript? 如何使用输入类型作为文件从浏览器将图像与javascript的json数据一起上传到服务器? - How to upload an image from the browser using input type as file to the server along with json data using javascript? 如何减少延迟和不错过服务器端事件中来自服务器的事件 - How to reduce latency and not miss events from server in Server Side Events
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM