javascript: 有没有可以测试网速的JS?

发布于 2024-10-09 16:13:28 字数 313 浏览 4 评论 0原文

我将测试我的网站速度,主要是网络服务器延迟。总结一下我想要实现的目标:

1)在我的网站上托管一个带有javascript的网页(http://myweb.com/test-speed.html)2

)我将此网址提供给我的朋友

3)他们不需要做任何事情,他们只需要访问这个网页,然后延迟就会在网页中打印出来。

4)如果网页还可以判断访问者处于哪种状态(使用IP地址范围数据库),那就更好了。

有现有的解决方案吗? 我可以修改javascript来将数据记录到数据库中,但我认为这里的核心是如何编写javascript来了解延迟。

I am going to test my website speed, primary the webserver latency. Summarize what I want to achieve:

1) a webpage with javascript hosted in my website(http://myweb.com/test-speed.html)

2) I give this url to my friends

3) They don't need to do anything, they just need to access this webpage then the latency is printed out in the webpage.

4) If the webpage can also tell which state the visitor is in(using IP address range database), it will be a plus.

Any existing solutions?
I can modify the javascript to log the data into database, but I think the core here is how to writ the javascript to know the latency.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

回眸一遍 2024-10-16 16:13:28

网络速度与网页加载延迟有很大不同,因为后者包括许多其他因素,例如服务器端计算时间、相关请求和缓存、渲染、异步元素等。

实际上,以下解决方案将测试从发送 GET 请求到完全接收响应正文(但在渲染任何内容或检索任何关联资产之前)之间的持续时间。我使用 jQuery 框架来稍微简化 XHR 创建过程,尽管您可以自由地使用替代实现。

// prepare the XHR object
$.ajax({
    beforeSend: function(){
        // right before firing off the request, create a global object that includes the request send time
        window.startTime = new Date();
    },

    // send the request to the root URI of this host
    url: '/',

    success: function(){
        // once the request comes back, record the end time
        window.endTime = new Date();

        // take the difference, which will yield a number in milliseconds, and print it out
        document.write(window.endTime - window.startTime + " ms");
    }
});

来源: http://jsfiddle.net/5h4GZ/

只需将其放入

网上有很多关于地理定位的教程,所以任何一个都可以。您可以利用 Geolocation API,但更有可能的是,您只需通过 IP 地址即可完成此操作,因为这实际上对您的需求更重要。为此,有大量在线网络服务可以为您提供基于 IP 的地理位置。当然,这一切都可以在服务器端完成,因此精确的实现将取决于您的技术。

Network speed is quite different from webpage loading latency since the latter includes a multitude of other factors like server-side computational time, associated requests and caching, rendering, asynchronous elements, and so on.

In practice, the following solution will test the duration between when a GET request is sent off to the time when the response body has been completely received (but before anything is rendered or any associated assets are retrieved). I use the jQuery framework to simplify the XHR creation process somewhat, although you're free to use an alternative implementation.

// prepare the XHR object
$.ajax({
    beforeSend: function(){
        // right before firing off the request, create a global object that includes the request send time
        window.startTime = new Date();
    },

    // send the request to the root URI of this host
    url: '/',

    success: function(){
        // once the request comes back, record the end time
        window.endTime = new Date();

        // take the difference, which will yield a number in milliseconds, and print it out
        document.write(window.endTime - window.startTime + " ms");
    }
});

Source: http://jsfiddle.net/5h4GZ/

Just plop this in a <script> tag on the page, and you're good to go. Modify the document.write call as required to put it in a more conspicuous place. Also, feel free to insert a subsequent call there to your monitoring web service to write it to a database.

There are plenty of tutorials on geolocation online, so any one of those will do. You can capitalize on the Geolocation API, but more likely, you'll do fine just to do it by IP address, since that's actually more important for your needs. For this, there are plenty of web services online that will give you a geolocation based on IP. This can of course all be done server side, so the precise implementation will depend on your technologies.

甜心小果奶 2024-10-16 16:13:28
  1. var startTime = new Date;
  2. 使用 XHR 向服务器请求一个简单的响应。 (jQuery 或更小的跨浏览器 XHR 包装器。)
  3. var roundTripLatencyInMS = new Date-startTime; 在 XHR 请求的成功回调中。

Google for Geolocation 来回答与此问题无关的第二个问题,或者提出一个单独的问题。

  1. var startTime = new Date;
  2. Use XHR to request a trivial response from your server. (jQuery or a much smaller cross-browser XHR wrapper.)
  3. var roundTripLatencyInMS = new Date-startTime; in the success callback from your XHR request.

Google for Geolocation for your second question that is unrelated to this one, or ask a separate question.

请别遗忘我 2024-10-16 16:13:28

我认为你可以使用以下命令进行基本的延迟测试:

<script type="text/javascript">
var time1 = new Date();
</script>

<script type="text/javascript" src="latency.js"></script>

latency.js 将具有:

var time2 = new Date();

我相信 time2 - time1 应该是粗略的往返延迟。这是执行 latency.js GET 请求并接收一行响应所需的时间。它并不精确,因为我们包括了用于处理数据包、传输实际线路(取决于带宽,而不是延迟)以及解析和执行 JavaScript 的时间。

然而,延迟仍应占主导地位。

如前所述,4. 是标准 IP 地理定位。 是一个使用 google 的简单演示.loader.ClientLocationgoogle.loader.ClientLocation.address.region 应该是美国的州。

I think you could do a basic latency test with:

<script type="text/javascript">
var time1 = new Date();
</script>

<script type="text/javascript" src="latency.js"></script>

latency.js would have:

var time2 = new Date();

I believe time2 - time1 should be the rough round-trip latency. This is the time required to do the latency.js GET request, and receive the one-line response. It's not precise because we're including time used to process the packet(s), transfer the actual line (which depends on bandwidth, not latency), and parse and execute the JavaScript.

However, latency should still dominate.

As noted, 4. is standard IP geolocation. This is a simple working demo using google.loader.ClientLocation. google.loader.ClientLocation.address.region should be the state in the US.

御守 2024-10-16 16:13:28

这是一个 JavaScript 应用程序示例:

http://gfblip.appspot.com/

源代码位于:

https://github.com/apenwarr/blip

Here's an example javascript app:

http://gfblip.appspot.com/

Source code is here:

https://github.com/apenwarr/blip

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文