Created
February 23, 2020 14:52
-
-
Save WoZ/e4173957707270ecf2200cfa8665c7d1 to your computer and use it in GitHub Desktop.
KeepAlive test stand
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 url = process.argv[2]; | |
const concurrency = process.argv[3] === undefined ? 1 : parseInt(process.argv[3]); | |
const requestInterval = process.argv[4] === undefined ? 10000 : parseInt(process.argv[4]); | |
if (url === undefined || isNaN(concurrency) || isNaN(requestInterval)) { | |
console.log('Usage: node keepalive_test.js http://google.com/ 10 10000'); | |
console.log('Where: 2nd - URL'); | |
console.log(' 3rd - amount of concurrent connections (default is 1)'); | |
console.log(' 4rd - interval between sequential requests'); | |
process.exit(); | |
} | |
const http = require('http'); | |
const keepAliveAgent = new http.Agent({ | |
keepAlive: true, | |
keepAliveMsecs: 60000, | |
maxSockets: 5 | |
}); | |
let globalRequestNo = 0; | |
function start(concurrency, timeout) { | |
for (let i = 0; i < concurrency; i++) { | |
makeRequest().then( | |
() => scheduleNextRequest(timeout), | |
() => scheduleNextRequest(timeout) | |
); | |
} | |
} | |
function scheduleNextRequest(timeout) { | |
setTimeout(() => { | |
makeRequest().then( | |
() => scheduleNextRequest(timeout), | |
() => scheduleNextRequest(timeout) | |
); | |
}, timeout); | |
} | |
function makeRequest() { | |
return new Promise((resolve, reject) => { | |
let requestNo = globalRequestNo++; | |
console.log(`REQUEST ${requestNo}: starting`); | |
const options = {method: 'GET', agent: keepAliveAgent}; | |
const req = http.request(url, options, res => { | |
res.on('data', chunk => {console.log(`BODY: ${chunk}`);}); | |
res.on('end', () => { | |
console.log(`REQUEST ${requestNo}: finished`); | |
return resolve(); | |
}); | |
req.on('error', e => { | |
console.log(`REQUEST ${requestNo}: ${e.toMessage()}`); | |
return reject(e); | |
}); | |
}); | |
req.end(); | |
}); | |
} | |
start(concurrency, requestInterval); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment