Node.js vs PHP processing speed

Posted by Cody Craven on Stack Overflow See other posts from Stack Overflow or by Cody Craven
Published on 2012-03-23T20:55:29Z Indexed on 2012/03/23 23:30 UTC
Read the original article Hit count: 366

Filed under:
|
|
|
|

I've been looking into node.js recently and wanted to see a true comparison of processing speed for PHP vs Node.js. In most of the comparisons I had seen, Node trounced Apache/PHP set ups handily. However all of the tests were small 'hello worlds' that would not accurately reflect any webpage's markup.

So I decided to create a basic HTML page with 10,000 hello world paragraph elements. In these tests Node with Cluster was beaten to a pulp by PHP on Nginx utilizing PHP-FPM. So I'm curious if I am misusing Node somehow or if Node is really just this bad at processing power.

Note that my results were equivalent outputting "Hello world\n" with text/plain as the HTML, but I only included the HTML as it's closer to the use case I was investigating.

My testing box:

  • Core i7-2600 Intel CPU (has 8 threads with 4 cores)
  • 8GB DDR3 RAM
  • Fedora 16 64bit
  • Node.js v0.6.13
  • Nginx v1.0.13
  • PHP v5.3.10 (with PHP-FPM)

My test scripts:

Node.js script

var cluster = require('cluster');
var http = require('http');
var numCPUs = require('os').cpus().length;

if (cluster.isMaster) {
  // Fork workers.
  for (var i = 0; i < numCPUs; i++) {
    cluster.fork();
  }

  cluster.on('death', function (worker) {
    console.log('worker ' + worker.pid + ' died');
  });
}
else {
  // Worker processes have an HTTP server.
  http.Server(function (req, res) {
    res.writeHead(200, {'Content-Type': 'text/html'});

    res.write('<html>\n<head>\n<title>Speed test</title>\n</head>\n<body>\n');
    for (var i = 0; i < 10000; i++) {
      res.write('<p>Hello world</p>\n');
    }

    res.end('</body>\n</html>');
  }).listen(80);
}

This script is adapted from Node.js' documentation at http://nodejs.org/docs/latest/api/cluster.html

PHP script

<?php

echo "<html>\n<head>\n<title>Speed test</title>\n</head>\n<body>\n";

for ($i = 0; $i < 10000; $i++) {
  echo "<p>Hello world</p>\n";
}

echo "</body>\n</html>";

My results

Node.js

$ ab -n 500 -c 20 http://speedtest.dev/
This is ApacheBench, Version 2.3 <$Revision: 655654 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking speedtest.dev (be patient)
Completed 100 requests
Completed 200 requests
Completed 300 requests
Completed 400 requests
Completed 500 requests
Finished 500 requests


Server Software:        
Server Hostname:        speedtest.dev
Server Port:            80

Document Path:          /
Document Length:        190070 bytes

Concurrency Level:      20
Time taken for tests:   14.603 seconds
Complete requests:      500
Failed requests:        0
Write errors:           0
Total transferred:      95066500 bytes
HTML transferred:       95035000 bytes
Requests per second:    34.24 [#/sec] (mean)
Time per request:       584.123 [ms] (mean)
Time per request:       29.206 [ms] (mean, across all concurrent requests)
Transfer rate:          6357.45 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   0.2      0       2
Processing:    94  547 405.4    424    2516
Waiting:        0  331 399.3    216    2284
Total:         95  547 405.4    424    2516

Percentage of the requests served within a certain time (ms)
  50%    424
  66%    607
  75%    733
  80%    813
  90%   1084
  95%   1325
  98%   1843
  99%   2062
 100%   2516 (longest request)

PHP/Nginx

$ ab -n 500 -c 20 http://speedtest.dev/test.php
This is ApacheBench, Version 2.3 <$Revision: 655654 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking speedtest.dev (be patient)
Completed 100 requests
Completed 200 requests
Completed 300 requests
Completed 400 requests
Completed 500 requests
Finished 500 requests


Server Software:        nginx/1.0.13
Server Hostname:        speedtest.dev
Server Port:            80

Document Path:          /test.php
Document Length:        190070 bytes

Concurrency Level:      20
Time taken for tests:   0.130 seconds
Complete requests:      500
Failed requests:        0
Write errors:           0
Total transferred:      95109000 bytes
HTML transferred:       95035000 bytes
Requests per second:    3849.11 [#/sec] (mean)
Time per request:       5.196 [ms] (mean)
Time per request:       0.260 [ms] (mean, across all concurrent requests)
Transfer rate:          715010.65 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   0.2      0       1
Processing:     3    5   0.7      5       7
Waiting:        1    4   0.7      4       7
Total:          3    5   0.7      5       7

Percentage of the requests served within a certain time (ms)
  50%      5
  66%      5
  75%      5
  80%      6
  90%      6
  95%      6
  98%      6
  99%      6
 100%      7 (longest request)

Additional details

Again what I'm looking for is to find out if I'm doing something wrong with Node.js or if it is really just that slow compared to PHP on Nginx with FPM.

I certainly think Node has a real niche that it could fit well, however with these test results (which I really hope I made a mistake with - as I like the idea of Node) lead me to believe that it is a horrible choice for even a modest processing load when compared to PHP (let alone JVM or various other fast solutions).

As a final note, I also tried running an Apache Bench test against node with $ ab -n 20 -c 20 http://speedtest.dev/ and consistently received a total test time of greater than 0.900 seconds.

© Stack Overflow or respective owner

Related posts about php

Related posts about node.js