【问题标题】:detecting node.js http.get calls that take long time检测需要很长时间的 node.js http.get 调用
【发布时间】:2014-09-04 04:33:22
【问题描述】:

进行多次 http 调用以呈现页面的处理程序需要很长时间才能返回。我想检测哪些调用需要很长时间才能完成。我通过谷歌搜索发现的一种分析方法[1]

  1. 不适用于 Mac OSX
  2. 似乎涉及
  3. 显示的信息过于详细,无法满足我的需要

如果有什么不同的话,我正在使用 express 和 Q 承诺嵌套几层

[1]http://blog.nodejs.org/2012/04/25/profiling-node-js/

【问题讨论】:

  • 我想你可以使用 connect responseTime 中间件,而不是添加标题,如果超过 x 毫秒,则记录所花费的时间。您将能够从 request 参数中获取 URL。
  • 如果您将其作为答案并包含示例,我会将其作为可接受的答案

标签: javascript node.js macos profiling


【解决方案1】:

使用中间件跟踪请求并在请求时间过长时记录它们。

一个例子是:

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();
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-09-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多