javascript:有没有可以测试网络速度的JS? [英] javascript: is there any JS can test network speed?

查看:159
本文介绍了javascript:有没有可以测试网络速度的JS?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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

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

2) I give this url to my friends

3)他们不需要执行任何操作,只需要访问该网页,然后在该网页中打印出等待时间即可.

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)如果该网页还可以(使用IP地址范围数据库)告诉访问者处于哪个状态,那将是一个加号.

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

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

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.

推荐答案

网络速度与网页加载延迟有很大不同,因为网页加载延迟包括许多其他因素,例如服务器端计算时间,关联的请求以及缓存,呈现,异步元素等等.

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.

在实践中,以下解决方案将测试从发出GET请求到完全接收到响应正文(但在呈现任何内容或检索到任何关联的资产之前)之间的持续时间.我使用 jQuery 框架在某种程度上简化了XHR的创建过程,尽管您可以自由使用其他实现. >

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");
    }
});

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

只需将其放在页面上的<script>标记中,就可以了.根据需要修改document.write调用,以将其放置在更显眼的位置.另外,您可以随时在其中插入对监视Web服务的后续调用,以将其写入数据库.

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.

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

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.

这篇关于javascript:有没有可以测试网络速度的JS?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆