Skip to content

Instantly share code, notes, and snippets.

@marcelklehr
Created October 13, 2012 20:11
Show Gist options
  • Save marcelklehr/3885976 to your computer and use it in GitHub Desktop.
Save marcelklehr/3885976 to your computer and use it in GitHub Desktop.
Test node's http.request
var http = require('http')
Number.prototype.times = function(fn) {
for(var i=1; i <= this; i++) process.nextTick(fn.bind(null, i))
return this
}
var finished=0;
function finish(i) {
finished++;
console.timeEnd('request '+i)
console.log('- -')
console.log('Outstanding requests:', http.globalAgent.requests['www.google.com:80']? http.globalAgent.requests['www.google.com:80'].length : 0)
console.log('Processing requests:', http.globalAgent.sockets['www.google.com:80']? http.globalAgent.sockets['www.google.com:80'].length : 0)
console.log('Finished requests:', finished)
console.log('- -')
if(finished == pool) process.exit()
}
var pool = (10).times(function(i) {
console.log('Requesting '+i)
console.time('request '+i)
http.get("http://www.google.com/", function(res) {
console.log("\nGot response for "+i+": " + res.statusCode)
finish(i)
}).on('error', function(e) {
console.log("\nGot error for "+i+": " + e.message)
finish(i)
});
})
console.log('Total requests '+pool)
console.log('Max sockets '+(http.globalAgent.maxSockets=5))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment