Skip to content

Instantly share code, notes, and snippets.

@suzmas
Created January 30, 2020 19:20
Show Gist options
  • Save suzmas/9206cf14f3d2384a8e709fe97b371417 to your computer and use it in GitHub Desktop.
Save suzmas/9206cf14f3d2384a8e709fe97b371417 to your computer and use it in GitHub Desktop.
logging stuff for meteor
////////////////////////////////////////////////////////////////
////////////////// -- Log DDP messages -- /////////////////////
///////////////////////////////////////////////////////////////
if (Meteor.isClient) {
// log sent messages
var _send = Meteor.connection._send;
Meteor.connection._send = function (obj) {
console.log("send", obj);
_send.call(this, obj);
};
// log received messages
Meteor.connection._stream.on('message', function (message) {
console.log("receive", JSON.parse(message));
});
}
////////////////////////////////////////////////////////////////
//////////// -- Store log of active/inactive subs -- //////////
///////////////////////////////////////////////////////////////
let subs = {};
if (Meteor.isClient) {
// log & store sent messages
var _send = Meteor.connection._send;
Meteor.connection._send = function(obj) {
if (obj.msg === "sub") {
const id = obj.id;
subs[id] = { name: obj.name, status: "active" };
} else if (obj.msg === "unsub") {
const id = obj.id;
subs[id] = { name: obj.name, status: "inactive" };
}
_send.call(this, obj);
};
}
////////////////////////////////////////////////////////////////////////////////////////
// -- Log Tracker comp counts & store log of active computations and total recalcs -- //
////////////////////////////////////////////////////////////////////////////////////////
let computations = {};
let totalRecalcs = 0;
Tracker.onInvalidate = function(f) {
if (!Tracker.active) { throw new Error("Tracker.onInvalidate requires a currentComputation"); }
const currentComp = Tracker.currentComputation;
const currentCompRuns = computations[currentComp._id] || 0;
computations[currentComp._id] = currentCompRuns + 1;
totalRecalcs += 1;
// log # of times this computation has re-run
console.count(currentComp._id);
Tracker.currentComputation.onInvalidate(f);
};
//////////////////////////////////////////////////////////////////////////
// -- Log DDP bytes received & store total in totalBytesReceived var -- //
//////////////////////////////////////////////////////////////////////////
function formatBytes(bytes, decimals = 2) {
if (bytes === 0) return '0 Bytes';
const k = 1024;
const dm = decimals < 0 ? 0 : decimals;
const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
const i = Math.floor(Math.log(bytes) / Math.log(k));
return parseFloat((bytes / Math.pow(k, i)).toFixed(dm)) + ' ' + sizes[i];
}
function memorySizeOf(obj) {
let bytes = 0;
if (obj !== null && obj !== undefined) {
switch (typeof obj) {
case 'number':
bytes += 8;
break;
case 'string':
bytes += obj.length * 2;
break;
case 'boolean':
bytes += 4;
break;
case 'object':
const objClass = Object.prototype.toString.call(obj).slice(8, -1);
if (objClass === 'Object' || objClass === 'Array') {
for (let key in obj) {
if (!obj.hasOwnProperty(key)) continue;
bytes += memorySizeOf(obj[key]);
}
} else bytes += obj.toString().length * 2;
break;
}
}
return bytes;
}
let totalBytesReceived = 0;
if (Meteor.isClient) {
// tally bytes of received DDP messages and log formatted sizes
Meteor.connection._stream.on('message', function(message) {
const messageBytes = memorySizeOf(message);
totalBytesReceived += messageBytes;
console.log('Receive', JSON.parse(message));
console.log(`Message size received: ${formatBytes(messageBytes)}`);
console.log(`Total bytes received: ${formatBytes(totalBytesReceived)}`);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment