By default, a Node.js program runs as a single-thread application. For utilizing all available cores in your system, you can use the cluster module in Node.js. This module can fork specified processes in your program into several processes that run on the desired number of available cores.
For example, you have a server that accepts a client request, generates random characters, and sends back a response. For the purpose of increasing the load, the random characters generator will redo its process 10,000 times. Then, you will make 100 requests to the server in a time to force your server application for spawning new handler processes.
1. Create this sample server program server.js
.
const http = require('http');
const cluster = require('cluster');
const coreNumber = require('os').cpus().length;
// define server object
const server = http.createServer(function(req, res){
console.log(`Handled by PID ${process.pid}.`);
// do expensive computation
let allowedChars = 'abcdefghijklmnopqrstuvwxyz0123456789';
let str;
for (let i=0; i<10000; i++) {
str = '';
for (let j=0; j<100; j++) {
str += allowedChars.charAt( Math.floor(Math.random() * 36) );
}
}
res.setHeader('Content-Type', 'application/json');
res.writeHead(200);
res.end(JSON.stringify({message:'Welcome to Hello API', code:str}));
});
// server initiation function
function init() {
server.listen(3000, ()=>{
console.log(`Server is running on port 3000 with PID ${process.pid}`);
});
}
// initiate cluster
if (cluster.isMaster) { // first process
// spawn new processes as many as core numbers
for (let i=0; i<coreNumber; i++) {
cluster.fork();
}
cluster.on('exit', (worker)=>{
console.log(`A server worker with PID ${worker.process.pid} is died.`);
});
} else { // forked processes
// initiate server
init();
}
Even the program initiates the server's process multiple times, all processes can listen to the same port because it is managed by the cluster module.
2. Create a client program client.js
that sends multiple requests to the server.
const http = require('http');
const requestOptions = {
protocol: 'http:',
hostname: 'localhost',
path: '/',
method: 'GET',
port: 3000
};
for (let i = 0; i<100; i++) {
const request = http.request(requestOptions, (res)=>{
console.log(`Request status code: ${res.statusCode}`);
});
request.end();
}
3. Run the server.
node server.js
4. Run the client.
node client.js
Comments
Post a Comment