Skip to content

Instantly share code, notes, and snippets.

@evantahler
Created June 16, 2014 05:42
Show Gist options
  • Save evantahler/dd6d63200262f6e3f64c to your computer and use it in GitHub Desktop.
Save evantahler/dd6d63200262f6e3f64c to your computer and use it in GitHub Desktop.
A websocket load test for actionhero
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<meta http-equiv="content-language" content="en" />
<meta name="description" content="actionhero.js" />
<title>actionhero.js WebSocket Load Test</title>
<style>
.green {background-color: green;}
.red {background-color: red;}
.yellow {background-color: yellow;}
.gray {background-color: gray;}
.connection{
margin: 5px;
border: 2px solid black;
float: left;
}
</style>
<script src="//code.jquery.com/jquery-1.11.0.min.js"></script>
<!-- <script type="text/javascript" src="/faye/client.js"></script> -->
<script type="text/javascript" src="/public/javascript/actionheroClient.js"></script>
<script type="text/javascript">
var running = false;
var numClients = 1000;
var numRequests = 10;
var testAction = 'sleepTest';
var sleep = 1;
var clients = {};
var totalGood = 0;
var totalBad = 0;
var totalRequets = 0;
var totalTime = 0;
var start; var end;
var client = function(id){
var self = this;
self.running = true;
self.id = id;
self.goodCount = 0;
self.badCount = 0;
self.numRequests = 0;
}
client.prototype.connect = function(){
var self = this;
self.client = new actionheroClient();
self.client.on('connected', function(){ console.log('['+self.id+'] ' + 'connected!') })
self.client.on('disconnected', function(){ console.log('['+self.id+'] ' + 'disconnected :(') })
self.client.on('alert', function(message){ alert('['+self.id+'] ' + JSON.stringify(message) ) })
self.client.on('api', function(message){ alert('['+self.id+'] ' + JSON.stringify(message) ) })
self.client.on('welcome', function(message){ console.log('['+self.id+'] ' + JSON.stringify(message) ); })
self.client.on('say', function(message){ console.log('['+self.id+'] ' + JSON.stringify(message) ); })
self.client.connect(function(err, details){
if(err != null){
console.log(err);
}else{
self.work();
}
});
}
client.prototype.work = function(){
var self = this;
$('#id_'+self.id).attr('class', 'yellow');
self.client.action(testAction, function(data){
self.numRequests++;
totalRequets++;
console.log('['+self.id+'] ' + JSON.stringify(data));
if(data.error == null){
self.goodCount++;
totalGood++
}else{
self.badCount++;
totalBad++;
}
$('#good_'+self.id).html(self.goodCount);
$('#bad_'+self.id).html(self.badCount);
if(self.numRequests >= numRequests){
// done!
if(self.badCount == 0){
$('#id_'+self.id).attr('class', 'green');
}else{
$('#id_'+self.id).attr('class', 'red');
}
self.running = false;
self.client.disconnect();
checkState();
}else{
setTimeout(function(){
self.work();
}, sleep)
}
});
}
var checkState = function(){
var i = 0;
while(i < numClients){
var runningCheck = false
if(clients[i].running != false){
runningCheck = true;
break;
}
i++;
}
if(runningCheck === false){
running = false;
end = new Date().getTime();
}
}
var boot = function(){
var i = 0;
start = new Date().getTime();
running = true;
while(i < numClients){
var c = new client(i);
c.connect();
clients[i] = c;
i++;
}
}
</script>
</head>
<body onload="boot()">
Check the console for logs. Be sure to update your ulimts!
<h3>Test State: <span id="testState"></span></h3>
<h3>Connections: <span id="numClients"></span></h3>
<h3>Total Requests: <span id="totalRequets"></span></h3>
<h3>Total Good: <span id="totalGood"></span></h3>
<h3>Total Bad: <span id="totalBad"></span></h3>
<h3>Req/Sec: <span id="reqPerSec"></span></h3>
<h3>% Good: <span id="percentGood"></span></h3>
<div id='boxes' style="float:left;"></div>
<script type="text/javascript">
setInterval(function(){
if(running === true){
delta = new Date().getTime() - start;
}else{
delta = end - start;
}
var reqPerSec = totalRequets / delta * 1000;
if(running === true){ var state = 'running' }
if(running === false){ var state = 'stopped' }
$("#testState").html( state );
$("#numClients").html( numClients );
$("#totalRequets").html( totalRequets );
$("#totalGood").html( totalGood );
$("#totalBad").html( totalBad );
$("#reqPerSec").html( reqPerSec );
$("#percentGood").html( Math.round(totalGood / (totalGood + totalBad) * 100) );
}, 1000)
var rows = '';
var i = 0;
while(i < numClients){
rows += '<div class="connection" id="connection_'+i+'">';
rows += '<div id="id_'+i+'" class="gray">#'+i+'</div>';
rows += '<span id="good_'+i+'" style="color: green;">0</span>';
rows += " | "
rows += '<span id="bad_'+i+'" style="color: red;">0</span>';
rows += '</div>';
i++;
}
$('#boxes').html(rows);
</script>
</body>
</html>
@evantahler
Copy link
Author

Here's what the tool looks like in action

http://i.imgur.com/dpZPHpT.png

@evantahler
Copy link
Author

If you want to test how fast actions can go, use an action like randomNumber. If you want to test how many simultaneous WS connections the server can hold, use an action like sleepTest

@evantahler
Copy link
Author

eh, this gist is out of date. Check the version included within actionhero: https://github.com/evantahler/actionhero/blob/master/public/websocketLoadTest.html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment