示例代码:
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});
引用:
关于javascript - 使用got时如何计算花费的时间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60297317/