How to calculate the response time of a Node application?

Asked

Viewed 124 times

0

How to calculate the response time of an application in Node.js ?

Without much delay only the request being made outside and a method that returns time through the Date.now()

Example:

Web application:

function calcular() {
    var dataRequest = Date.now();
    $.ajax({
        url: 'https://rotaqueretornatempo/tempo,
        contentType: 'application/json',
        method: 'GET',
        dataType: 'json',
        success: function (data) {
            console.log(data);
            let tempo = Math.floor((data.resposta - dataRequest) / 1000)
            $("#demo").text(`Método: ${dataRequest} | Server: ${data.resposta}`)
        }
    });
}

Node application:

function applicationStatus(request: Request, response: Response){
  try {
    var dataResponse = Date.now();
    return response.json({resposta: dataResponse }) 
    
  } catch (error) {
    return response.json({resposta: error})
  }

}

routes.get('/tempo', applicationStatus);

How to return: Method: 1605634553413 | Server: 1605635103030

But when I subtract the values and make the conversion to seconds gives me an approximate return of 550 seconds, and it does not take even 1 second to have the answer.

  • 1

    Wouldn’t this difference in values be because of the synchronization of the server and the user’s browser? Because Voce is using the Date.now() of each environment and then probably they are not synchronized. I’m not sure it can be that. To deduce why Voce placed a url that is not local (localhost), so it is a remote server

  • That was exactly the problem. The server is 10 minutes ahead of the application time. Thanks

  • 1

    Hello Igor Carreiro, the server Timezone is different from the local Timezone (browser). Brazil uses -3 hours in most states (depending on daylight saving time), while the server usually uses "zero zone" (UTC): https://answall.com/a/417492/3635

1 answer

1


When you use the method to pick up the current time in the frontend, the time returned will be whatever the browser is configured. When you use this same method, only in Node.js, then the server time will be returned. Note that if the server time is a few milliseconds different from the user time, then the time measurement will be poorly done. My suggestion is to use some software like Postman, because it returns how many milliseconds exact the request spent.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.