Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get requests per second from node.js http server

Is there a way in node.js to get the number of open connections and number of requests per second from a http server?

Assume the following simple server:

http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end("Hello World!");
}).listen(80);

Thanks.

like image 701
Justin Avatar asked Jan 26 '26 13:01

Justin


1 Answers

This is what I usually do when I want to double-check numbers ab/httperf/wrk/siege report:

var served = 0;
var concurrent = 0;

http.createServer(function (req, res) {
  concurrent++;
  res.writeHead(200, {'Content-Type': 'text/plain'});
  setTimeout(function() { // emulate some async delay
    served++;
    concurrent--;
    res.end("Hello World!");
  }, 10);
}).listen(80);

setInterval(function() {
  console.log('Requests per second:' + served);
  console.log('Concurrent requests:' + concurrent);
  served = 0;
}, 1000);
like image 136
Andrey Sidorov Avatar answered Jan 29 '26 03:01

Andrey Sidorov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!