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: