Skip to content

Instantly share code, notes, and snippets.

@jrgm
Created January 31, 2013 06:57
Show Gist options
  • Save jrgm/4680891 to your computer and use it in GitHub Desktop.
Save jrgm/4680891 to your computer and use it in GitHub Desktop.
Measure time and responses
// Measure time and responses
// usage: while [ 1 ]; do node sc.js; sleep 5; done
// or: while [ 1 ]; do SC_HEARTBEAT=1 node sc.js; sleep 5; done
var http = require('http');
var path, heartbeat;
if (process.env.SC_HEARTBEAT) {
heartbeat = true;
path = '/__heartbeat__';
} else {
path = '/wsapi/session_context';
}
var options = {
hostname: '127.0.0.1',
port: 63300, // router daemon
path: path
};
function get_payload(data) {
if (heartbeat) return data === 'ok';
try {
info = JSON.parse(data);
} catch(e) {
console.log(new Date().toISOString(), 'exception', e);
}
return info;
}
function session_context() {
var start = Date.now();
var req = http.get(options, function(res) {
var elapsed = Date.now() - start;
var data = '';
res.setEncoding('utf8');
res.on('data', function(chunk) {
data += chunk;
});
res.on('end', function() {
var info = get_payload(data);
var parseable = !!info;
var ok = (res.statusCode === 200 && parseable) ? 'PASS' : 'FAIL';
console.log(new Date().toISOString(), 'status:', ok, res.statusCode, elapsed + 'ms');
});
});
req.on('error', function(e) {
var elapsed = Date.now() - start;
console.log(new Date().toISOString(), 'error:', e.message, elapsed + 'ms');
});
}
session_context();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment