Skip to content

Instantly share code, notes, and snippets.

@isaacs
Created September 15, 2013 05:36
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save isaacs/6568289 to your computer and use it in GitHub Desktop.
Save isaacs/6568289 to your computer and use it in GitHub Desktop.
console.log(process.argv[2]);
switch (process.argv[2]) {
case 'child':
return child();
case undefined:
return parent();
default:
throw new Error('wtf');
}
function parent() {
var http = require('http');
var server = http.createServer(function(req, res) {
var bigResponse = new Buffer(10240).fill('x');
res.end(bigResponse);
});
server.listen(10007, function() {
console.error('spawning children');
var spawn = require('child_process').spawn;
var node = process.execPath;
var args = [__filename, 'child'];
var opt = {}; // stdio: 'inherit' };
var c1 = spawn(node, args.concat('1'), opt);
var c2 = spawn(node, args.concat('2'), opt);
var c3 = spawn(node, args.concat('3'), opt);
var c4 = spawn(node, args.concat('4'), opt);
setInterval(function() {
console.error('master: %j', process.memoryUsage());
}, 1000);
});
}
function child() {
var net = require('net');
var conn = net.connect({ port: 10007 });
var req = 'GET / HTTP/1.1\r\nHost: localhost:10007\r\nAccept: */*\r\n\r\n';
req = new Array(10241).join(req);
conn.on('connect', function() {
write();
});
conn.on('drain', write);
function write() {
console.error('%s: write requests', process.argv[3]);
var i = 1;
while (false !== conn.write(req, 'ascii')) i++;
console.error('%s: wrote %d of request', process.argv[3], i * req.length);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment