SimonHF's Blog

Just another WordPress.com site

node.js versus SXE “Hello World”; node.js performance revisited October 8, 2010

Filed under: Uncategorized — simonhf @ 4:42 am

About a week ago I tested a simple node.js “Hello World” server against one written using SXE. Naively, I copy-and-pasted the example server from the node.js documentation. Since then I have discovered that it’s possible to get faster queries per second speeds out of node.js. So I decided to test the queries per second of different variations of the node.js “Hello World” server. Here is the original version that I tested earlier:

var http = require('http');
http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end('Hello World\n');
}).listen(8000, "127.0.0.1");

Here is a version which is very nearly the same functionality as the SXE “Hello World” server that doesn’t search the headers. After taking the advice of Ryan Dahl, I have opted to use the faster node.js ‘net’ package  instead of the slower node.js ‘http’ package which apparently performs unwanted – for the purposes of the simple “Hello World” test – processing on the HTTP headers. ‘Very nearly the same functionality’ means that – unlike SXE – the data read from consecutives packets on the same socket are not automatically accumulated, e.g. because the HTTP request is bigger than the MTU. I’m not sure the most efficient way to do this in JavaScript so I just left it out for this test where all the packets are smaller than the MTU anyway.

var net = require('net');
var server = net.createServer(function (stream) {
 stream.setEncoding('utf8');
 stream.on('connect', function () {});
 stream.on('data', function (data) {
   if(data.match("\r\n\r\n$")){
     stream.write('HTTP/1.0 200 OK\r\n" "Connection: Keep-Alive\r\n" "Content-Type: text/html\r\nContent-Length: 14\r\n\r\nHello World\n\r\n');
   }
 });
 stream.on('end', function () {stream.end();});
});
server.listen(8000, 'localhost');

And here is hopefully  the simplest and fastest node.js variant that doesn’t check for the end of the HTTP headers so it’s difficult to imagine it doing anything useful with HTTP but it gives us an idea of the fastest possible speed that node.js can handle socket communications:

var net = require('net');
var server = net.createServer(function (stream) {
 stream.on('connect', function () {});
 stream.on('data', function (data) {
     stream.write('HTTP/1.0 200 OK\r\n" "Connection: Keep-Alive\r\n" "Content-Type: text/html\r\nContent-Length: 14\r\n\r\nHello World\n\r\n');
 });
 stream.on('end', function () {stream.end();});
});
server.listen(8000, 'localhost');

And now the results:

“Hello World”    Queries/ % Speed
Server           Second   of SXE
---------------- -------- -------
node.js+http     12,344    16%
Node.js+net+crcr 18,448    24%
Node.js+net      28,867    37%
SXE              78,437   100%

In conclusion, if you want to get the very best performance out of node.js then you might consider using the node.js ‘net’ package instead of the ‘http’ package. Doing so will let your your node.js application approach about a quarter of the speed of the C-language-based SXE equivalent.

Advertisements
 

3 Responses to “node.js versus SXE “Hello World”; node.js performance revisited”

  1. Vesa Vilhonen Says:

    The second node.js example may write response multiple times now depending on how many chunks get received. If you close the stream after writing the response this doesn’t happen. It can be argued that what’s the use for benchmarking a server thats very fast in sending the same hardcoded response to arbitrary requests..

    • simonhf Says:

      Vesa, you’re right! The second example is shown for the purposes of creating an upper bound for the number of events per second that node.js can handle per second. It’s also interesting to see how expensive the ‘data.match’ operator is.

  2. Roland Jonstone Says:

    HTTP has it’s place, but where node shines is it’s sockets. I’m currently serving out my client using lighttp and making sweet comet with node.js.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s