最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to calculate the time spent when using got? - Stack Overflow

programmeradmin6浏览0评论

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
Add a ment  | 

5 Answers 5

Reset to default 3

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./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

发布评论

评论列表(0)

  1. 暂无评论