Node JS :: example stress-testing


/ Published in: JavaScript
Save to your folder(s)



Copy this code and paste it in your HTML
  1. Here�s helloworld.js:
  2.  
  3. var sys = require('sys'),
  4. http = require('http');
  5.  
  6. http.createServer(function(req, res) {
  7. res.sendHeader(200, {'Content-Type': 'text/html'});
  8. res.sendBody('<h1>Hello World</h1>');
  9. res.finish();
  10. }).listen(8080);
  11.  
  12. sys.puts('Server running at http://127.0.0.1:8080/');
  13. 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.
  14.  
  15. 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:
  16.  
  17. http.createServer(function(req, res) {
  18. setTimeout(function() {
  19. res.sendHeader(200, {'Content-Type': 'text/html'});
  20. res.sendBody('<h1>Hello World</h1>');
  21. res.finish();
  22. }, 2000);
  23. }).listen(8080);

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

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.