user871457
user871457

Reputation:

Latency / Ping test from Browser

I want to check ping from Browser to a Server.

Ping/Latency should not be from Server but from Client's Machine.

I have seen sites like Pingtest.net so it is possible to do from Browser.

Any language will do.

The server ping can also be tested by an UDP packet other than normal shell command ping.

How should i proceed. ?

Upvotes: 2

Views: 2085

Answers (4)

Gediz GÜRSU
Gediz GÜRSU

Reputation: 646

You need to warmup and then check the average here is the javascript

async function pingServer(url, timeoutDuration = 5000) {
    const controller = new AbortController();
    const signal = controller.signal;

    const timeoutId = setTimeout(() => controller.abort(), timeoutDuration);
    const startTime = Date.now();
    try {
        await fetch(`https://${url}`, { mode: 'no-cors', signal });
        const endTime = Date.now();
        return endTime - startTime; 
    } catch (e) {
        if (e.name === 'AbortError') {
            return "Timeout"; 
        }
        return "Error"; 
    } finally {
        clearTimeout(timeoutId);
    }
}

async function pingWithWarmup(url) {
    // Perform two warmup requests
    await pingServer(url);
    await pingServer(url);

    // Now collect the average of three requests
    let totalPing = 0;
    const pingResults = [];

    for (let i = 0; i < 3; i++) {
        const pingTime = await pingServer(url);
        if (typeof pingTime === 'number') {
            pingResults.push(pingTime);
        }
    }

    // Calculate the average ping time
    if (pingResults.length > 0) {
        totalPing = pingResults.reduce((a, b) => a + b) / pingResults.length;
    }

    return totalPing;
}

Do not forget cors issue no-cors.

I have implemented it to my website you can check the entire source:

https://tcplogger-website-firebase.web.app/urlpingtest.html

Upvotes: 0

user1416932
user1416932

Reputation: 297

Try this - I have invoked a Blank page website so that it doesnt end up calculating time to load instead serve as ping time. FYI - its still not going to be same as ping time measured by running the ping command.

function loadXMLDoc() {  
    var xhttp = new XMLHttpRequest(); 
    var ping = new Date;
    xhttp.open("GET", "http://cors-anywhere.herokuapp.com/blank.org/", false);
    xhttp.setRequestHeader('Cache-Control', 'no-cache');
    xhttp.send();
    if(xhttp.status == 200) {
             ping = new Date - ping;
             console.log('Ping ' + ping + 'ms');
    }
  }

Upvotes: 0

test
test

Reputation: 21

<script type="text/javascript">
try {
  netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserRead");
} catch (e) {
  alert("Permission UniversalBrowserRead denied.");
}

var req = new XMLHttpRequest();
    req.open('GET', 'http://www.mozilla.org/', false);.
    req.send();
    if(req.status == 200) {
        alert(req.responseText);
    }

Upvotes: 0

brunoais
brunoais

Reputation: 6866

Try using XMLHTTPRequest() for javascript. The objective is to make about 10 objects and place them in an array. Then use onreadystatechange in each and every one of them. For the function inside it, use the status 1 to start the timer and status 3 to finish the timer. Use the object Date() to get the times.

That's the whole idea behind solving that. if you need more just comment my answer and I'll try to make it more complete and "spoily".


Assuming you know how to program in javascript, this is one idea for a possible solution: You first create an array where you'll insert in each position an instance of XMLHTTPRequest(). Then, you'll create a function that returns a function where:
The outside function will save some sort of identification towards the "current" request, including a reference to the instance. This function is supposed to be executed right away.
The inner function is supposed to be used to execute the state and know when to "start" the timer and when to "stop" the timer. Take care of opening the connect and sending the headers to all of 'em, wait to get the answer and finally register the times you got. Do some math and you get the ping.

That's the whole idea behind this. Good luck!

Upvotes: 1

Related Questions