Javascript: is there any JS capable of testing network speed?

I am going to test the speed of my website, mainly the web server latency. Summarize what I want to achieve:

1) a web page with javascript hosted on 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 web page, then the delay will be printed on the web page.

4) If the web page can also indicate what state the visitor is in (using the database of IP range), this will be a plus.

Any existing solutions? I can modify javascript to write data to the database, but I think the core here is how to write javascript to find out the delay.

+3
source share
4 answers

Network speed is very different from the latency of loading a web page, since the latter includes many other factors, such as server-side computing time, related requests and caching, rendering, asynchronous elements, etc.

In practice, the following solution will check the duration between when the GET request is sent at the time the response body was fully received (but before anything is displayed or any related assets are retrieved). I use the jQuery framework to simplify the process of creating XHR, although you can 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/

<script> , . document.write , . , - , .

- , . API , , , , IP-, . - , IP. , , .

+8

, :

<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.ClientLocation. google.loader.ClientLocation.address.region .

+2
  • var startTime = new Date;
  • XHR . (jQuery - XHR.)
  • var roundTripLatencyInMS = new Date-startTime; XHR.

Google for geolocation for your second question that is not relevant to this, or ask a separate question.

+2
source

Here is an example javascript application:

http://gfblip.appspot.com/

Source code here:

https://github.com/apenwarr/blip

0
source

Source: https://habr.com/ru/post/1782872/


All Articles