REST API measuring server-side response times (performance).
javascript, node.js, performance, rest
Solution
Here is example of how to make event injection with precise time measuring using express.js.
Add this before your routes:
app.all('*', function(req, res, next) {
var start = process.hrtime();
// event triggers when express is done sending response
res.on('finish', function() {
var hrtime = process.hrtime(start);
var elapsed = parseFloat(hrtime[0] + (hrtime[1] / 1000000).toFixed(3), 10);
console.log(elapsed + 'ms');
});
next();
});
It will save start time of each request, and will trigger `finish` after response is sent to client. Thanks for user419127 pointing to 'finish' event
Problem
I developed some rest APIs based nodejs, I want to test the performance of the APIs. Is any tool can easily count the time of each API call? Or how to implement measuring of time required for REST API to response on requests.