How To Measure How Much Time Request Take In Nodejs
const axios = require('axios'); axios.get('url') .then(response => { console.log(response) }) .catch(err => console.log(err)) How can i measure how much time did
Solution 1:
You can use performance.now()
to measure the time between starting and finishing the request.
const axios = require("axios");
const { performance } = require('perf_hooks');
let time = performance.now();
axios.get('url')
.then(response => {
console.log(response)
console.log(`${(performance.now() - time) / 1000} seconds`);
})
.catch(err =>console.log(err))
Solution 2:
Also, You can easily use axios.interceptors
for getting the time at request start and after the response has reached And add it as a part of axios.create
.
For request:
axios.interceptors.request.use(function (config) {
config.timeData = { startTime: newDate()}
return config;
}, function (error) {
returnPromise.reject(error);
});
And for response:
axios.interceptors.response.use(function (response) {
response.config.timeData.endTime = newDate()
return response;
}, function (error) {
returnPromise.reject(error);
});
You can checkout more on interceptors here
Post a Comment for "How To Measure How Much Time Request Take In Nodejs"