Stack s
Stack s

Reputation: 133

how to make cpu usage to 100% in nodejs

I am having a nodejs server.I want to test the node-toobusy module in this server. I wrote a client code hitting concurrently this server to make the cpu usage to 100% but still i could not make that module work. PFB the server code

var toobusy = require('toobusy'),
express = require('express');

var app = express();

// middleware which blocks requests when we're too busy app.use(function(req, res, next) {
 if (toobusy()) {
res.send(503, "I'm busy right now, sorry."); console.log("hi");
 } else {
 next();
console.log("hi");
 }
});

console.log("hi");
app.get('/', function(req, res) {
// processing the request requires some work!
var i = 0;
while (i < 1e5) i++;
res.send("I counted to " + i);
console.log("hi");
});

var server = app.listen(3005);

process.on('SIGINT', function() {
server.close();
// calling .shutdown allows your process to exit normally  toobusy.shutdown();
process.exit();
});

Any ideas regarding how to use that module will be really helpful.

Upvotes: 2

Views: 1953

Answers (1)

raygerrard
raygerrard

Reputation: 842

You are only checking if the server is toobusy() when your app starts up.

See below for usage as middleware:

app.use(function(req, res, next) {
    if (toobusy()) {
        res.send(503, "I'm busy right now, sorry.");
    } else {
        next();
    } 
});

This will check every request that comes in and only call next() if the current load is below the maximum threshold.

You can also set the maximum latency threshold with:

toobusy.maxLag(10);

Upvotes: 1

Related Questions