Return to Snippet

Revision: 22137
at January 4, 2010 06:10 by magicrebirth


Initial Code
Here�s helloworld.js:

var sys = require('sys'), 
  http = require('http');

http.createServer(function(req, res) {
  res.sendHeader(200, {'Content-Type': 'text/html'});
  res.sendBody('<h1>Hello World</h1>');
  res.finish();
}).listen(8080);

sys.puts('Server running at http://127.0.0.1:8080/');
If you have Apache Bench installed, try running ab -n 1000 -c 100 �http://127.0.0.1:8080/� to test it with 1000 requests using 100 concurrent connections. On my MacBook Pro I get 3374 requests a second.

So Node is fast�but where it really shines is concurrency with long running requests. Alter the helloworld.js server definition to look like this:

http.createServer(function(req, res) {
  setTimeout(function() {
    res.sendHeader(200, {'Content-Type': 'text/html'});
    res.sendBody('<h1>Hello World</h1>');
    res.finish();
  }, 2000);
}).listen(8080);

Initial URL
http://simonwillison.net/2009/Nov/23/node/

Initial Description


Initial Title
Node JS :: example stress-testing

Initial Tags
js, apache

Initial Language
JavaScript