Skip to content

Instantly share code, notes, and snippets.

@creationix
Created June 9, 2010 01:49
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 7 You must be signed in to fork a gist
  • Save creationix/430932 to your computer and use it in GitHub Desktop.
Save creationix/430932 to your computer and use it in GitHub Desktop.
var child_process = require('child_process'),
sys = require('sys'),
net = require('net'),
netBinding = process.binding('net');
var fd = netBinding.socket('tcp4');
netBinding.bind(fd, 8080);
netBinding.listen(fd, 128);
for (var i = 0; i < 4; i++) {
// Create an unnamed unix socket to pass the fd to the child.
var fds = netBinding.socketpair();
// Spawn the child process
var child = child_process.spawn(
process.argv[0],
[__dirname + '/worker.js'],
undefined,
[fds[1], -1, -1]
);
// For some reason stdin isn't getting set, patch it externally
if (!child.stdin) {
child.stdin = new net.Stream(fds[0], 'unix');
}
child.stdin.write(sys.inspect(fd), "ascii", fd);
// Echo output from child
child.stderr.addListener('data', function (data) {
process.stdout.write(data);
});
}
// When run with ab with varying numbers of workers, mape got the following results:
// The ab used ab -n 20000 -c 100 on a quad-core debian system.
// 1 worker: Requests per second: 6985.85 [#/sec] (mean)
// 2 workers: Requests per second: 13420.92 [#/sec] (mean)
// 3 workers: Requests per second: 19375.66 [#/sec] (mean)
// 4 workers: Requests per second: 23868.80 [#/sec] (mean)
var net = require('net'),
http = require('http'),
sys = require('sys');
sys.debug("Child started " + process.pid);
// var stdin = process.openStdin();
var stdin = new net.Stream(0, 'unix');
stdin.addListener('fd', function(fd) {
http.createServer(function (req, res) {
res.writeHead(200, {"Content-Type": "text/plain"});
res.end("Hello from " + process.pid);
}).listenFD(fd, "tcp4");
});
stdin.resume();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment