Skip to content

Instantly share code, notes, and snippets.

@amezhenin
Created December 26, 2013 11:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amezhenin/8132687 to your computer and use it in GitHub Desktop.
Save amezhenin/8132687 to your computer and use it in GitHub Desktop.
This script simplify some profiling tasks in MongoDB. Import it on startup: $ mongo [db_name] --shell profile.js Usage: > profile_hist() > profile_ns() > profile_op() Links to original posts in my blog about profiling in MongoDB [1](http://amezhenin.ru/mongodb/mongodb-profiling-part-1.html) and [2](http://amezhenin.ru/mongodb/mongodb-profiling-p…
function profile_hist(){
res = db.system.profile.aggregate([{$project: {'ms':{'$subtract':['$millis',{$mod:['$millis', 50]}]}}}, {$group:{_id:'$ms', sum:{$sum:1}}}, {$sort:{_id:1}}]);
res['result'].forEach(function(i) { print(i['_id'], '\t',i['sum']); });
}
function profile_ns(){
res = db.system.profile.aggregate([{$group:{_id:'$ns', count:{$sum:1}, avg_ms:{$avg:'$millis'}, min_ms:{$min:'$millis'}, max_ms:{$max:'$millis'}}}])
print('ns min_ms max_ms avg_ms count total_ms')
res['result'].forEach(function(i) {
ns = i['_id'].substr(0,17);
ns = ns + Array(20 - ns.length+1).join(" ")
print(ns,i['min_ms'],'\t\t',i['max_ms'],'\t\t',Math.round(i['avg_ms']),'\t\t',i['count'],'\t\t',Math.round(i['count']*i['avg_ms'])); });
}
function profile_op(){
res = db.system.profile.aggregate([{$group:{_id:'$op', count:{$sum:1}, avg_ms:{$avg:'$millis'}, min_ms:{$min:'$millis'}, max_ms:{$max:'$millis'}}}])
print('op min_ms max_ms avg_ms count total_ms')
res['result'].forEach(function(i) {
ns = i['_id'].substr(0,17);
ns = ns + Array(20 - ns.length+1).join(" ")
print(ns,i['min_ms'],'\t\t',i['max_ms'],'\t\t',Math.round(i['avg_ms']),'\t\t',i['count'],'\t\t',Math.round(i['count']*i['avg_ms'])); });
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment