Use a basic time tracker instead of Date manipulation

This commit is contained in:
Cryptkeeper 2015-11-03 01:40:09 -06:00
parent fcff7a0fec
commit 3373ebc9ee
4 changed files with 49 additions and 14 deletions

1
.gitignore vendored

@ -1,2 +1,3 @@
node_modules/
*.log
.idea/

@ -1,6 +1,7 @@
var request = require('request');
var logger = require('./logger');
var profiler = require('./profiler');
var serviceNameLookup = {
'minecraft.net': 'Website',
@ -24,7 +25,7 @@ function updateService(name, status) {
// If it's an outage, track when it started.
if (status === 'yellow'|| status === 'red') {
newEntry.startTime = (new Date).getTime();
newEntry.startTime = profiler.getCurrentTimeMs();
}
// Generate a nice title from the color.
@ -55,15 +56,15 @@ exports.update = function(timeout) {
try {
body = JSON.parse(body);
for (var i = 0; i < body.length; i++) {
var service = body[i];
var name = Object.keys(service)[0]; // Because they return an array of object, we have to do this :(
for (var i = 0; i < body.length; i++) {
var service = body[i];
var name = Object.keys(service)[0]; // Because they return an array of object, we have to do this :(
// If it's not in the lookup, we don't care about it.
if (name in serviceNameLookup) {
updateService(name, service[name]);
}
}
// If it's not in the lookup, we don't care about it.
if (name in serviceNameLookup) {
updateService(name, service[name]);
}
}
logger.log('debug', 'Updated Mojang services: %s', JSON.stringify(serviceStates));
} catch(err) {

@ -1,9 +1,11 @@
var mcpe_ping = require('mcpe-ping');
var mcpc_ping = require('mc-ping-updated');
var profiler = require('./profiler');
// This is a wrapper function for mc-ping-updated, mainly used to convert the data structure of the result.
function pingMinecraftPC(host, port, timeout, callback) {
var milliseconds = (new Date).getTime();
profiler.track(host);
// Try catch incase the down stream module is bad at handling exceptions.
try {
@ -18,7 +20,7 @@ function pingMinecraftPC(host, port, timeout, callback) {
max: res.players.max
},
version: res.version.protocol,
latency: (new Date).getTime() - milliseconds
latency: profiler.untrack(host)
});
}
}, timeout);
@ -29,7 +31,7 @@ function pingMinecraftPC(host, port, timeout, callback) {
// This is a wrapper function for mcpe-ping, mainly used to convert the data structure of the result.
function pingMinecraftPE(host, port, timeout, callback) {
var milliseconds = (new Date).getTime();
profiler.track(host);
// Try catch incase the down stream module is bad at handling exceptions.
try {
@ -44,7 +46,7 @@ function pingMinecraftPE(host, port, timeout, callback) {
max: res.maxPlayers
},
version: res.version,
latency: (new Date).getTime() - milliseconds
latency: profiler.untrack(host)
});
}
}, timeout);

31
lib/profiler.js Normal file

@ -0,0 +1,31 @@
var logger = require('./logger');
var timestamps = {};
function getCurrentTimeMs() {
return (new Date).getTime();
};
exports.track = function(name) {
if (timestamps[name]) {
throw new Error(name + ' is already being profiled!');
}
timestamps[name] = getCurrentTimeMs();
};
exports.untrack = function(name) {
if (!timestamps[name]) {
throw new Error(name + ' isn\'t being profiled!');
}
var timestamp = getCurrentTimeMs() - timestamps[name];
delete timestamps[name];
logger.log('debug', name + ' took ' + timestamp + 'ms');
return timestamp;
};
exports.getCurrentTimeMs = getCurrentTimeMs();