[英]node.js timing: process.hrtime() vs new Date().getTime()?
[英]node.js process.hrtime in asynchronous server?
我使用http.createServer(onRequest)
創建一個http服務器,並希望測量做出響應所需的時間。
當前,在我的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()
我擔心當一堆請求立即到來時這是否可以正常工作,還是異步會破壞它/混淆時間?
該變量將在onRequest
處理函數的關閉范圍內,因此它將按您期望的方式工作(假定process.hrtime
您的要求)。
你應該做些類似的事情,
function measureRequestTime (req, res, next) {
var start = process.hrtime();
response.on('end', function () {
// logging the time here..
});
}
// app init
app.use(meastureTime);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.