Created
January 16, 2013 07:32
-
-
Save NoUsername/4545292 to your computer and use it in GitHub Desktop.
a simple load tester for websocket server which connects a desired number of clients, sends "client _RANDOM_UNIQUE_ID_" and then in a given interval "ping _RANDOM_NUMBER_"
prints out any messages sent by the server
This file contains 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 | |
// to run install: nodejs and the module "websocket" | |
// npm install websocket | |
// then execute | |
// node tester.js _NUMBER_OF_CLIENTS_ | |
var WebSocketClient = require('websocket').client; | |
var HOST = 'ws://localhost:8449/ws/endpoint'; | |
var PING_TIME_MS = 20000; | |
var DELAY_BETWEEN_TESTERS_MS=100; | |
var append = new Date().getTime().toString(); | |
var PREFIX = append.substring(append.length-8, append.length-1); | |
var counter = 0; | |
function Tester() { | |
this.client = new WebSocketClient(); | |
var clientName = "client_" + PREFIX + "_" + (counter++); | |
this.client.on('connectFailed', function(error) { | |
console.log('Connect Error: ' + error.toString()); | |
}); | |
this.client.on('connect', function(connection) { | |
connection.sendUTF("client " + clientName); | |
console.log('WebSocket client connected'); | |
connection.on('error', function(error) { | |
console.log("Connection Error: " + error.toString()); | |
}); | |
connection.on('close', function() { | |
console.log('echo-protocol Connection Closed'); | |
}); | |
connection.on('message', function(message) { | |
if (message.type === 'utf8') { | |
console.log(new Date().getTime().toString() + " - Received: '" + message.utf8Data + "'"); | |
} | |
}); | |
function sendNumber(connection) { | |
if (connection.connected) { | |
var number = Math.round(Math.random() * 0xFFFFFF); | |
connection.sendUTF("ping " + number.toString()); | |
setTimeout(function(_this) {sendNumber(_this)}, PING_TIME_MS, connection); | |
} | |
} | |
sendNumber(connection); | |
}); | |
} | |
Tester.prototype.go = function() { | |
this.client.connect(HOST); | |
}; | |
var args = process.argv.splice(2); | |
var testerCount = args.length > 0 ? args[0] : 100; | |
for (var i=0; i<testerCount; i++) { | |
setTimeout(function() { | |
new Tester().go(); | |
}, i*DELAY_BETWEEN_TESTERS_MS); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment