📜 ⬆️ ⬇️

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 test of the performance of fresh versions of PHP and Node.js.

Server configuration:

Simple VDS - 1 2GHz processor core, 1GB of RAM, 10GB SSD.
OS: Debian 8.6.
The basic kernel settings were also made so that the server could in principle handle a large number of connections.
')
Subjects:

- PHP 7.1.1 FPM
- Node.js 7.4.0

First stage:

There are operations that are mainly used by the backend. Namely: stitching strings, network I / O, arithmetic, and working 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(); 

PHP code:

 <?php $phpVersion = "v" . explode('-', PHP_VERSION)[0]; $start = microtime(1); $str = ''; for ($i = 0; $i < 1000000; $i++) { $str .= 's'; } echo "PHP $phpVersion:   1000000 : " . round((microtime(1) - $start) * 1000, 3) . "ms \n"; $start = microtime(1); $count = 0; for ($i = 0; $i < 1000000; $i++) { $count++; } echo "PHP $phpVersion:   1000000 : " . round((microtime(1) - $start) * 1000, 3) . "ms \n"; $start = microtime(1); $array = array(); for ($i = 0; $i < 1000000; $i++) { $array[] = 's'; } echo "PHP $phpVersion:    1000000 : " . round((microtime(1) - $start) * 1000, 3) . "ms \n"; $start = microtime(1); $array = array(); for ($i = 0; $i < 1000000; $i++) { $array["s" . $i] = 's'; } echo "PHP $phpVersion:    1000000 : " . round((microtime(1) - $start) * 1000, 3) . "ms \n"; $start = microtime(1); for ($i = 0; $i < 100; $i++) { $fp = fopen("./someFile.txt", "r"); $content = fread($fp, filesize("./someFile.txt")); fclose($fp); } echo "PHP $phpVersion:   100 : " . round((microtime(1) - $start) * 1000, 3) . "ms \n"; $start = microtime(1); $mysql = new mysqli('localhost', 'root', 'password', 'test'); for ($i = 0; $i < 100; $i++) { $res = $mysql->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 for the addition operation.

Second phase:

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:

 <?php echo "Hello world"; ?> 

Node.js code:

 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 drove 10 tests for PHP and Node.js and chose the best results from both.

Node.js:

image

PHP:

image

As we see here, PHP wins by 23% or by 628 requests per second. Much or little judge you.

Share your thoughts about this in the comments.

Source: https://habr.com/ru/post/320670/


All Articles