javascript - 使用got时如何计算花费的时间?

标签 javascript node.js time xmlhttprequest node.js-got

示例代码:

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

不知道有没有更好的办法?

最佳答案

使用got时不需要自己实现时序逻辑。

响应包含一个计时对象,该对象收集每个阶段花费的所有毫秒数。

你只需要从响应对象中读出timings.phases.total

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});

引用:

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

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

关于javascript - 使用got时如何计算花费的时间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60297317/

相关文章:

javascript - 在 ChartJS 散点图中显示图像而不是点

node.js - 使用 MongoDb 获取基于产品的价格范围..?

javascript - 如何使用 localhost 在浏览器中运行 Web 应用程序?

node.js - 如何在 expressjs 中使用 director 作为路由器

javascript - 如何在 javascript 中使用 ICU 字符串获取日期的 ISO8601 表示?

c++ - 关于strftime的一个问题

c - 从特定时区获取日期 - C

javascript - 在 Meteor 中添加时间和日期

javascript - 添加功能的笨拙实现

javascript - 查找具有没有 id 和类的文本值的 b 标签的父级