Asmoun
Asmoun

Reputation: 1747

How to calculate or get the response time of an API?

using cypress i'm intercepting an API call and want to get how much the response time it took

cy.intercept("PATCH", "https://api.website.com/b2b2/**").as("myAPP")
cy.wait("@myAPP").then(({ request, response }) => {
    expect(response.duration).to.be.lessThan(2000)// => expected undefined to be a number or a date
    expect(response.statusCode).to.equal(200)
}) 

Upvotes: 0

Views: 72

Answers (2)

Ketan Niruke
Ketan Niruke

Reputation: 19

there are many ways but You may try with

  1. using JavaScript (Node.js and Fetch API):

const fetch = require('node-fetch');

(async () => {
    const startTime = Date.now();

    const response = await fetch('https://api.example.com/data');
    const data = await response.json();

    const endTime = Date.now();
    const responseTime = endTime - startTime;

    console.log(`Response Time: ${responseTime} ms`);
})();

  1. Using Postman Postman, a popular API testing tool, provides an easy way to measure response time.

Open Postman. Send the API request. After the request is completed, Postman displays the response time in the bottom-right corner under the "Response" section.

Upvotes: 0

Yanxuan Weng
Yanxuan Weng

Reputation: 59

In JavaScript, you can measure how fast an API call is by using performance API.

For example:

const start = performance.now();
fetch('https://api.example.com/data')
    .then(response => response.json())
    .then(data => {
      const end = performance.now();
      const responseTime = end - start;
      console.log(`Response time: ${responseTime} milliseconds`);
    });

Upvotes: 0

Related Questions