2013-08-26 32 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);