How to calculate the time spent when using got?

ebyte ebyte :

Example code:

const got = require('got');
async function timeSpent (){
   const time = new Date().getTime()
   await got('***')
   return new Date().getTime() - time
}

I wonder if there is a better way?

Nico Van Belle :

It's not required to implement your own timing logic when using got.

The response includes a timings object that collects all millis spent per phase.

You only need to read out timings.phases.total from the response object.

const path = "http://localhost:3000/authenticate";
const response = await got.post(path, {
  body: { username: "john_doe", password: "mypass" },
  json: true,
  headers: {
    Accept: "application/json",
    "Content-type": "application/json",
  },
});

logger.debug({type: 'performance', path, duration: response.timings.phases.total});

Reference:

https://github.com/sindresorhus/got#timings

https://github.com/sindresorhus/got/pull/590

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=11333&siteId=1