Last active
August 29, 2015 14:10
-
-
Save tasinet/3ad091af55c659e5bd49 to your computer and use it in GitHub Desktop.
Node - http.request - KeepAlive test: traffic generator
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
//with 0.10.33 | |
//run with -mS1 and watch network: ~ 2 TCP connections, 6 UDP/DNS reqs <-- KeepAliv | |
//run without an option for maxSockets = 5 (default from globalAgent) and watch network: ~ 100 TCP connections and 400 UDP/DNS req <-- No KeepAlive | |
//this is modeled sequentially, which is closer to npm install's request pattern (rather than 100 requests fired off at init time) | |
//results in https://gist.github.com/tasinet/9cfe55909393a9ebb9c8 | |
var https = require('https'); | |
getCmd('-mS') | |
function getCmd( prefix ) { | |
return process.argv.filter(function(arg){ return arg.substr(0, prefix.length) === prefix; }).map(function(arg){ | |
return arg.substr(prefix.length); | |
})[0] || 5; | |
}; | |
https.globalAgent.maxSockets = getCmd('-mS') | |
console.log('maxSockets: ',https.globalAgent.maxSockets); | |
var options = { | |
hostname: 'registry.npmjs.org', | |
port: 443, | |
path: '/', | |
method: 'GET', | |
}; | |
function streamingReq(options, cb) { | |
var x = []; | |
var err = false, done = false; | |
var req = https.request(options, function(res) { | |
//console.log("statusCode: ", res.statusCode); | |
//console.log("headers: ", res.headers); | |
res.on('data', function(d) { x.push(d)}); | |
res.on('end', function() { | |
if (err) return; | |
done = true; | |
cb(null, res.statusCode, x.join("")); | |
}); | |
}); | |
req.end(); | |
req.on('error', function(e) { | |
if (done) return; | |
err = true; | |
cb(e); | |
}); | |
} | |
function makeReq(i) { | |
if (i) { | |
streamingReq(options, function(err, code, data) { | |
console.log(i, err || code, data); | |
makeReq(--i); | |
}); | |
} | |
} | |
makeReq(100); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment