Skip to content

Instantly share code, notes, and snippets.

@neruthes
Last active September 22, 2020 07:19
Show Gist options
  • Select an option

  • Save neruthes/44ee6b4be2b8b2cabe01774ec1c695f5 to your computer and use it in GitHub Desktop.

Select an option

Save neruthes/44ee6b4be2b8b2cabe01774ec1c695f5 to your computer and use it in GitHub Desktop.
const pingHttps = function(limit) {
let totalTimes = -1;
const myPingData = [];
const analyzeData = function (inputDataArr) {
console.log('Raw Ping Data:');
console.log(myPingData);
var roundTripTimeArr = inputDataArr.map(x => x.end - x.begin);
var avgRoundTripTime = roundTripTimeArr.reduce((a, b) => a + b) / roundTripTimeArr.length;
console.log(`Tested ${roundTripTimeArr.length} times. Average round trip time: ${avgRoundTripTime} ms.`);
};
const microPing = function (limit) {
totalTimes += 1;
var currentSeq = totalTimes + 0;
var xhr = new XMLHttpRequest();
xhr.open('GET', location.href + '?t=' + Date.now());
xhr.onload = (function (currentSeq) {
return function () {
myPingData[currentSeq].end = Date.now();
if (totalTimes < limit - 1) {
microPing(limit);
} else {
analyzeData(myPingData);
};
};
})(currentSeq);
xhr.send();
myPingData[currentSeq] = {
begin: Date.now()
};
};
microPing(limit);
};
pingHttps(10);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment