Last active
December 26, 2015 09:49
-
-
Save abonhomme/7132858 to your computer and use it in GitHub Desktop.
Adds a function to the MongoDB shell context that prints out a table showing info for each collection: storage size, storage efficiency%, size of all indexes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
This allows you print some stats about the storage efficiency of | |
your collections in MongoDB. | |
Example: | |
> printStorageStats() | |
Thu Oct 31 2013 14:07:36 GMT-0400 (EDT) | |
Collection | *Size | Effcy% | Idxs | |
------------------------------------------------- | |
test.people | 8.2 kB | 0.63 | 16.4 kB | |
test.system.indexes | 4.1 kB | 3.71 | 0 B | |
------------------------------------------------- | |
| 12.3 kB | | 16.4 kB | |
Add the "printStorageStats()" function to your MongoDB shell | |
context by saving this to disk, then either: | |
-- place 'load("printStorageStats.js")' in your ~/.mongorc.js file | |
-- execute 'load("printStorageStats.js")' within your mongo shell | |
Once the function has been added to your shell, you can type | |
"printStorageStats()" to see your stats. | |
Passing the value "true" to the function will cause the table to | |
be sorted by storage efficiency. | |
*/ | |
function printStorageStats(sortByEffcy) { | |
var pad, humanFileSize, stats, hdr_labels, colLengths, i, ds_size, idx_size, line, hbar; | |
print(new Date()); | |
pad = function (input, length, padding, right) { | |
//from: http://www.sitepoint.com/a-utility-function-fo-padding-strings-and-numbers/ | |
while ((input = input.toString()).length + (padding = padding.toString()).length < length) { | |
padding += padding; | |
} | |
if (!right) { | |
return padding.substr(0, length - input.length) + input; | |
} | |
return input + padding.substr(0, length - input.length); | |
}; | |
humanFileSize = function (bytes, no_si) { | |
//from: http://stackoverflow.com/questions/10420352/converting-file-size-in-bytes-to-human-readable/10420404#10420404 | |
var si, thresh, units, u; | |
si = !no_si; | |
thresh = si ? 1000 : 1024; | |
if (bytes < thresh) { | |
return bytes + ' B'; | |
} | |
units = si ? ['kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'] : ['KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB']; | |
u = -1; | |
do { | |
bytes /= thresh; | |
u++; | |
} while (bytes >= thresh); | |
return bytes.toFixed(1) + ' ' + units[u]; | |
}; | |
stats = generateStorageStats(db, sortByEffcy); | |
hdr_labels = ["Collection", "Size", "Effcy%", "Idxs"]; | |
colLengths = []; | |
for (i = 0; i < hdr_labels.length; i++) { | |
colLengths.push(hdr_labels[i].length + 1); | |
} | |
ds_size = 0; | |
idx_size = 0; | |
stats.forEach(function (c) { | |
c.humanStorageSize = humanFileSize(c.storageSize); | |
c.humanIndexSize = humanFileSize(c.totalIndexSize); | |
colLengths[0] = Math.max(colLengths[0], c.ns.length); | |
colLengths[1] = Math.max(colLengths[1], c.humanStorageSize.length); | |
colLengths[3] = Math.max(colLengths[3], c.humanStorageSize.length); | |
ds_size += c.storageSize; | |
idx_size += c.totalIndexSize; | |
}); | |
ds_size = humanFileSize(ds_size); | |
idx_size = humanFileSize(idx_size); | |
colLengths[1] = Math.max(colLengths[1], ds_size.length); | |
colLengths[3] = Math.max(colLengths[3], idx_size.length); | |
line = [pad(hdr_labels[0], colLengths[0], " ", true), | |
pad((sortByEffcy ? "" : "*") + hdr_labels[1], colLengths[1], " "), | |
pad((sortByEffcy ? "*" : "") + hdr_labels[2], colLengths[2], " "), | |
pad(hdr_labels[3], colLengths[3], " ") | |
]; | |
line = line.join(" | "); | |
print(line); | |
hbar = pad("", line.length, "-"); | |
print(hbar); | |
stats.forEach(function (c) { | |
var lline = [pad(c.ns, colLengths[0], " ", true), | |
pad(c.humanStorageSize, colLengths[1], " "), | |
pad((c.storageEfficiency * 100).toFixed(2), colLengths[2], " "), | |
pad(c.humanIndexSize, colLengths[3], " ") | |
]; | |
print(lline.join(" | ")); | |
}); | |
print(hbar); | |
line = [pad("", colLengths[0], " ", true), | |
pad(ds_size, colLengths[1], " "), | |
pad("", colLengths[2], " "), | |
pad(idx_size, colLengths[3], " ") | |
]; | |
line = line.join(" | "); | |
print(line); | |
} | |
/* | |
Gets the normal stat objects and adds a '.storageEfficiency' property to each one. | |
Returns a sorted list (defaults to 'by size') of stat objects. | |
*/ | |
function generateStorageStats(the_db, sortByEffcy) { | |
var colNames, allStats, prop; | |
colNames = the_db.getCollectionNames(); | |
allStats = []; | |
colNames.forEach(function (name) { | |
var o = db[name].stats(); | |
o.storageEfficiency = o.size / o.storageSize; | |
allStats.push(o); | |
}); | |
prop = sortByEffcy ? 'storageEfficiency' : 'size'; | |
allStats.sort(function (a, b) {return a[prop] === b[prop] ? 0 : (a[prop] < b[prop] ? -1 : 1); }); | |
return allStats; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment