Last active
September 17, 2015 22:23
-
-
Save stephank/1c18a3ce3e5c1a40e282 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env node | |
'use strict'; | |
// The master must be reasonably stressed for the crash to appear. These | |
// parameters can be tweaked to accomplish that. Increasing them too far can | |
// cause backlog fill and undesirable connection resets. | |
// | |
// These particular values seem to work on Ubuntu 15.04 running on an EC2 | |
// c3.large instance with official node.js 4.1.0 binaries. (Everything 64-bit) | |
var WORKERS = 50; | |
var CONN_PER_WORKER = 200; | |
var cluster = require('cluster'); | |
if (cluster.isMaster) { | |
var http = require('http'); | |
// Simple HTTP upgrade server that simply ignores all data. | |
var server = http.createServer(); | |
server.on('upgrade', function(req, sock) { | |
sock.resume(); | |
}); | |
server.listen(function() { | |
var port = server.address().port; | |
cluster.setupMaster({ args: [port] }); | |
for (var i = 0; i < WORKERS; i++) | |
cluster.fork(); | |
}); | |
} | |
else { | |
var net = require('net'); | |
var port = process.argv[2]; | |
var connections = []; | |
var numConnect = 0; | |
// Create connections up to the per worker limit. | |
var connectInterval = setInterval(doConnect, 20); | |
function doConnect() { | |
if (numConnect++ === CONN_PER_WORKER) | |
clearInterval(connectInterval); | |
var conn = net.connect(port); | |
conn.on('connect', function() { | |
conn.write( | |
'GET / HTTP/1.1\r\n' + | |
'Host: localhost:' + port + '\r\n' + | |
'Upgrade: nonsense\r\n' + | |
'Connection: upgrade\r\n' + | |
'\r\n' | |
); | |
connections.push(conn); | |
if (connections.length === CONN_PER_WORKER) { | |
setInterval(doMsg, 0); | |
process.stdout.write('.'); | |
} | |
}); | |
conn.resume(); | |
} | |
// Send data on random open connection. | |
function doMsg() { | |
for (var i = 0; i < CONN_PER_WORKER; i++) | |
connections[i].write('garbage'); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment