Skip to content Skip to sidebar Skip to footer

Javascript: Is There Any Js Can Test Network Speed?

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.

Solution 1:

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 timewindow.startTime = newDate();
    },

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

    success: function(){
        // once the request comes back, record the end timewindow.endTime = newDate();

        // take the difference, which will yield a number in milliseconds, and print it outdocument.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.

Solution 2:

  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.

Solution 3:

I think you could do a basic latency test with:

<scripttype="text/javascript">var time1 = newDate();
</script><scripttype="text/javascript"src="latency.js"></script>

latency.js would have:

var time2 = newDate();

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.

Solution 4:

Here's an example javascript app:

http://gfblip.appspot.com/

Source code is here:

https://github.com/apenwarr/blip

Post a Comment for "Javascript: Is There Any Js Can Test Network Speed?"