Skip to content

Instantly share code, notes, and snippets.

@ndelitski
Created July 23, 2017 23:38
Show Gist options
  • Save ndelitski/0e3671c54f9a8ad7a3b15dbf1e3d4cfe to your computer and use it in GitHub Desktop.
Save ndelitski/0e3671c54f9a8ad7a3b15dbf1e3d4cfe to your computer and use it in GitHub Desktop.
Streaming data performance test client harness (Node.js, Socket.io)
var URI = process.argv[2],
TEST_ID = new Date().getTime(),
SOCKET_INTERVAL = 0,
MAX_SOCKETS = 5000,
WARM_UP_SOCKETS = 10,
MESSAGE_PADDING_SIZE = 2000,
MESSAGE_SIZE_BASE = 41,
ADD_RESULT_DOMAIN = "lonlx10557.nomura.com",
ADD_RESULT_PORT = 80,
ADD_RESULT_PATH = "/streamtest/add_result.php",
ADD_RESULT_USER_AGENT = "node.js/streamtest.js";
var sequence_id = 0,
rtt_total = 0,
rtt_max = 0,
rtt_min = 0,
sockets_sent = 0,
sockets_received = 0,
last_socket_received_timestamp = 0;
var querystring = require('querystring'),
http = require('http'),
io = require('socket.io-proxy'),
os = require('os');
var rtt_data = [];
console.log("Starting a new test with ID: " + TEST_ID);
console.log("Establishing a streaming connection to " + URI + "...");
var socket = io.connect(URI);
process.on('uncaughtException', function(err) {
console.log('Caught exception: ' + err);
console.log(err.stack);
});
socket.on('initial_connect', function (data) {
console.log("Streaming connection established!");
send_socket(socket, sequence_id ++);
});
socket.on('new_response', function (data) {
receive_socket(data);
if (sequence_id < MAX_SOCKETS)
setTimeout(function() { send_socket(socket, sequence_id ++); }, SOCKET_INTERVAL);
else
{
console.log("Test complete. Test ID: " + TEST_ID);
process.exit(code = 0);
}
});
function send_socket(socket, sequence_id)
{
socket.emit('connect', {
message_padding_size: MESSAGE_PADDING_SIZE,
sequence_id: sequence_id,
timestamp_start: new Date().getTime()
});
sockets_sent ++;
console.log("Sending socket for test " + TEST_ID + ": " + sockets_sent + " / " + MAX_SOCKETS);
}
function receive_socket(data)
{
var timestamp_current = new Date().getTime();
last_socket_received_timestamp = timestamp_current;
sockets_received ++;
if (sockets_received > WARM_UP_SOCKETS)
{
var message_size = MESSAGE_SIZE_BASE + MESSAGE_PADDING_SIZE;
var timestamp_start = data["timestamp_start"];
var timestamp_end = timestamp_current;
var rtt_current = timestamp_end - timestamp_start;
rtt_total += rtt_current;
// Calculate min, max and average RTT
rtt_min = (rtt_current < rtt_min || rtt_min === 0 ) ? rtt_current : rtt_min;
rtt_max = rtt_current > rtt_max ? rtt_current : rtt_max;
var rtt_avg = parseFloat(rtt_total / (sockets_received - WARM_UP_SOCKETS)).toFixed(2);
// Calculate median RTT
rtt_data.push(rtt_current);
var rtt_med = parseFloat(median(rtt_data)).toFixed(2);
console.log(
"Sockets received: " + sockets_received +
" -- Message size: " + message_size + " bytes" +
" -- Current RTT: " + rtt_current + "ms" +
" -- Min. RTT: " + rtt_min + "ms" +
" -- Max. RTT: " + rtt_max + "ms" +
" -- Avg. RTT: " + rtt_avg + "ms" +
" -- Med. RTT: " + rtt_med
);
postResult();
}
else
console.log("Warm up socket " + sockets_sent + " / " + WARM_UP_SOCKETS + " - RTT data ignored");
function postResult()
{
var post_data = querystring.stringify({
'host': os.hostname(),
'message_size': message_size,
'round_trip_time': rtt_current,
'test_id': TEST_ID,
'test_type': 'streaming',
'timestamp_start': timestamp_start,
'timestamp_end': timestamp_end,
'uri': URI
});
var post_options = {
host: ADD_RESULT_DOMAIN,
port: ADD_RESULT_PORT,
path: ADD_RESULT_PATH,
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': post_data.length,
'user-agent': ADD_RESULT_USER_AGENT
}
};
// Build the request
var post_req = http.request(post_options);
// Write parameters to post body
post_req.write(post_data);
post_req.end();
}
}
function median(values) {
values.sort( function(a,b) {return a - b;} );
var half = Math.floor(values.length/2);
if(values.length % 2)
return values[half];
else
return (values[half-1] + values[half]) / 2.0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment