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]

  • doesn't work on Mac OSX
  • seems involved
  • displays way too detailed information for my needs
  • 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

    上一篇: 如何从Node.js服务器命令行执行代码?

    下一篇: 检测需要很长时间的node.js http.get调用