nodejs请求库,获取响应时间

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18106825/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 15:13:26  来源:igfitidea点击:

nodejs request library, get the response time

node.js

提问by David Portabella

Using the nodejs requestlibrary: https://github.com/mikeal/request

使用 nodejsrequest库:https: //github.com/mikeal/request

var request = require('request');
request('http://example.com', function (error, response, body) {
   ...
})

Is it possible to get the response time on the callback? The doc mentions response.statusCode. looking at the library source code I also see undocumented response.headers and response.href, but I don't see responseTime or similar.

是否有可能获得回调的响应时间?该文档提到了 response.statusCode。查看库源代码,我还看到未记录的 response.headers 和 response.href,但我没有看到 responseTime 或类似内容。

or, is there an alternative library to requestthat provides the response time?

或者,是否有request提供响应时间的替代库?

ps: I know I could do something like this, but that's not a solution, as I am making many async requests and I cannot control when each request will be started.

ps:我知道我可以做这样的事情,但这不是解决方案,因为我发出了许多异步请求,而且我无法控制每个请求何时开始。

var request = require('request');
var start = new Date();
request('http://example.com', function (error, response, body) {
   ...
   var responseTime = new Date() - start;
})

回答by James Skinner

The request library can do timing for you (docs):

请求库可以为您计时(文档):

request.get({
  url : 'http://example.com',
  time : true
},function(err, response){
  console.log('Request time in ms', response.elapsedTime);
});

As the question implies, you could get issues with the approach of starting a timer, calling requestthen stopping the timer in the callback:

正如问题所暗示的那样,您可能会遇到启动计时器、request在回调中调用然后停止计时器的方法的问题:

var start = new Date();
request.get('http://example.com', function(err, response){
  // NOT GOOD
  console.log('Request time plus 5 seconds', new Date() - start);
});
require('sleep').sleep(5); // time-consuming synchronous action

回答by Aaron Dufour

Something like

就像是

var timedReq = function(url, next) {
  var start = new Date();
  request(url, function(err, res, body) {
    res.responseTime = new Date() - start;
    next(err, res, body);
  });
};

will do it for you. This seems small enough that it's probably easier than finding a new library.

会为你做的。这看起来足够小,可能比找到一个新库更容易。

回答by Alexandru Savin

Instead of using Date() you could also use process.hrtime. I have a library specifically for these kind of cases that uses hrtime. The library name is exectimer.

除了使用 Date(),您还可以使用 process.hrtime。我有一个专门用于使用 hrtime 的情况的库。库名称是exectimer

You could use it like this:

你可以这样使用它:

var timedReq = function(url, next) {
  var tick = new t.Tick("responseTime");
  tick.start();
  request(url, function(err, res, body) {
    tick.stop();
    next(err, res, body);
  });
};

// when ready check the results with this:
var responseTime = t.timers.responseTime;
console.log(responseTime.min()); // minimal response time
console.log(responseTime.max()); // minimal response time
console.log(responseTime.mean()); // mean response time
console.log(responseTime.median()); // median response time