Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
var Url = require('url');
var Http = require('http');
function noop() { }
function finish(err, res, callback) {
if (res)
res.destroy();
this.abort();
if (!this._isFinished) {
this._isFinished = true;
this.removeAllListeners();
this.on('error', noop);
clearTimeout(this._timeoutId);
return callback(err, res);
}
}
function request(method, url, options, callback) {
var uri = Url.parse(url);
var timeoutId;
uri.method = method.toUpperCase();
uri.headers = options.headers;
var req = Http.request(uri);
// Register handlers
req._isFinished = false;
req.once('error', function onerror(err) {
return finish.call(this, err, null, callback);
});
req.once('response', function onresponse(res) {
return finish.call(this, null, res, callback);
});
req.write(options.payload);
req._timeoutId = setTimeout(function () {
return finish(new Error('Client request timeout'));
}, 60000);
req.end();
}
var completed = 0;
var envelope = new Buffer(JSON.stringify({
a: 1,
b: 'asd',
c: { d: 123, e: 'asd' }
}));
var options = {
headers: {
'content-type': 'application/json',
'content-length': envelope.length
},
payload: envelope
};
function send() {
request('post', 'http://localhost:8000/', options, function (err, res) {
completed++;
send();
});
}
process.nextTick(function() {
for (var i = 0; i < 500; i++)
send();
});
setInterval(function () {
var stats = process.memoryUsage();
console.log(stats.rss, stats.heapTotal, stats.heapUsed, completed);
completed = 0;
}, 10000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.