diff --git a/.gitignore b/.gitignore index 5a451dc..56fe296 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ node_modules/ -*.log \ No newline at end of file +*.log +.idea/ diff --git a/lib/mojang_services.js b/lib/mojang_services.js index adfdf3b..c4272d8 100644 --- a/lib/mojang_services.js +++ b/lib/mojang_services.js @@ -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) { diff --git a/lib/ping.js b/lib/ping.js index 5cf4981..0e390fa 100644 --- a/lib/ping.js +++ b/lib/ping.js @@ -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); diff --git a/lib/profiler.js b/lib/profiler.js new file mode 100644 index 0000000..5aafca9 --- /dev/null +++ b/lib/profiler.js @@ -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(); \ No newline at end of file