开发者

Is there a way to reliably time a SQL query in Node.js or any other type of function?

I'm trying to wrap my head around aysnc functions in node. It there a good way to time how long a DB query takes to run via Node.js.

For the applied usage is to time how long a set of algorithms takes to run. I take the input from one and pipe it into another. Tweaks to the first set could output more or less data. so measuring the response of the overa开发者_Go百科ll timing might not be too accurate. I'm trying to measure the run times of each function.

any pointers* about the theory would appreciated trying to learn the concept I think is important!.


In a sync context, you'd do the following :

 var time = function(fn){
   var start = Date.now();
   fn();
   return start - Date.now()
 }
 // which would be called this way : 
 var elapsed = time(function(){
   // do something
 }
 // do something with elapsed
 // do something else

The async version is :

 var time = function(fn, cb){
    var start = Date.now();
    fn(null, function(){
      var args = [].slice.call(arguments);
      args.splice(1, 0, Date.now() - start);
      cb.apply(this,args); 
    })
 }
 // which you would use this way: 
 time(function(err, cb){
   // do something
   cb(/* params */);
 }, function(err, elapsed /*, params */){
   // do something with elapsed
   // do something else
 });

Of course, you could merge the two functions together, but this gives some useful genericity (you could use it with any function).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜