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?
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?
Share Improve this question edited Jan 4, 2022 at 5:56 SuperStormer 5,4275 gold badges28 silver badges39 bronze badges asked Feb 19, 2020 at 9:47 ebyteebyte 1,5173 gold badges22 silver badges36 bronze badges 1- You can find alternatives here. stackoverflow./questions/313893/… – robinvrd Commented Feb 19, 2020 at 9:53
5 Answers
Reset to default 3It'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./sindresorhus/got#timings
https://github./sindresorhus/got/pull/590
You may use a 3rd Party statman-stopwatch to easily record the total time.
Example
const got = require('got');
const Stopwatch = require('statman-stopwatch');
async function timeSpent (){
const sw = new Stopwatch();
sw.start();
await got('***');
sw.stop();
const delta = sw.read();
return delta;
}
You can go through this way.
console.time('test1');
console.time('test2');
setTimeout( (elem) => {
console.log('You want to write something here!!');
console.timeEnd('test2');
}, 2000);
console.timeEnd('test1');
performance - is a native way to measure things like this.
const got = require('got');
async function timeSpent (){
var t0 = performance.now();
await got('***')
var t1 = performance.now();
console.log("Call to do something took " + (t1 - t0) + " milliseconds.");
return t1 - t0;
}
Check browser support https://caniuse./#search=performance
let currentDate = moment().format('YYYY/MM/DD HH:mm')
let dataCalca = moment(dataValue).format('YYYY/MM/DD HH:mm')
let hours = moment
.duration(moment(currentDate, 'YYYY/MM/DD HH:mm')
.diff(moment(dataCalca, 'YYYY/MM/DD HH:mm'))
).asHours();
So you get the numbers as an hour. You can calculate with that