Last active
October 9, 2015 16:52
-
-
Save bitifet/8ddaef0e09d1bb4905d8 to your computer and use it in GitHub Desktop.
Enhanced plv8 (debugging) object to easily examine and measure query execution time.
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
var _debug = true;var pg = (function(_debug){ // Our enhanced plv8 object.//{{{ | |
if (! _debug) return { | |
prepare: plv8.prepare, | |
log: function(){}, | |
}; | |
function gett() { | |
return parseInt(plv8.execute("select EXTRACT(EPOCH FROM (clock_timestamp()))*100000 as t")[0].t); | |
}; | |
function log () {//{{{ | |
var args = Array.prototype.slice.call(arguments, 0).map(function(arg){ | |
return (arg instanceof Object) | |
? JSON.stringify(arg) | |
: arg | |
; | |
}); | |
plv8.elog.apply(this, [NOTICE].concat(args)); | |
};//}}} | |
function executePlan (data) {//{{{ | |
log ("Executing plan..."); | |
log ("Input data: ", data); | |
log ("SQL: ", this.sql); | |
var t0 = gett(); | |
var r = this.plan.execute(data); | |
log ("Total time: " + (gett() - t0)); | |
return r; | |
};//}}} | |
return { | |
prepare: function prepare(sql) { | |
var self = { | |
sql: sql, | |
plan: plv8.prepare.apply(plv8, arguments), | |
}; | |
return { | |
execute: function () { | |
return executePlan.apply(self, arguments); | |
}, | |
}; | |
}, | |
log: log, | |
}; | |
})(_debug);//}}} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment