Home > Software engineering >  is ping measured in time to and from the server, or time between the request being made and the serv
is ping measured in time to and from the server, or time between the request being made and the serv

Time:09-26

look at the title

idk how to do specifically, a ping in javascript,

but I made this itty bitty snippet that returns

  • the time the request was made,
  • the time the server received the request, (done from server side)
  • and the time the server sent the response.
async function getResponseTimeOnce(){
  var times = {};
  times.beforeRequest = Date.now();
  await fetch("https://randobytes.yimmee.repl.co/ping").then((serverReceive)=>{
    serverReceive.json().then((serverReceive)=>{
      times.serverReceive = serverReceive;
    });
    times.afterRequest = Date.now();
  });
  return times;
}

all I'm asking, is, which values am I supposed to subtract to get the ping time?

CodePudding user response:

In standard ping programs, the latency measurement is always the round-trip-time (RTT). You should subtract beforeRequest from afterRequest to get the difference.

Source: https://developer.mozilla.org/en-US/docs/Glossary/Round_Trip_Time_(RTT)

$ ping example.com
PING example.com (216.58.194.174): 56 data bytes
64 bytes from 216.58.194.174: icmp_seq=0 ttl=55 time=25.050 ms
64 bytes from 216.58.194.174: icmp_seq=1 ttl=55 time=23.781 ms
64 bytes from 216.58.194.174: icmp_seq=2 ttl=55 time=24.287 ms
64 bytes from 216.58.194.174: icmp_seq=3 ttl=55 time=34.904 ms
64 bytes from 216.58.194.174: icmp_seq=4 ttl=55 time=26.119 ms
--- google.com ping statistics ---
5 packets transmitted, 5 packets received, 0.0% packet loss
round-trip min/avg/max/stddev = 23.781/26.828/34.904/4.114 ms

For this particular application, you may also consider using the Performance API to get a high resolution timestamp.

CodePudding user response:

Ping uses RTD/RTT (Round-trip delay/time). The RTD should be calculated from the time you sent the request to the time you receive the response.

  • Related