I was inspired by the last comment, so I wrote this quick piece of code.
This is a kind of "HTTP request", which, it seems to me, can be very useful for use with XMLHttpRequest () calls, for example, to find out which one is the fastest server in any case or to collect approximate statistics of the user's internet connection speed.
This small function simply connects to the HTTP server at a nonexistent URL (which is expected to return 404), then measures the time until the server responds to the HTTP request and does a cumulative time and number of iterations.
The requested URL is randomly modified on every call, as I noticed that (maybe) some transparent proxies or caching mechanisms, in which in some cases there are falsifications that give faster answers (faster than ICMP in fact, which is somewhat strange).
Beware of using FQDNs that match the real HTTP server! Results will be displayed in the body element with the identifier "result", for example:
<div id="result"></div>
Function Code:
function http_ping(fqdn) { var NB_ITERATIONS = 4; // number of loop iterations var MAX_ITERATIONS = 5; // beware: the number of simultaneous XMLHttpRequest is limited by the browser! var TIME_PERIOD = 1000; // 1000 ms between each ping var i = 0; var over_flag = 0; var time_cumul = 0; var REQUEST_TIMEOUT = 9000; var TIMEOUT_ERROR = 0; document.getElementById('result').innerHTML = "HTTP ping for " + fqdn + "</br>"; var ping_loop = setInterval(function() { // let change non-existent URL each time to avoid possible side effect with web proxy-cache software on the line url = "http://" + fqdn + "/a30Fkezt_77" + Math.random().toString(36).substring(7); if (i < MAX_ITERATIONS) { var ping = new XMLHttpRequest(); i++; ping.seq = i; over_flag++; ping.date1 = Date.now(); ping.timeout = REQUEST_TIMEOUT; // it could happen that the request takes a very long time ping.onreadystatechange = function() { // the request has returned something, let log it (starting after the first one) if (ping.readyState == 4 && TIMEOUT_ERROR == 0) { over_flag--; if (ping.seq > 1) { delta_time = Date.now() - ping.date1; time_cumul += delta_time; document.getElementById('result').innerHTML += "</br>http_seq=" + (ping.seq-1) + " time=" + delta_time + " ms</br>"; } } } ping.ontimeout = function() { TIMEOUT_ERROR = 1; } ping.open("GET", url, true); ping.send(); } if ((i > NB_ITERATIONS) && (over_flag < 1)) { // all requests are passed and have returned clearInterval(ping_loop); var avg_time = Math.round(time_cumul / (i - 1)); document.getElementById('result').innerHTML += "</br> Average ping latency on " + (i-1) + " iterations: " + avg_time + "ms </br>"; } if (TIMEOUT_ERROR == 1) { // timeout: data cannot be accurate clearInterval(ping_loop); document.getElementById('result').innerHTML += "<br/> THERE WAS A TIMEOUT ERROR <br/>"; return; } }, TIME_PERIOD); }
For example, starting with:
fp = new http_ping("www.linux.com.au");
Please note that I could not find a simple relationship between the outcome metrics from this script and ICMP ping on the respective servers, although the HTTP response time seems to be roughly exponential from the ICMP response time. This can be explained by the amount of data that is transmitted through an HTTP request, which can vary depending on the taste and configuration of the web server, obviously, the speed of the server itself and, possibly, other reasons.
This is not very good code, but I thought it could help and perhaps inspire others.