简体   繁体   中英

REST API measuring server-side response times (performance).

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.

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

What about a performance measuring tool like Apache JMeter . You can easily use it to simulate a (heavy) load on your server and then measure response times and other performance indicators. It provides multiple graphical representations for this.

This Blog Post shows how you can setup an HTTP based performance test for Web-APIs. I do it this way to test my RESTful webservices.

Keep it simple use the console.time('timerName'); console.timeEnd('timerName') features in Node. 'timerName' is obviously configurable.

Example:

console.time('getCustomers'); console.timeEnd('getCustomers')

Output:

getCustomers: 58ms

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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