Skip to content

Instantly share code, notes, and snippets.

@daniyalzade
Created November 7, 2014 18:54
Show Gist options
  • Save daniyalzade/5a35da998f76ee08779d to your computer and use it in GitHub Desktop.
Save daniyalzade/5a35da998f76ee08779d to your computer and use it in GitHub Desktop.
parallelConnections.js
var async = require('async');
var http = require('http');
var URL = 'www.walmart.com';
var MAX_SOCKETS = 50;
var NUM_CONNECTIONS = 100;
http.globalAgent.maxSockets = MAX_SOCKETS;
var request = function(cb) {
var options = {
host: URL,
path: '/',
};
var callback = function(response) {
var str = '';
response.on('data', function (chunk) {
str += chunk;
});
response.on('end', function () {
cb(null, str);
});
};
var req = http.request(options, callback);
req.on('error', function(e) {
console.log('problem with request: ' + e.message);
cb(null, '');
});
req.end();
};
var execInParallel = [];
var functionToExec = function(cb) {
request(cb);
};
for(var i=0;i<NUM_CONNECTIONS;i++){
execInParallel.push(functionToExec);
}
var startTime = new Date();
console.log('****before**** (' + NUM_CONNECTIONS + ')');
async.parallel(execInParallel, function() {
var endTime = new Date();
console.log('****done**** (' + String(endTime - startTime) + ' ms)');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment