Last active
August 29, 2015 14:01
-
-
Save jayjanssen/394c18e64fdd31e6cab7 to your computer and use it in GitHub Desktop.
Percona Training scoreboard -- node.js
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
// To Install: (requires EPEL) | |
// yum install nodejs nodejs-async -y | |
// To start: | |
// export NODE_PATH=/usr/lib/node_modules/ | |
// screen node scoreboard.js | |
var http = require('http'); | |
var async = require('async'); | |
var Team = function( name, ip ) | |
{ | |
var that = {}; | |
that.name = name; | |
that.ip = ip; | |
that.homepageResult = 0; | |
that.searchResult = 0; | |
that.movieResult = 0; | |
that.gather_data = function(cb) | |
{ | |
// async.series([ | |
async.parallel([ | |
function(callback) | |
{ | |
var start = new Date(); | |
var req = http.get('http://' + that.ip + '/my-movies/', function(res) { | |
that.homepageResult = new Date() - start; | |
callback(null, 'homepage'); | |
}); | |
req.on('socket', function(socket) { | |
socket.setTimeout(30000); | |
socket.on('timeout', function() { | |
req.abort(); | |
}); | |
}); | |
}, | |
function(callback) | |
{ | |
var start = new Date(); | |
var req = http.get('http://' + that.ip + '/my-movies/search.php?search_type=movies&q=' + genRandString(), function(res) { | |
that.searchResult = new Date() - start; | |
callback(null, 'searchpage'); | |
}); | |
req.on('socket', function(socket) { | |
socket.setTimeout(30000); | |
socket.on('timeout', function() { | |
req.abort(); | |
}); | |
}); | |
}, | |
function(callback) | |
{ | |
var start = new Date(); | |
var req = http.get('http://' + that.ip + '/my-movies/movie.php?id=' + getRandomInt(0, 100000), function(res) { | |
that.movieResult = new Date() - start; | |
callback(null, 'moviepage'); | |
}); | |
req.on('socket', function(socket) { | |
socket.setTimeout(30000); | |
socket.on('timeout', function() { | |
req.abort(); | |
}); | |
}); | |
}, | |
], | |
function(err, results) | |
{ | |
// results is now equal to ['homepage', 'searchpage', 'moviepage'] | |
// on success of each function in the series | |
cb(); | |
}); | |
}; | |
return that; | |
}; | |
teams = Array(); | |
teams.push( Team( "1", "54.211.196.12" )); | |
teams.push( Team( "Robert", "ec2-23-20-224-23.compute-1.amazonaws.com" )); | |
teams.push( Team( "David", "ec2-54-235-59-152.compute-1.amazonaws.com" )); | |
teams.push( Team( "Chris", "ec2-54-226-115-255.compute-1.amazonaws.com" )); | |
function genRandString() | |
{ | |
var length = 20; | |
var chars = "abcdefghijklmnopqrstuvwxyz "; | |
var characters = chars.split(''); | |
var string = ''; | |
for( var i = 0; i < length; i++ ) | |
{ | |
string += characters[Math.floor(Math.random()*characters.length)]; | |
} | |
return string; | |
} | |
function getRandomInt (min, max) | |
{ | |
return Math.floor(Math.random() * (max - min + 1)) + min; | |
} | |
function pretty_time( ms ) | |
{ | |
if( ms < 1000 ) | |
{ | |
return ms + 'ms'; | |
} | |
else | |
{ | |
var seconds = ms / 1000; | |
return seconds.toFixed(2) + 's'; | |
} | |
} | |
function print_bar( time, goal ) | |
{ | |
var output = ''; | |
var color = "#33FF33"; | |
var tablewidth = 250; | |
var w = time; | |
if( time > goal * 2 ) | |
{ | |
w = goal * 2; | |
color = "#FF3333"; | |
} | |
else if( time > goal ) | |
{ | |
color = "#FFFF33"; | |
} | |
var w = Math.round( w * tablewidth / ( goal * 2)); | |
var v = tablewidth - w; | |
return " \ | |
<table border=1 cellpadding=0 cellspacing=0 width='" + tablewidth + "'> \ | |
<tr> \ | |
<td width='" + w + "' style='background-color:" + color + "'> </td> \ | |
<td width='" + v + "'> </td> \ | |
</tr> \ | |
</table> \ | |
"; | |
} | |
http.createServer(function (request, response) | |
{ | |
var start = new Date(); | |
async.each(teams, | |
function( team, cb ) | |
{ | |
// Do this to each item | |
team.gather_data( cb ); | |
}, | |
// When they are all done, we generate the page | |
function(err) | |
{ | |
output = '<html> \ | |
<head> \ | |
<title>Scoreboard</title> \ | |
<style> \ | |
\ | |
body { \ | |
font-family: arial; \ | |
} \ | |
\ | |
#homepage, #search, #other { \ | |
width: 10%; \ | |
border: 1px solid #cccccc; \ | |
float: left; \ | |
padding: 10px; \ | |
margin-left: 5px; \ | |
} \ | |
\ | |
#refreshing { \ | |
float: right; \ | |
font-style: italic; \ | |
} \ | |
</style> \ | |
<meta http-equiv="refresh" content="60"> \ | |
</head> \ | |
<body> \ | |
<table width=100%> \ | |
<tr><th>Team name</th><th>Homepage (Goal: <200ms)</th><th>Search Results (Goal: < 1000ms)</th><th>Any other page (Goal: <500ms)</tr> \ | |
'; | |
for( var i = 0; i < teams.length; i++ ) | |
{ | |
var team = teams[i]; | |
output += "<tr><th>Team " + team.name + "</th>"; | |
output += "<td align=center>" + pretty_time( team.homepage ) + " " + print_bar( team.homepage, 200 ) + "</td>"; | |
output += "<td align=center>" + pretty_time( team.search ) + " " + print_bar( team.search, 1000 ) + "</td>"; | |
output += "<td align=center>" + pretty_time( team.other ) + " " + print_bar( team.other, 500 ) + "</td>"; | |
} | |
output += "</table>"; | |
output += "Page built in " + pretty_time( new Date() - start ) + "<br/>"; | |
output += "Page last updated at" + Date(); | |
output += "</body></html>"; | |
response.writeHead(200, {'Content-Type': 'text/html'}); | |
response.end( output + '\n'); | |
} | |
); | |
}).listen(80); | |
console.log('Server running at http://127.0.0.1:80/');; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment