javascript: 有没有可以测试网速的JS?
我将测试我的网站速度,主要是网络服务器延迟。总结一下我想要实现的目标:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
网络速度与网页加载延迟有很大不同,因为后者包括许多其他因素,例如服务器端计算时间、相关请求和缓存、渲染、异步元素等。
实际上,以下解决方案将测试从发送 GET 请求到完全接收响应正文(但在渲染任何内容或检索任何关联资产之前)之间的持续时间。我使用 jQuery 框架来稍微简化 XHR 创建过程,尽管您可以自由地使用替代实现。
来源: 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.
Source: http://jsfiddle.net/5h4GZ/
Just plop this in a
<script>
tag on the page, and you're good to go. Modify thedocument.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.
var startTime = new Date;
var roundTripLatencyInMS = new Date-startTime;
在 XHR 请求的成功回调中。Google for Geolocation 来回答与此问题无关的第二个问题,或者提出一个单独的问题。
var startTime = new Date;
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.
我认为你可以使用以下命令进行基本的延迟测试:
latency.js 将具有:
我相信
time2 - time1
应该是粗略的往返延迟。这是执行latency.js
GET 请求并接收一行响应所需的时间。它并不精确,因为我们包括了用于处理数据包、传输实际线路(取决于带宽,而不是延迟)以及解析和执行 JavaScript 的时间。然而,延迟仍应占主导地位。
如前所述,4. 是标准 IP 地理定位。 此是一个使用
google 的简单演示.loader.ClientLocation
。google.loader.ClientLocation.address.region
应该是美国的州。I think you could do a basic latency test with:
latency.js would have:
I believe
time2 - time1
should be the rough round-trip latency. This is the time required to do thelatency.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.这是一个 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