簡體   English   中英

異步服務器中的node.js process.hrtime?

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM