Javascript node.js process.hr异步服务器中的时间?
我使用http.createServer(onRequest)创建了一个http服务器,并希望测量做出响应所需的时间 目前,在我的Javascript node.js process.hr异步服务器中的时间?,javascript,node.js,asynchronous,Javascript,Node.js,Asynchronous,我使用http.createServer(onRequest)创建了一个http服务器,并希望测量做出响应所需的时间 目前,在我的onRequest处理程序中,我执行以下操作: var start = process.hrtime(); response.on('end', function (){ console.log('time to respond', process.hrtime(start)); }); // call various asynchronous funct
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);