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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With