Created
December 10, 2013 00:06
-
-
Save junosuarez/7883441 to your computer and use it in GitHub Desktop.
example profiling from minq < 1.0.0
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
// rudimentary db query profiling | |
var profile | |
if (!config.profiledb) { | |
profile = function (x) { return x } | |
} else { | |
var totalQueries = 0 | |
var profileLog = require('fs').createWriteStream('./dbprofile.log') | |
var path = require('path') | |
var base = path.resolve(path.join(__dirname, '..')) | |
function blackbox(stacktrace) { | |
try { | |
return stacktrace.split('\n') | |
.slice(2) | |
.filter(function (frame) { | |
return frame.indexOf('node_modules') < 0 | |
&& !frame.match(/ \(\w+\.js:.+\)$/) | |
}) | |
.map(function (frame) { | |
return frame.replace(base, '') | |
}) | |
.join('\n') | |
} catch (e) { | |
return stacktrace.split('\n')[2] | |
} | |
} | |
profile = function (query) { | |
var start = Date.now() | |
var stack | |
function report(results) { | |
totalQueries++ | |
var end = Date.now() | |
profileLog.write(JSON.stringify({ | |
n: totalQueries, | |
time: end-start, | |
projectionUsed: Object.keys(query._.projection || {}).length > 0, | |
resultLength: results.length, | |
resultSize: results.map(function (x) { return JSON.stringify(x) }).join('').length, | |
collection: query._.collection, | |
query: query._.query, | |
at: blackbox(stack) | |
}) + '\n') | |
} | |
query.one = function () { | |
var promise = query.__proto__.one.call(query) | |
stack = new Error().stack | |
promise.then(function (result) { | |
report(result ? [result] : []) | |
}) | |
return promise | |
} | |
query.toArray = function () { | |
var promise = query.__proto__.toArray.call(query) | |
stack = new Error().stack | |
promise.then(report) | |
return promise | |
} | |
return query | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment