Node.js vs PHP+Apache

PHP and Apache - the most content technology for implementing web applications. Both tools often face significant criticism regarding their performance. However, PHP in particular has to endure a lot regarding its language design. In this Blog postthe main points of criticism are summarized. Broadly speaking, the ecosystem around PHP can be described as a collection of inconsistent 1:1 wrappers for C libraries. Node.js stands in contrast to this. Node.js is an asynchronous communication framework based on Google's V8 JavaScript engine, which is often praised for its good performance and good design.

Synchronous vs Asynchronous

However, to explain the difference between the communication models of PHP and Node.js, one must delve a little deeper: what is the difference between synchronous and asynchronous I/O? In synchronous or blocking I/O, a read command causes the program to freeze until the data to be read is available. The CPU therefore spends most of its time waiting for data rather than calculating, even though some things could still be calculated. To allow a web server to respond to other requests despite blocking I/O, the server simply starts a separate thread for each request. A lot of RAM is occupied by programs waiting for data. Asynchronous communication, on the other hand, promises better resource utilization: after a read command, the data is not yet there, but the program continues to run. Further read commands can be issued. Only when everything that needs to be done has been done is there a wait. In the best-case scenario, the first pieces of data are already available at that moment, and processing can begin. Furthermore, multiple simultaneous incoming read commands can be handled better by the operating system, as, for example, Linux reorders access to hard drives to move the hard drive's read head as little as possible. In Node.js, this looks like providing a callback function to a read command, which is triggered when the data arrives. This communication paradigm allows for the development of asynchronous programs that fire off as much I/O as possible instead of waiting for data each time.

The Benchmark

In principle, the asynchronous I/O concept is superior to synchronous communication. The V8 engine used in Node.js is also credited with a 50-fold processing speed of JavaScript compared to PHP. However, aside from these point measurements and theoretical considerations - how much advantage does Node.js really bring? For this, the following experimental setup is used:

The Code

View for PHP View for Node.js View for Node.js with mysql-native (better code readability)

The corresponding sample database can also be download.

The results

In the first test setup, there is already a surprising result:
PHP Node.js with mysql Node.js with mysql-native
real 0m7.410s
user 0m3.653s
sys 0m2.107s
real 0m9.771s
user 0m6.707s
sys 0m1.399s
real	0m7.533s
user	0m5.249s
sys	0m0.984s
As you can see, PHP is finished 2 seconds sooner and also only "wasted" half as much CPU time. After switching the MySQL adapter, you get roughly the same runtime as PHP, albeit still with significantly more wasted computing time. The second test setup provides an even more shocking picture. Using the "ab" benchmark tool from Apache, a storm of requests was unleashed on the web servers set up with the test program. Work was done on one hand with the parameters "ab -c 200 -n 20000" and with "ab -c 20 -n 2000". The -c parameter stands for the number of concurrent requests and -n stands for the number of GET requests to be fired in total. Here, PHP+Apache performs 10 times faster than Node.js. The use of the mysql-native adapter in Node.js even worsened the result here. Here is the sobering result:
Value PHP+Apache Node.js
-c 20 -c 200 -c 20 -c 200
Requests/s 1173 1146 119 114
Response time in ms 17 174 167 1750
Throughput in MiB/s 21 20 2 2
CPU utilization 100% on all 4 cores 80% Node.js 16% MySQL 16% MySQL
Response time -c 1 9.4 ms 8.9 ms
As you can see, both servers have roughly the same throughput, regardless of how many requests arrive simultaneously. However, the load is slightly noticeable on them. The response time also increases proportionally with the load on both servers. Since Apache starts a new PHP process for every request, all 4 cores can be utilized. Node.js, on the other hand, only processes the code on one core and uses multiple threads only to handle blocking IO operations. PHP can therefore utilize up to 4 times more resources than Node.js. In addition, PHP consumed only about half as much CPU time in the first benchmark with roughly the same execution time.All in all, Node.js thus only achieves one-tenth of the throughput of PHP+Apache.

Conclusion

The factor of 10 is not a definitive criterion. If one were to replicate the Node.js server and balance the loads, one could at least utilize all 4 cores of the server and be "only" 2.5 times slower than PHP+Apache. If the Node.js developers put a bit more work into the micro-optimization of the framework, even better results could certainly be achieved. Because a large part of the CPU time is likely used by Node.js for converting strings into buffers.   For practical purposes where data throughput is critical, one should, however, stick with the already proven and optimized PHP. Perhaps the tide will turn, but until then, a lot of water will still flow down the Spree.
Категории
Метки