Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

nodejs request library, get the response time

Tags:

node.js

Using the nodejs request library: 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.

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

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.

var request = require('request');
var start = new Date();
request('http://example.com', function (error, response, body) {
   ...
   var responseTime = new Date() - start;
})
like image 636
David Portabella Avatar asked Aug 07 '13 14:08

David Portabella


People also ask

How do I get response time in Express?

express/connect var express = require('express') var responseTime = require('response-time') var app = express() app. use(responseTime()) app. get('/', function (req, res) { res. send('hello, world!

How many requests per second can NodeJS handle?

However, considering that a “Hello World” Node. js server is easily capable of thirty thousand requests per second on that machine that produced these results, 23 requests per second with an average latency exceeding 3 seconds is dismal.


1 Answers

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 request then stopping the timer in the callback:

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
like image 88
James Skinner Avatar answered Sep 19 '22 12:09

James Skinner