Created
October 16, 2019 11:44
-
-
Save abhi11210646/7d6be301bad4ad13921d820e75483a55 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const http = require("http"); | |
let keepAliveAgent = new http.Agent({ | |
keepAlive: true, | |
maxSockets: 5, | |
keepAliveMsecs: 60000 | |
}); | |
let name; | |
const P_REQ_COUNT = 15; | |
// make HTTP Request | |
function callMe(i) { | |
return new Promise((resolve, reject) => { | |
var options = { | |
host: "localhost", | |
path: "/" + i, | |
port: 3001, | |
agent: keepAliveAgent, | |
headers: { | |
'x-onecom-rid': 12344, | |
Connection: 'keep-alive' | |
}, | |
}; | |
name = `${options.host}:${options.port}:`; | |
var httpReq = http.get(options, function (response) { | |
var bodyChunks = []; | |
response.on('data', function (chunk) { | |
bodyChunks.push(chunk); | |
}).on('end', function () { | |
console.log("Response recieved for request: ", i); | |
log(); | |
if (i == P_REQ_COUNT) { | |
httpReqDebug(); // Log when all requests are done | |
} | |
resolve(); | |
}); | |
}); | |
httpReq.setTimeout(3000); | |
httpReq.on('timeout', function () { | |
console.log("Aborting request...", i); | |
httpReq.abort(); | |
}); | |
httpReq.on('error', function (err) { | |
console.log("Error recieved: ", i, err.message); | |
log(); | |
reject(); | |
}); | |
console.log("Making request: ", i); | |
log(); | |
httpReq.end(); | |
}) | |
} | |
(async function () { | |
for (let i = 0; i < P_REQ_COUNT; i++) { | |
// await callMe(i+1); // Series | |
callMe(i + 1); // parallel | |
} | |
})(); | |
async function httpReqDebug() { | |
await sleep(2000); | |
console.log("After 2 sec...."); | |
log(); | |
await sleep(3000); | |
console.log("After 5 sec...."); | |
log(); | |
await sleep(5000); | |
console.log("After 10 sec...."); | |
log(); | |
await sleep(5000); | |
console.log("After 15 sec...."); | |
log(); | |
} | |
function sleep(time) { | |
return new Promise((r, re) => setTimeout(() => r(), time)); | |
} | |
function log() { | |
console.log("++++++++++++++++++++++++") | |
console.log('Active Sockets => ', (keepAliveAgent.sockets[name] || []).length); | |
console.log('Queued Requests => ', (keepAliveAgent.requests[name] || []).length); | |
console.log('Free Sockets Pool=> ', (keepAliveAgent.freeSockets[name] || []).length); | |
console.log("++++++++++++++++++++++++") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment