How to measure Sails.js requests / response time

I would like to see in the console the time needed to respond to the HTTP request. A kind of like express.js does.

GET api/myurl/ 210ms 200

I am running sails debug , but it shows little.
I have a node inspector working for me, but it seems to allow me to check JavaScript objects at runtime, but not for this specific thing.
Is there a configuration in Sails that I can enable, or an NPM module that I can install to find out this time between the request and the response?

+7
source share
5 answers

If you want to measure the overall response time (including creating views and passing data to the socket), you can use req._startTime defined in the startRequestTimer middleware, or use response-time , which gives much more accurate results.

This middleware adds an X-Response-Time header to all HTTP responses, so you can check it both on the client side and on the server side.

 module.exports.http = { middleware: { order: [ 'responseTimeLogger', // ... ], // Using built-in variable defined by Sails responseTimeLogger: function (req, res, next) { req.on("end", function() { sails.log.info('response time: ' + new Date() - req._startTime + 'ms'); }); next(); }, // Using response-time middleware responseTimeLogger: function (req, res, next) { req.on("end", function() { sails.log.info('response time: ' + res.getHeader('X-Response-Time')); }); require('response-time')()(req, res, next); } } } 
+12
source share

@abeja the answer is great, but a bit outdated. If someone is still looking for an answer, I put the latest version below:

 // Using response-time middleware responseTimeLogger: function(req, res, next) { res.on("finish", function() { sails.log.info("Requested :: ", req.method, req.url, res.get('X-Response-Time')); }); require('response-time')()(req, res, next); } 

A little explanation:

  • Listen to the finish event of the res object, because when the req emit end event occurs, the res header is not set yet.
  • Recent Node.js are changing the getHeader API to get .

btw, my reputation is 0, so I can not leave a comment on @abeja's answer :)

+6
source share

In addition to @abeja's answer (a very good answer, though), if you are looking for monitoring a production application, you might be interested in a New Relic .

The tool helps you measure (among other things) response time, throughput, error rate, etc.

New relic transactions

It also measures database performance (in my case it's MongoDB) and helps you easily explore bottlenecks.

enter image description here

In addition, it allows you to measure application performance from a browser (including connection time, DOM rendering time, etc.).

The new Relic setup is very simple - a few steps to run your statistics (more details here: Installing and supporting Node.js ). For development purposes, you may also have a free account with 24-hour data storage.

I hope this also helps.

+3
source share

For a more general solution for statistics (which can also be used in your Sails.js project), there is a large series of digitalocean tutorials on using StatD with Graphite to measure anything (and everything) on ​​your server: https: //www.digitalocean .com / community / tutorials / an-introduction-to-tracking-statistics-with-graphite-statsd-and-collectd

There is also a good StatsD node.js client called Lynx: https://github.com/dscape/lynx along with explicit middleware for Lynx to measure the number and response time for express routes: https://github.com / rosskukulinski / lynx-express (Sails is fully compatible with Express / Connect middleware)

I plan to use this in my sail project in the near future, before starting production, I will update it with more details when I do this, let me know if you continue to work before I do this.

+2
source share

I created a Sails Hook that makes the whole process easy. Just install it with npm install sails-hook-responsetime --save and it will add X-Response-Time to the HTTP and Socket requests.

https://www.npmjs.com/package/sails-hook-responsetime

+1
source share

All Articles