使用fetch in node.js测量API响应时间

使用fetch in node.js测量API响应时间,node.js,performance-testing,fetch-api,Node.js,Performance Testing,Fetch Api,my node.js应用程序中的一个函数是使用fetch函数调用外部API。 我试图找到最准确的方法来测量上述API的响应时间 我正在使用Date方法执行此操作: let start_time = new Date().getTime(); fetch('https://jsonplaceholder.typicode.com/posts/1') .then((res) => { return res.json(); }) .then((data) =

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

我正在使用Date方法执行此操作:

     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

您可以将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

您可以将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
你可以参考你可以参考