【问题标题】:Measure API response time with fetch in node.js在 node.js 中使用 fetch 测量 API 响应时间
【发布时间】:2020-08-02 21:00:43
【问题描述】:

我的 node.js 应用程序中的一个函数是使用 fetch 函数调用外部 API。 我正在尝试找到最准确的方法来测量所述 API 的响应时间。

我正在使用“日期”方法来做到这一点:

     let start_time = new Date().getTime();
      fetch('https://jsonplaceholder.typicode.com/posts/1')
  .then((res) => {
    return res.json();
  })
.then((data) => {
  console.log('Response time:', new Date().getTime() - start_time);

有没有更好\更准确的方法来使用 fetch 呢?

【问题讨论】:

标签: node.js performance-testing fetch-api


【解决方案1】:

您可以将 console.time 和 console.timeEnd 用作:

    console.time("timer1");
      fetch('https://jsonplaceholder.typicode.com/posts/1')
  .then((res) => {
    return res.json();
  })
.then((data) => {
  console.timeEnd("timer1");

它会打印过去的时间

timer1: 0.105ms

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-05-22
    • 2020-11-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-13
    • 2020-11-16
    • 1970-01-01
    相关资源
    最近更新 更多