📜 ⬆️ ⬇️

Nginx + PHP 7.1.1 FPM vs Node.js 7.7.1 as part 2 backend

Hello! Continuing the confrontation of 2 languages.

Today we will have a more honest comparison that displays most of the real-world problems.
So, today we compare PHP and Node.js with the following features:

  1. Typical dynamic page
  2. REST API


Important! Answers to questions and presentations that may arise:
')
Question: And if you run Node.js in a cluster, will it be faster?
Answer: A single nuclear processor will be slower. (Checked repeatedly)

Question: And if you use a connection pool to connect to mysql in Node.js, it will probably work faster because of the parallelization of queries?
Answer: A single nuclear processor will be slower. (Checked repeatedly)

Question: It is necessary that the code from the node is warmed up, there is no way JIT, then it will work faster?
Answer: It goes without saying that everything is heated and the results are taken as the most successful.

Predjava: Yes, you set everything up for a hike, for real results will be different.
Answer: Test code in open access on github, copy and verify yourself.

Predjava: ab should not be run on the server on which the test backends are running, but on another computer.
Answer: Explain laziness. But if in 2 words, then ab practically does not take resources during the test and gives more real numbers that your server is capable of.

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.

I generated data in a table (100 records).

Let's get started

First stage (single requests): out of 10 single requests for each test subject, select the average value.

Node.js - Typical dynamic page

image

Node.js - REST API

image

PHP - Typical dynamic page

image

PHP - REST API

image

As you can see, the difference in the generation rate is different at times, and PHP wins this round.

Second stage (load testing):

From the 10 runs for each test, select the average value. Here we are interested in how many requests per second our server is able to handle. 3000 requests in 1000 threads. #ab -n 3000 -c 1000 ...

Node.js - Typical dynamic page

image

Node.js - REST API

image

PHP - Typical dynamic page

image

PHP - REST API

image

Dynamic page - PHP is almost 2 times faster (1.78 to be exact)
REST API - PHP is almost 3.5 times faster (3.32 to be exact)

Link to the repository with code for tests

Concerning configs of system, pkhp, Mysql
I have been configured to work as fast as possible.
If you do not know how the operating system should be configured, php, nginx and mysql. It's not my problem.

I would be very grateful if you write your own variants of the implementation of the REST API for Node.js, so that the results are faster than mine. And leave the code in the comments, I will take measurements and write the results. The goal is to catch up and better overtake PHP.

UPDATE


Following the advice of the dos user, I changed the MySQL driver in Node.js to github.com/mscdex/node-mariasql and the results have changed dramatically!

Updated Node.js Results

Stage 1

Node.js - Typical dynamic page
image

Node.js - REST API
image

Stage 2

Node.js - Typical dynamic page
image

Node.js - REST API
image

Dynamic page - Node.js is 1.16 times faster (by 16%)
REST API - Node.js is 1.26 times faster (by 26%)

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


All Articles