I am testing async http requests using NodeJs 14 and AWS Lambda.
So, I am doing 100 asynchronous http GET requests and trying measure duration for every single request. Total duration: 5632.74 ms.
async makeRequests() {
const axList = []
for (const item of this.targets) {
const {url, method} = item
const config = {url, method}
const stat = {
startTime: this.getTime(),
duration: 0,
status: 0,
response: null
}
const ax = axios.create()
ax.interceptors.request.use((config) => {
config.headers['startTime'] = this.getTime().toString();
return config
})
ax.interceptors.response.use((response) => {
const currentTime = this.getTime()
const startTime = response.config.headers['startTime']
response.headers['duration'] = (currentTime - startTime).toString()
return response
})
const axItem = ax.request(config)
.then((resp) => {
const {status, data} = resp;
stat.duration = resp.headers['duration']
stat.response = data
stat.status = status
})
.catch((error) => {
console.log(error)
})
.finally(() => {
this.reponses.push(stat)
})
axList.push(axItem)
}
await axios.all(axList)
return this.reponses
}
In this case All requests are asynchronous but duration for every request is not correct or unexpected. Response logs:
{
"startTime": 1634169624637,
"duration": "4063",
"status": 200,
"response": {
"data": "OK"
}
},
{
"startTime": 1634169624638,
"duration": "4120",
"status": 200,
"response": {
"data": "OK"
}
},
// .... + 97 logs
{
"startTime": 1634169624837,
"duration": "5341",
"status": 200,
"response": {
"data": "OK"
}
}
But when I do request synchronously by blocking every request using await inside the loop. I get real duration time for every request.
async makeRequests() {
for (const item of this.targets) {
const {url, method} = item
const config = {url, method}
const stat = {
startTime: this.getTime(),
duration: 0,
status: 0,
response: null
}
const ax = axios.create()
ax.interceptors.request.use((config) => {
config.headers['startTime'] = this.getTime().toString();
return config
})
ax.interceptors.response.use((response) => {
const currentTime = this.getTime()
const startTime = response.config.headers['startTime']
response.headers['duration'] = (currentTime - startTime).toString()
return response
})
await ax.request(config)
.then((resp) => {
const {status, data} = resp;
stat.duration = resp.headers['duration']
stat.response = data
stat.status = status
})
.catch((error) => {
console.log(error)
})
.finally(() => {
this.reponses.push(stat)
})
}
return this.reponses
}
The duration for every request is realistic now. But it works synchronously which is bad and Total duration much more.
{
"startTime": 1634162343040,
"duration": "95",
"status": 200,
"response": {
"data": "OK"
}
},
{
"startTime": 1634162343135,
"duration": "44",
"status": 200,
"response": {
"data": "OK"
}
},
// .... + 97 logs
{
"startTime": 1634162349596,
"duration": "83",
"status": 200,
"response": {
"data": "OK"
}
}
What can I do for getting correct duration time for every request but at the same time it should be asynchronously ?