2013-08-26 33 views
0

我使用http.createServer(onRequest)创建了一个http服务器,并且要测量做出响应所需的时间。node.js process.hrtime在异步服务器?

目前,我onRequest处理我做的:

var start = process.hrtime(); 

response.on('end', function(){ 
    console.log('time to respond', process.hrtime(start)); 
}); 

// call various asynchronous functions and send them 'response' object. One of them eventually does response.end() 

我担心这是否会正常工作时,一堆请求来瞬间,或将异步打破它/混淆次?

回答

0

该变量将处于您的onRequest处理程序函数的关闭范围内,因此它将按照您期望的方式工作(假设process.hrtime按您的要求执行)。

0

你应该做somethinng一样,

function measureRequestTime (req, res, next) { 
    var start = process.hrtime(); 
    response.on('end', function() { 
    // logging the time here.. 
    }); 
} 

// app init 

app.use(meastureTime);