PHP 7.1.1 FPM vs Node.js 7.4.0 as a web backend server

Hello everyone, I decided to share with you the results of the synthetic performance test of the latest versions of PHP and Node.js.

Server configuration:

Simple VDS - 1 processor core 2GHz, 1GB of RAM, 10GB SSD.
OS: Debian 8.6.
Basic kernel settings have also been made so that the server, in principle, can handle a large number of connections.

Test subjects:

- PHP 7.1.1 FPM
- Node.js 7.4.0

First step:

Here are the operations that the backend mainly uses. Namely: string gluing, network input-output, arithmetic and work with arrays.

Code for Node.js:

var fs = require('fs');
var mysql = require('mysql2');
console.time('Node.js ' + process.version + ': склеивание строк 1000000 раз');
var str = '';
for (var i = 0; i < 1000000; i++) {
	str += 's';
}
console.timeEnd('Node.js ' + process.version + ': склеивание строк 1000000 раз');
console.time('Node.js ' + process.version + ': сложение чисел 1000000 раз');
var count = 0;
for (var i = 0; i < 1000000; i++) {
	count++;
}
console.timeEnd('Node.js ' + process.version + ': сложение чисел 1000000 раз');
console.time('Node.js ' + process.version + ': наполнение простого массива 1000000 раз');
var array = [];
for (var i = 0; i < 1000000; i++) {
	array.push('s');
}
console.timeEnd('Node.js ' + process.version + ': наполнение простого массива 1000000 раз');
console.time('Node.js ' + process.version + ': наполнение ассоциативного массива 1000000 раз');
var array = {};
for (var i = 0; i < 1000000; i++) {
	array['s' + i] = 's';
}
console.timeEnd('Node.js ' + process.version + ': наполнение ассоциативного массива 1000000 раз');
console.time('Node.js ' + process.version + ': чтение файла 100 раз');
var content;
for (var i = 0; i < 100; i++) {
    content = fs.readFileSync('./someFile.txt');
}
console.timeEnd('Node.js ' + process.version + ': чтение файла 100 раз');
console.time('Node.js ' + process.version + ': mysql query (SELECT NOW()) 100 раз');
// create the connection to database
var connection = mysql.createConnection({host:'localhost', user: 'root', database: 'test', password: 'password'});
function promiseQuery(query) {
    return new Promise((resolve, reject) => {
            connection.query(query, function (err, results, fields) {
            resolve({err, results, fields});
        });
	});
}
for (var i = 0; i < 100; i++) {
    var a = promiseQuery('SELECT NOW()');
    a.then(({err, results, fields}) => {
        //console.log(results);
    });
}
console.timeEnd('Node.js ' + process.version + ': mysql query (SELECT NOW()) 100 раз');
connection.end();

Code for PHP:

query("SELECT NOW() as `now`");
	$now = $res->fetch_assoc()['now'];
}
echo "PHP $phpVersion: mysql query (SELECT NOW()) 100 раз: " . round((microtime(1) - $start) * 1000, 3) . "ms \n";

Results:

image

As you can see, PHP wins on all points except the addition operation.

Second stage:

Load testing "Hello world". Nginx 11.7 + PHP 7.1.1 FPM vs Node.js. 1000 requests in 1000 threads. #ab -n 1000 -c 1000 ...

PHP code:


Code Node.js:

const http = require('http');
const hostname = '127.0.0.1';
const port = 3000;
const server = http.createServer((req, res) => {
  res.statusCode = 200;
  res.setHeader('Content-Type', 'text/plain');
  res.end('Hello World');
});
server.listen(port, hostname, () => {
  console.log(`Server running at http://${hostname}:${port}/`);
});

Results I

ran 10 tests for PHP and Node.js and chose the best results for both.

Node.js:

image

PHP:

image

As you can see here, PHP wins by 23% or 628 requests per second. It is a lot or a little to judge you.

Share your thoughts on the subject in the comments.

Also popular now: