Created
February 26, 2020 06:28
-
-
Save WoZ/53d03a6ba46910466cfc1d9233f50bc2 to your computer and use it in GitHub Desktop.
No 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 no_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: false | |
}); | |
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