dail
dail

Reputation: 89

Why node uses 100% of the CPU?

i'm using Express web framework and Node.js.

I'm doing a simple test with ab:

ab -n 1000 -c 100 http://127.0.0.1:3000/

i'm using the default middleware of Express and only one get()

app.get('/', function(req, res){
  res.send("hello");    
});

how can load the CPU at 100%, is not really async ?

THANK YOU

Upvotes: 0

Views: 1172

Answers (1)

ironchefpython
ironchefpython

Reputation: 3409

Just because something is asynchronous, doesn't mean that it is incapable of using all processing resources available. Let's look at your sample server:

// when you get a request for "/", perform the 
// following function as quickly as you can.
app.get('/', function(req, res) {  

  // this  is the function to perform. It is CPU 
  // bound when serving a client *on the same machine*.
  res.send("hello");    
});

When you request ab to make 100 concurrent requests to your sample app, you're obviously going to spike 100% CPU utilization, because node is trying to satisfy those requests as quickly as it can. Just because it's asynchronous, doesn't mean it isn't going to work as hard as it can to do what you tell it to.

Upvotes: 3

Related Questions