Skip to content

Instantly share code, notes, and snippets.

@yukw777
Last active October 25, 2018 15:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yukw777/e7d81347771f9b3a3abefaf8b805a6e4 to your computer and use it in GitHub Desktop.
Save yukw777/e7d81347771f9b3a3abefaf8b805a6e4 to your computer and use it in GitHub Desktop.
/*
This server simply bombards each websocket connection with a message every millisecond up to 20k messages.
How to run.
1. Run pushpin locally with the default publish port and backend port as 8080.
ex: pushpin --merge-output --port=17999 --route="* localhost:8080,over_http" --config pushpin.conf
2. Start this server in another terminal
ex: node loadtest.js
3. Open up four websocket connections via pushpin. I just ran wscat in four different terminals.
ex: wscat --connect localhost:17999
4. Watch the terminal for the node load test server. You'll see something like this:
....
published true null null 11
published true null null 10
published true null null 10
published true null null 329 <--- pushpin took 329 ms to respond
published true null null 329
published true null null 330
published true null null 329
published true null null 9
published true null null 9
published true null null 19783 <--- pushpin took 19783 ms to respond
published true null null 19783
published true null null 19783
published true null null 19787
published true null null 3525
published true null null 3522
published true null null 3522
published true null null 3524
....
*/
const http = require('http');
const pubcontrol = require('pubcontrol');
const grip = require('grip');
const PORT = 8080;
const PUSHPINLOCAL = 'http://localhost:5561';
const TIME_LIMIT = 100; // print if pushpin took more than 100 ms
var channelId = 0;
http.createServer(function (req, res) {
// Validate the Grip-Sig header:
// if (!grip.validateSig(req.headers['grip-sig'], 'changeme')) {
// return;
// }
// Set the headers required by the GRIP proxy:
res.writeHead(200, {
'Sec-WebSocket-Extensions': 'grip; message-prefix=""',
'Set-Meta-ClientType': 'ios-sdk',
'Content-Type': 'application/websocket-events'});
var body = '';
req.on('data', function (chunk) {
body += chunk;
});
req.on('end', function() {
var inEvents = grip.decodeWebSocketEvents(body);
if (inEvents[0].getType() == 'OPEN') {
// Open the WebSocket and subscribe it to a channel:
var outEvents = [];
outEvents.push(new grip.WebSocketEvent('OPEN'));
outEvents.push(
new grip.WebSocketEvent(
'TEXT', 'c:' +
grip.webSocketControlMessage(
'subscribe',
{'channel': channelId.toString()}
)
)
);
const out = grip.encodeWebSocketEvents(outEvents);
res.end(out);
// Wait and then publish a message to the subscribed channel:
var i = 0;
const p = function(id) {
//console.log('publishing: ' + i);
var grippub = new grip.GripPubControl({
'control_uri': PUSHPINLOCAL});
var start = new Date().getTime();
grippub.publish(
id.toString(),
new pubcontrol.Item(new grip.WebSocketMessageFormat('Channe:' + id + '::Test::' + i)),
(success, message, context) => {
var taken = (new Date().getTime()) - start;
if (taken > TIME_LIMIT) {
console.log('published', success, message, context, taken);
}
if (i < 20000) {
setTimeout(() => {p(id)}, 1);
} else {
console.log('done');
}
});
i++ ;
};
p(channelId);
channelId++ ;
}
});
}).listen(PORT, '0.0.0.0');
console.log('Server running...');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment