7

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.

moka
  • 22,846
  • 4
  • 51
  • 67
ldlchina
  • 927
  • 2
  • 12
  • 31
  • Are you looking for a code coverage tool? That should give you most of what you want. Don't know one off the top of my head though. – beatgammit Jul 16 '13 at 13:42
  • 2
    why dont you write your own tool? it's pretty simple – Rikky Jul 16 '13 at 13:55
  • I created a web service based on nodejs, exposed some rest APIs, some clients are consuming these apis. I want to mornitor the time of each API call, is there any such tool? – ldlchina Jul 16 '13 at 13:57
  • Hi Rikky, How to write the tool? I want a separate tool, so that won't affect the performance of the web service. – ldlchina Jul 16 '13 at 14:04
  • Do you use any Proxy? I think it's best to measure there: http://lincolnloop.com/blog/2010/nov/9/tracking-application-response-time-nginx/ – joewhite86 Jul 16 '13 at 14:07
  • If you're using Express, use `app.use(express.logger('dev'));`; that will log (to `stdout`) how long the request took. – robertklep Jul 16 '13 at 14:25

3 Answers3

5

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

moka
  • 22,846
  • 4
  • 51
  • 67
  • Do I need to call req.timer.complete() in each API? Does this affect the perfoemance? I don't want to make too much change in my code either affect the performance. – ldlchina Jul 16 '13 at 15:12
  • You can override your send method to call it for you, or you can call it manually by yourself after each send. You are definitely overcomplicating just a single call of function - as performance comes really in complex situations, not such little like measuring time of requests.. – moka Jul 16 '13 at 16:37
  • I've modified answer above, that now has injection into `res.send` that does not require any API modifications at all. – moka Jul 16 '13 at 16:46
  • Thanks, I don't like the measuring code is included in the production, any better solution or any 3rd party tool can do this? – ldlchina Jul 17 '13 at 05:10
  • New Relic have Beta for node.js. Any other 3rd party software will be much more resource consuming. Simply, you can't have anything smaller than this code above. – moka Jul 17 '13 at 09:42
  • 1
    How about the code below? Needn't to override the res.json. https.createServer(options, function(req, res){ var start = process.hrtime(); expressApp(req, res); res.on('finish', function() { var elapsed = parseFloat((process.hrtime(start)[1] / 1000000).toFixed(3)); console.log(elapsed); }); }).listen(listeningPort); – ldlchina Jul 18 '13 at 07:56
  • Good one, did some tests, and it looks like that it triggers a bit later, but it is more fair, as probably triggers when data is really sent. I updated answer above. – moka Jul 18 '13 at 08:38
  • 1
    if, god forbid, you have calls that take more then one second, you're printing wrong info to the console. Should be: console.log(t[0] + ":" + elapsed) [where t = process.hrtime(start)] – marmor Mar 02 '14 at 12:33
  • True! :) Lets hope no one will have such case, but I saw some nasty stuff in my life, so who knows.. – moka Mar 03 '14 at 17:11
0

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.

benjiman
  • 3,888
  • 4
  • 29
  • 44
0

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

mike james
  • 8,840
  • 3
  • 18
  • 21