detecting node.js http.get calls that take long time
A handler that makes multiple http calls to render a page takes a long time to return. I'd like to detect which of the calls take long time to complete. A profiling approach that I've found via googling[1]
I'm using express and Q promises nested several layers deep if it makes any difference
[1] http://blog.nodejs.org/2012/04/25/profiling-node-js/
Use middleware to track the requests and log them if they take too long.
An example would be:
app.use(function(req, res, next) {
var start = new Date;
var maxDuration = 3000; //3 seconds
var url = req.protocol + '://' + req.get('host') + req.originalUrl;
if (res._responseTime) {
return next();
}
res._responseTime = true;
res.on('header', function() {
var duration = new Date - start;
if (duration > maxDuration) {
//this request took longer than 3 seconds, log it.
log('This request took longer than 3 seconds. URL: ' + url + ' Time: ' + duration + 'ms'); //Pseudo code
}
});
next();
});
链接地址: http://www.djcxy.com/p/52636.html