Skip to content

Instantly share code, notes, and snippets.

@bogdanconstantinescu
Created September 13, 2012 15:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bogdanconstantinescu/3715241 to your computer and use it in GitHub Desktop.
Save bogdanconstantinescu/3715241 to your computer and use it in GitHub Desktop.
Async Sinatra vs Express.js

Async Sinatra

  • ruby 1.9.3p194
  • async_sinatra (1.0.0)
  • thin (1.2.11)
    thin start

    ab -n 10000 -c 100 http://127.0.0.1:3000/
    Server Software:        thin
    Server Hostname:        127.0.0.1
    Server Port:            3000

    Document Path:          /
    Document Length:        11 bytes

    Concurrency Level:      100
    Time taken for tests:   5.439 seconds
    Complete requests:      10000
    Failed requests:        0
    Write errors:           0
    Total transferred:      1530000 bytes
    HTML transferred:       110000 bytes
    Requests per second:    1838.52 [#/sec] (mean)
    Time per request:       54.391 [ms] (mean)
    Time per request:       0.544 [ms] (mean, across all concurrent requests)
    Transfer rate:          274.70 [Kbytes/sec] received

    Connection Times (ms)
                  min  mean[+/-sd] median   max
    Connect:        0    1   2.0      1      37
    Processing:    15   53  10.8     52     119
    Waiting:       12   49  10.1     48      96
    Total:         25   54  10.6     53     119

    Percentage of the requests served within a certain time (ms)
      50%     53
      66%     57
      75%     61
      80%     62
      90%     65
      95%     72
      98%     78
      99%     94
     100%    119 (longest request)

Node.js

  • node v0.8.8
  • express 3.0.0rc4
    node app.js

    ab -n 10000 -c 100 http://localhost:3001/
    Server Software:        
    Server Hostname:        127.0.0.1
    Server Port:            3001

    Document Path:          /
    Document Length:        170 bytes

    Concurrency Level:      100
    Time taken for tests:   14.909 seconds
    Complete requests:      10000
    Failed requests:        0
    Write errors:           0
    Total transferred:      3290000 bytes
    HTML transferred:       1700000 bytes
    Requests per second:    670.71 [#/sec] (mean)
    Time per request:       149.095 [ms] (mean)
    Time per request:       1.491 [ms] (mean, across all concurrent requests)
    Transfer rate:          215.49 [Kbytes/sec] received

    Connection Times (ms)
                  min  mean[+/-sd] median   max
    Connect:        0    0   0.8      0      21
    Processing:     2  148  84.1    146     405
    Waiting:        2  148  84.1    146     405
    Total:          2  148  84.1    146     405

    Percentage of the requests served within a certain time (ms)
      50%    146
      66%    192
      75%    218
      80%    233
      90%    263
      95%    281
      98%    299
      99%    312
     100%    405 (longest request)

Conclusion

Ruby still has it (besides being a beautiful language).

//Nothe this is a default installation on Express.js
/**
* Module dependencies.
*/
var express = require('express')
, routes = require('./routes')
, user = require('./routes/user')
, http = require('http')
, path = require('path');
var app = express();
app.configure(function(){
app.set('port', process.env.PORT || 3001);
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));
});
app.configure('development', function(){
app.use(express.errorHandler());
});
app.get('/', routes.index);
app.get('/users', user.list);
http.createServer(app).listen(app.get('port'), function(){
console.log("Express server listening on port " + app.get('port'));
});
# default example for async_sinatra gem
require 'sinatra/async'
class App < Sinatra::Base
register Sinatra::Async
aget '/' do
body "hello async"
end
aget '/delay/:n' do |n|
EM.add_timer(n.to_i) { body { "delayed for #{n} seconds" } }
end
end
require './app'
run App
source 'https://rubygems.org'
gem 'thin'
gem 'bundler'
gem 'async_sinatra'
gem 'thin'
@mattmc3
Copy link

mattmc3 commented Apr 28, 2013

It seems hardly fair to return the simple "hello async" 11 byte string in your Sinatra payload, and have your Node.js Express payload return a full HTML5 page at 170 bytes along with a favicon, style.css, headers and whatever else for a total of 329 bytes.

If you change your index.js file to be equivalent by switching res.render('index', { title: 'Express' }); to res.send("hello async");, then Node turns the tables on Sinatra real fast. Don't get me wrong - I love me some Ruby - but there's no scenario I've seen where a true apples-to-apples comparison has Ruby coming out ahead on performance. Elegance and beauty though, that's another metric entirely, albeit subjective.

Server Hostname:        127.0.0.1
Server Port:            3001

Document Path:          /
Document Length:        11 bytes

Concurrency Level:      100
Time taken for tests:   2.701 seconds
Complete requests:      10000
Failed requests:        0
Write errors:           0
Total transferred:      1690000 bytes
HTML transferred:       110000 bytes
Requests per second:    3702.67 [#/sec] (mean)
Time per request:       27.008 [ms] (mean)
Time per request:       0.270 [ms] (mean, across all concurrent requests)
Transfer rate:          611.09 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   0.8      0      48
Processing:     2   27   9.3     23      80
Waiting:        2   27   9.3     23      80
Total:          6   27   9.1     23      81

Percentage of the requests served within a certain time (ms)
  50%     23
  66%     25
  75%     29
  80%     31
  90%     35
  95%     49
  98%     60
  99%     66
 100%     81 (longest request)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment