SimonHF's Blog

Just another WordPress.com site

node.js versus Lua “Hello World” October 13, 2010

Neil Watkiss — known among other things for many cool Perl modules — has created a non-optimized, experimental version of SXE (pronounced ‘sexy’) containing embedded Lua called SXELua. So I thought it would be fun to redo the familiar – to readers of this blog – ‘Hello World’ benchmark using SXELua. And here is the Lua source code:

do
    local connect = function (sxe) end
    local read = function (sxe, content)
        if content:match("\r\n\r\n", -4) then sxe_write(sxe,"HTTP/1.0 200 OK\r\nConnection: Close\r\nContent-Type: text/html\r\nContent-Length: 14\r\n\r\nHello World\n\r\n")
        end
    end
    local close = function (sxe) end
    sxe_register(10001, function () sxe_listen(sxe_new_tcp("127.0.0.1", 8000, connect, read, close)) end)
end

Compare this with the slightly longer node.js equivalent from the last blog:

var net = require('net');
var server = net.createServer(function (stream) {
  stream.on('connect', function () {});
  stream.on('data', function (data) {
    var l = data.length;
    if (l >= 4 && data[l - 4] == 0xd && data [l - 3] == 0xa && data[l - 2] == 0xd && data[l - 1] == 0xa) {
      stream.write('HTTP/1.0 200 OK\r\nConnection: Keep-Alive\r\nContent-Type: text/html\r\nContent-Length: 13\r\n\r\nHello World\r\n');
    }
  });
  stream.on('end', function () {stream.end();});
});
server.listen(8000, 'localhost');

And now the updated results:

“Hello World”    Queries/ % Speed
Server           Second   of SXE
---------------- -------- -------
node.js+http     12,344    16%
Node.js+net+crcr 23,224    30% <-- *1
Node.js+net      28,867    37%
SXELua           66,731    85%
SXE              78,437   100%

In conclusion, calling Lua functions from C and vice-versa is very fast… close  to the speed of C itself. I am very excited by how well Lua performed in the benchmark. The Lua “Hello World” program performed 3.6 times better than the node.js equivalent. After a quick Google it looks like this isn’t the first time that JavaScript V8 has gone up against Lua; these results suggest that SXELua could get even faster after optimization. It looks like Lua will become part of SXE soon. Lua seems ideal for creating tests for SXE & SXELua programs alike, and prototyping programs. Stay tuned…!

*1 Update: Somebody who knows JavaScript better than me offered faster code to detect the “\n\r\n\r”. I updated the script above and the resulting queries per second and % speed of SXE.

Advertisements
 

18 Responses to “node.js versus Lua “Hello World””

  1. kuroikaze85 Says:

    Why you need empty callback on ‘connect’ event?

    Also, I cannot find SXELUA anywhere. Can you give me the link please? I want to test it too.

    • simonhf Says:

      kuroikaze85, the connect event is just for show 🙂 Unfortunately, SXELua is not out yet but hopefully very soon… 🙂 I will announce it here.

  2. Konic Says:

    The Lighttpd author has done a similar thing with his Lua magnet (in fact you have two choices, either use Lua over fastcgi – allows for concurrent processing, but uses an internal socket, or for really small and fast things, there’s mod_magnet, which embeds lua directly into the server).

  3. Vesa Vilhonen Says:

    In the examples you might never send response as data may be chunked so that you receive only \n\r for example and never end up sending the response. This might have effect on the results..

  4. Anonymouse Says:

    Yawn.

    This benchmark is meaningless. The difference between Node.js+net and SXELUA is 19 microseconds. Not miliseconds, MICROseconds. (Anyone else remember the 1MHz Apple2? We’re now responding to HTTP requests in the time it used to take to load an 8-bit register.)

    Some problems:
    – People should never write their own HTTP parsers.
    – Is SXE even event driven? (Couldn’t find the link to it.) That’s a huge win for node — scalability without multithreading problems.
    – Why not go all the way and really write a test in C? Given how low-level the test is, your language doesn’t seem to be adding anything useful.

    • simonhf Says:

      Anonymouse, hehe I remember the Apple II and playing Castle Wolfenstein 🙂 SXE is event driven and it’s a bit more than that too; https://simonhf.wordpress.com/2010/10/09/what-is-sxe/ . The test in C is the result (listed above) called ‘SXE’ and comes from a previous blog: https://simonhf.wordpress.com/2010/10/01/node-js-versus-sxe-hello-world-complexity-speed-and-memory-usage/ . I agree with you that the language part of the test isn’t doing any heavy lifting with the “Hello World” test. But it’s interesting to see, for example, how much cheaper calling a subroutine from C in Lua is than calling a subroutine from C in V8 Javascript. Or seeing how greedy V8 Javascript is… for example, memory seems to be limiting concurrency with node.js much more than CPU… I take your point about the time for an individual query. However, SXE is designed to offer rapid development and scalability all the way up to facebook levels and beyond — i.e. the 1 billion range — where the number of servers plays a significant factor in the business plan. I agree with you also that a more complex benchmark — where the languages get to do more heavy lifting — sounds good. Any ideas for one?

  5. Sébastien Says:

    Could you also give the code/reference to the script/tool you used for this benchmarks ? It would be good to be able to reproduce the test and see if the number actually work 🙂

  6. John Stracke Says:

    “Lua” is not an acronym.

  7. simonhf Says:

    Interesting thread — sparked by this blog — about node.js and Lua:
    http://news.ycombinator.com/item?id=1786003

  8. Paul Grenier Says:

    I’m dubious about your choice of JavaScript string evaluation. I wrote some tests at JSPERF (http://jsperf.com/string-test-techniques) using most of your logic. Because I tested in a browser (Chrome) and not a server with Node.js, there may be differences. However, it seems to me that chunking the string, as you’ve done, but with ===’\r’ or ===’\n’ is faster (~23%).

    • simonhf Says:

      Paul, Thanks for the info. I’m not very experienced with JavaScript and I’m happy to try a different technique. Please post the code for me to try out because I’m not sure I understood the suggestion. Thanks!

  9. Alex Kloss Says:

    I use both lua (via my own magnet and a cgi module which handles get/post/cookies) and node.js in different projects. This sxe-Module seems very interesting and I look forward to test it against lighttpd/fcgi magnet/lua (which exceeds node.js on big static requests, but is in some cases a tad slower on smaller dynamic requests) soon.

    Anyway, you do know that javascript can do RegExp, too? A short benchmark shows me that /\r\n\r\n$/.test(data) is probably faster than the complicated if-condition (3.2ms vs. 0.3ms in Firefox/Firebug JS Benchmark).

    Greetings, Alex

    • simonhf Says:

      Thanks for the message, Alex.

      You know, I think I had such a regex originally and then changed it to the ‘if’ statement and it was faster but I can’t remember by how much. I did make an effort to try and get the javascript code as fast as possible.

      I do agree with you that it’s not clear cut as to which would be faster in the javascript world. In the C world then the ‘if’ statement would be very much faster. However, with javascript we’re at the mercy of the underlying object providing the element that the ‘if’ statement works on.

      The original performance test I blogged about picked up some criticism. Readers wanted a test more realistic and closer to real world situations. One idea is to have a client which queries a server, and then in order to compose the reply, the server queries n other servers. This sounds like a good test to run. However, the javascript is going to get more complicated because the server (contacted by the client) is going to have to keep state as it asynchronously queries the other servers. Could you imagine to use your optimization skills to help out with such a test?


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