简体   繁体   English

如何获取node-http-proxy完成的每个代理请求的响应时间?

[英]How to get response time of each proxy-request done by node-http-proxy?

i want to compute the response-time for each proxy-request, done by node-http-proxy, like this: 我想计算每个代理请求的响应时间,由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));
});

so i want to get the time-difference between the start-time and the ent-time. 所以我想得到开始时间和实时时间之间的时差。 i am be able to catch the proxyRes event, but how to match the proxy-response to the right client-request? 我能够捕获proxyRes事件,但如何将代理响应与正确的客户端请求相匹配?

update: i tried the following: 更新:我尝试了以下内容:

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

so, the time of the target-server is measured correctly, cause one request could last either 2 oder 5 seconds, but the proxy measures sometimes response-times of only some milli-seconds. 因此,正确测量目标服务器的时间,导致一个请求可能持续2或5秒,但代理测量有时响应时间仅为几毫秒。 I tried it by using apache-bench like: ab -n 10 -c 3 http://localhost:3000/ and the result of the console: 我尝试使用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

now it works, got the key hint on the nodejs google-gloup, the cope of start_time was not set locally, so here is the working code: 现在它的工作原理,得到了nodejs google-gloup的关键提示, start_time的应对没有在本地设置,所以这里是工作代码:

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

(untested, but let's see if this works) (未经测试,但让我们看看这是否有效)

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM