Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to test latency from user to server in browser using javascript?

I want to gather some information using the visitors of my websites.
What I need is for each visitor to ping 3 different hostnames and then save the following info into a DB.

Visitor IP, latency 1,latency 2, latency 3

Of course everything has to be transparent for the visitor without interrupting him in any way. Is this possible? Can you give me an example? Are there any plugins for jQuery or something to make it easier

EDIT

This is what I have so far jsfiddle.net/dLVG6 but the data is too random. It jumps from 50 to 190

like image 706
Jim Avatar asked Apr 28 '13 19:04

Jim


People also ask

How to check server latency from browser?

Accordingly, [Liu Zhiyong] has come up with a tool he calls “pingms”, which allows you to check web server latency right from your browser. Rather than relying on ICMP, pingms performs a more realistic test. It takes the list of targets from the file “targets.js” and connects to each one over HTTP.

What is testing latency?

Testing Latency... What is Latency? Latency is the amount of time it takes for a computer or application to respond to your request. The less time it takes the better. This is a test where you want to score low and there's a theoretical limit to how low you can go. Zero. Meaning the response was instant.

How to test network and Internet latency (lag) in Microsoft Windows?

Test Network Latency Windows How to Test Network and Internet Latency (Lag) in Microsoft Windows - Using Windows Command Prompt 1 Access the Command Line Prompt. 2 Run Ping Loopback test. 3 Run Ping to a remote server. 4 Run Traceroute test. See More....

How do I test the latency of an IP address?

For example, using the following command, ping sends one ICMP packet per second to the specified IP address until it has sent 100 packets. Network testing tools such as netperf can perform latency tests plus throughput tests and more. In netperf, the TCP_RR and UDP_RR (RR=request-response) tests report round-trip latency.


2 Answers

This is going to be more of a pain that you might think.

Your first problem is that Javascript doesn't have ping. Mostly what Javascript is good at is HTTP and a few cousin protocols.

Second problem is that you can't just issue some ajax requests and time the results (that would be way too obvious). The same origin policy will prevent you from using ajax to talk to servers other than the one the page came from. You'll need to use JSONP, or change the src of an image tag, or something else more indirect.

Your third problem is that you don't want to do anything that will result in a lot of data being returned. You don't want data transfer time or extensive server processing to interfere with measuring latency.

Fourth, you can't ask for URLs that might be cached. If the object happened to be in the cache, you would get really low "latency" measurements but it wouldn't be meaningful.

My solution was to use an image tag with no src attribute. On document load, set the src to point to a valid server but use an invalid port. Generally, it is faster for a server to simply reject your connection than to generate a proper 404 error response. All you have to do then is measure how long it takes to get the error event from the image.

From The Filddle:

var start = new Date().getTime();
$('#junkOne').attr('src', 'http://fate.holmes-cj.com:8886/').error(function () {
    var end = new Date().getTime();
    $('#timer').html("" + (end - start) + "ms");
});

The technique could probably be improved. Here's some ideas:

  1. Use IP address instead of DNS host name.
  2. Do the "ping" multiple times, throw out the highest and lowest scores, then average the rest.
  3. If your web page has a lot heavy processing going on, try to do the tests when you think the UI load is lightest.
like image 127
slashingweapon Avatar answered Oct 11 '22 17:10

slashingweapon


With jQuery you could: $.ajax(url,settings)(http://api.jquery.com/jQuery.ajax/) and take the time from beforeSend and on complete via Date.now(), subtract those times -> then you have the time for the request (not excactly the "Ping" though)

like image 2
olsn Avatar answered Oct 11 '22 16:10

olsn