Reputation: 6542
Iv'e installed on my local machine a simple NodeJS Server (v8.16.2) with the following hello-world code snippet:
const http = require('http');
const hostname = '127.0.0.1';
const port = 4000;
const server = http.createServer((req, res) => {
res.send('Hello world!');
});
server.listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`);
});
I'm load testing the server using 800 parallel requests via JMeter and getting an average response time of 130(+)ms.
Any ideas what tweaks \ configurations I might require in order to achieve a better average response time?
Upvotes: 1
Views: 361
Reputation: 2081
A single instance of Node.js runs in a single thread. To take advantage of multi-core systems, the user will sometimes want to launch a cluster of Node.js processes to handle the load.
The cluster module allows easy creation of child processes that all share server ports.
HTTP/2 commonly called SPDY is the latest web protocol standard developed by the IETF HTTP workgroup. HTTP/2 makes web browsing faster, easier and lower bandwidth usage. It focuses on performance, especially to solve problems that still occur in previous versions of HTTP/1.x. Currently, you can see some popular web like google, facebook and youtube, have implemented the HTTP/2 protocol on its web page. comment it in your code.
const cluster = require('cluster');
const http = require('http');
//const http2 = require('http2');
const numCPUs = require('os').cpus().length;
/*const options = {
key: fs.readFileSync('server.key'),
cert: fs.readFileSync('server.crt'),
requestCert: false,
rejectUnauthorized: false
};*/
if (cluster.isMaster) {
console.log(`Master ${process.pid} is running`);
// Fork workers.
for (let i = 0; i < numCPUs; i++) {
cluster.fork();
}
cluster.on('exit', (worker, code, signal) => {
console.log(`worker ${worker.process.pid} died`);
});
} else {
// Workers can share any TCP connection
// In this case it is an HTTP server
// const server = http2.createSecureServer(options);
http.createServer((req, res) => {
res.writeHead(200);
res.end('hello world\n');
}).listen(4000);
console.log(`Worker ${process.pid} started`);
}
and using the newest version of node js.
Upvotes: 2