【问题标题】:How to get response time of each proxy-request done by node-http-proxy?如何获取 node-http-proxy 完成的每个代理请求的响应时间?
【发布时间】:2014-01-23 17:44:24
【问题描述】:

我想计算每个代理请求的响应时间,由 node-http-proxy 完成,如下所示:

var httpProxy = require('http-proxy');
var proxy = httpProxy.createProxyServer();

require('http').createServer(function(req, res) {
   // start-time for one request
   proxy.web(req, res, { target: 'localhost:80' });
}).listen(3000);

proxy.on('proxyRes', function (res) {
  // end-time for one request to calculate the time
  console.log('RAW Response from the target', JSON.stringify(res.headers, true, 2));
});

所以我想得到开始时间和开始时间之间的时间差。我能够捕捉到proxyRes 事件,但是如何将代理响应与正确的客户端请求相匹配?

更新:我尝试了以下方法:

var httpProxy = require('http-proxy');
var http = require('http');

var proxy = new httpProxy.createProxyServer({
    target: {
        host: 'localhost',
        port: 80
    }
});

var start_time = 0;
var proxyServer = http.createServer(function (req, res) {


    start_time = new Date().getTime();

    proxy.web(req, res);

    res.on('finish', function() {
        console.log("The request was proxied in " + (new Date().getTime() - start_time) + "ms");
    });
});

proxyServer.listen(3000);

// Proxy-Target server
var n = 0; sleep = 0;
http.createServer(function (req, res) {

    n++;

    var start_time = new Date().getTime();

    res.writeHead(200, {'Content-Type': 'text/plain'});

    if (n % 2 == 0){
        sleep = 2000;
    } else {
        sleep = 5000;
    }

    setTimeout(function(){
        res.end("...");
    }, sleep);

    res.on('finish', function(d) {
        var diff_time = new Date().getTime() - start_time;
        console.log("The request was processed in " + (new Date().getTime() - start_time) + "ms");
    });

}).listen(80);

因此,目标服务器的时间测量正确,因为一个请求可能持续 2 或 5 秒,但代理有时测量的响应时间仅为几毫秒。 我通过使用 apache-bench 进行了尝试,例如:ab -n 10 -c 3 http://localhost:3000/ 和控制台的结果:

The request was processed in 2007ms
The request was proxied in 2017ms
The request was processed in 2003ms
The request was proxied in 2006ms
The request was processed in 5002ms
The request was processed in 5001ms
The request was proxied in 980ms
The request was proxied in 981ms
The request was processed in 2002ms
The request was proxied in 2006ms
The request was processed in 2002ms
The request was proxied in 2005ms
The request was processed in 5000ms
The request was proxied in 8ms
The request was processed in 5000ms
The request was proxied in 982ms
The request was processed in 2001ms
The request was proxied in 2005ms
The request was processed in 5002ms
The request was proxied in 4998ms

【问题讨论】:

  • 您可以使用new Date().getTime() 并记录差异吗?
  • 我不这么认为,因为代理异步工作,所以我必须将代理请求与其相关响应相匹配。因此我必须以某种方式确定一个请求。
  • 脚本异步运行,但代理看起来不像。您应该能够将两者都放入回调中(在收到响应后)并以这种方式进行测量。
  • 你能给我更多关于你的想法的信息吗?我不确定,但我认为proxy.web() 函数是异步的,最终会发出像proxyRes 这样的事件来监听。据我了解,只有异步。函数正在发出事件,还是我错了?
  • 写在一个答案中。看看有没有效果?

标签: javascript node.js proxy node-http-proxy


【解决方案1】:

现在可以了,在 nodejs google-gloup 上得到了关键提示,start_time 的 cope 没有在本地设置,所以这里是工作代码:

var httpProxy = require('http-proxy');
var http = require('http');

var proxy = new httpProxy.createProxyServer({});

// before: var start_time = 0;
var proxyServer = http.createServer(function (req, res) {

    // now
    var start_time = new Date().getTime();

    proxy.web(req, res, { target: 'http://localhost:80' });

    res.on('finish', function() {
       console.log("The request was proxied in " + (new Date().getTime() - start_time) + "ms");
    });
});
proxyServer.listen(3000);

【讨论】:

    【解决方案2】:

    (未经测试,但让我们看看这是否有效)

    var httpProxy = require('http-proxy');
    var proxy = httpProxy.createProxyServer();
    var startTime = 0;
    var endTime = 0;
    
    require('http').createServer(function(req, res) {
       // start-time for one request
       startTime = new Date().getTime();
       proxy.web(req, res, { 
            target: 'localhost:80' 
       });
    }).listen(3000);
    
    proxy.on('proxyRes', function (res) {
      // end-time for one request to calculate the time
      endTime = new Date().getTime();
      console.log(endTime - startTime);
      console.log('RAW Response from the target', JSON.stringify(res.headers, true, 2));
    });
    

    【讨论】:

    • 到目前为止,谢谢,但是使用此代码,每个新的客户端连接都会导致将 startTime 设置为当前时间。因此,当一个早期请求的处理时间比后一个请求的处理时间长时,第一个请求的 startTime 不再正确。我尝试通过在proxy.web() 之前添加console.out("startTime");console.out("endTime");proxy.on(...); 中添加console.out("startTime");,输出顺序必须在任何时候都相同,但它不是,所以代理正在异步工作。
    猜你喜欢
    • 1970-01-01
    • 2013-04-29
    • 1970-01-01
    • 2014-05-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多