Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
Ruby off Rails Rack, Sinatra and Sequel Jiang Wu 2009.3.14
Outline <ul><li>Rack </li></ul><ul><li>Sinatra </li></ul><ul><li>Sequel </li></ul><ul><li>Ruby off Rails </li></ul><ul><li...
rack http://rack.rubyforge.net/ Rack aims to provide a minimal API for connecting web servers and web frameworks.
Rack SPEC {&quot;HTTP_USER_AGENT&quot;=>&quot;curl/7.12.2 ...&quot;, &quot;REQUEST_URI&quot;=>&quot;http://ruby-lang.org/&...
Rack middleware: process RESP RESP RESP Rack Middleware 1 Rack Middleware 3 Rack Middleware 2
A rack middleware can do: <ul><li>Do nothing (usually after a condition judgment) </li></ul><ul><li>Response directly </li...
Write your own rack middleware <ul><li>nothing.ru </li></ul><ul><li>middleware do nothing </li></ul><ul><li>calc_time.ru <...
sinatra http://www.sinatrarb.com/
sinatra program: infinity.rb <ul><li>1 require 'rubygems' </li></ul><ul><li>2 require 'sinatra' </li></ul><ul><li>  3  </l...
sequel The Database Toolkit for Ruby sequel postgres://localhost/blog Your database is stored in DB...  >> DB.tables => [:...
sinatra + sequel: a simple note ruby notes.rb Just  ONE  file to run!
Ruby off Rails <ul><li>jquery  vs.   prototype.js </li></ul><ul><li>jquery UI  vs.  script.aculo.us </li></ul><ul><li>extl...
Off Rails if you ... <ul><li>Hate the namespace pollution </li></ul><ul><ul><li>Prototype.js </li></ul></ul><ul><ul><li>Ac...
On Rails if you ... <ul><li>Need mature plug-ins </li></ul><ul><li>Need write-through cache(with support from Cache-money)...
Rails metal: new in 2.3 <ul><li>If you have a Rails application that has service end </li></ul><ul><li>points that need to...
Rails Metal: the code <ul><li>“Hello World” Metal: </li></ul><ul><li>class Poller < Rails::Rack::Metal </li></ul><ul><li>d...
Rails Metal and sinatra <ul><li>1 require 'sinatra/base' </li></ul><ul><li>2  </li></ul><ul><li>3 class Hello < Sinatra::B...
Speed contrast:  sequel and metal <ul><li>Ubuntu 8.04, Intel Atom N270 @1.60GHz, 100 requests, </li></ul><ul><li>developme...
Questions?
Thank you! <ul><li>http://masterwujiang.javaeye.com/ </li></ul><ul><li>my blog (Chinese only) </li></ul><ul><li>http://git...
Upcoming SlideShare
Loading in …5
×

Ruby off Rails---rack, sinatra and sequel

11,930 views

Published on

A presentation on Shanghai on Rails, March 14 2009, includes rack middelware introduction, one-file application using sinatra and sequel, and sinatra program running under Rails Metal.

Published in: Technology
  • The production benchmark is in the under link, wish you know Chinese to understand it better.
    http://masterwujiang.javaeye.com/blog/349203
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here

Ruby off Rails---rack, sinatra and sequel

  1. 1. Ruby off Rails Rack, Sinatra and Sequel Jiang Wu 2009.3.14
  2. 2. Outline <ul><li>Rack </li></ul><ul><li>Sinatra </li></ul><ul><li>Sequel </li></ul><ul><li>Ruby off Rails </li></ul><ul><li>Rails Metal: A way of fusion </li></ul>
  3. 3. rack http://rack.rubyforge.net/ Rack aims to provide a minimal API for connecting web servers and web frameworks.
  4. 4. Rack SPEC {&quot;HTTP_USER_AGENT&quot;=>&quot;curl/7.12.2 ...&quot;, &quot;REQUEST_URI&quot;=>&quot;http://ruby-lang.org/&quot;, &quot;QUERY_STRING&quot;=>&quot;&quot;, &quot;HTTP_ACCEPT&quot;=>&quot;*/*&quot;, &quot;REQUEST_METHOD&quot;=>&quot;GET&quot;, &quot;rack.input&quot; => '...', ... } call(env) [status, headers, body] to_i each each [String,String] String yields yields
  5. 5. Rack middleware: process RESP RESP RESP Rack Middleware 1 Rack Middleware 3 Rack Middleware 2
  6. 6. A rack middleware can do: <ul><li>Do nothing (usually after a condition judgment) </li></ul><ul><li>Response directly </li></ul><ul><li>Alter environment variables </li></ul><ul><li>Alter response(body, headers and status code) </li></ul>
  7. 7. Write your own rack middleware <ul><li>nothing.ru </li></ul><ul><li>middleware do nothing </li></ul><ul><li>calc_time.ru </li></ul><ul><li>add response time to body </li></ul><ul><li>method_override.ru </li></ul><ul><li>alter HTTP method </li></ul>
  8. 8. sinatra http://www.sinatrarb.com/
  9. 9. sinatra program: infinity.rb <ul><li>1 require 'rubygems' </li></ul><ul><li>2 require 'sinatra' </li></ul><ul><li>  3 </li></ul><ul><li>  4 use Rack::CommonLogger </li></ul><ul><li>  5 use Rack::ShowExceptions </li></ul><ul><li>  6 </li></ul><ul><li>  7 get '/' do </li></ul><ul><li>  8   &quot;Hello, Sinatra&quot; </li></ul><ul><li>  9 end </li></ul><ul><li>10 </li></ul><ul><li>11 get '/version' do </li></ul><ul><li>12   &quot;infinity 0.1&quot; </li></ul><ul><li>13 end </li></ul><ul><li>14 </li></ul><ul><li>15 get '/version/last' do </li></ul><ul><li>16   &quot;infinity beta 0.13&quot; </li></ul><ul><li>17 end </li></ul>
  10. 10. sequel The Database Toolkit for Ruby sequel postgres://localhost/blog Your database is stored in DB... >> DB.tables => [:blogs, :tags] >> require 'logger' >> DB.logger = Logger.new(STDOUT) >> DB[:tags].filter(:count > 100).order( :name.desc).limit(5).all INFO -- : SELECT * FROM &quot;tags&quot; WHERE (&quot;count&quot; > 150) ORDER BY &quot;name&quot; DESC LIMIT 5 >> class Tag < Sequel::Model;end >> Tag.filter(:count > 150).order(:name.desc).limit(5).all
  11. 11. sinatra + sequel: a simple note ruby notes.rb Just ONE file to run!
  12. 12. Ruby off Rails <ul><li>jquery vs. prototype.js </li></ul><ul><li>jquery UI vs. script.aculo.us </li></ul><ul><li>extlib vs. ActiveSupport </li></ul><ul><li>sequel vs. ActiveRecord </li></ul><ul><li>sinatra vs. ActionPack </li></ul><ul><li>pony vs. ActionMailer </li></ul><ul><li>rest-client vs. ActiveResource </li></ul>
  13. 13. Off Rails if you ... <ul><li>Hate the namespace pollution </li></ul><ul><ul><li>Prototype.js </li></ul></ul><ul><ul><li>ActiveSupport </li></ul></ul><ul><ul><li>ActiveRecord pollutes collection </li></ul></ul><ul><li>Want to use a dispatcher based on URL </li></ul><ul><li>Hate the constraints </li></ul><ul><ul><li>Constraint of program structure </li></ul></ul><ul><ul><li>Constraint of table structure </li></ul></ul><ul><ul><li>Constraint of URL structure </li></ul></ul><ul><li>Want to speed up </li></ul><ul><ul><li>Speed up upload </li></ul></ul><ul><ul><li>Speed up performance </li></ul></ul><ul><ul><li>Speed up test performance </li></ul></ul>
  14. 14. On Rails if you ... <ul><li>Need mature plug-ins </li></ul><ul><li>Need write-through cache(with support from Cache-money) </li></ul><ul><li>Need existing view helpers </li></ul><ul><li>Need help from community </li></ul>
  15. 15. Rails metal: new in 2.3 <ul><li>If you have a Rails application that has service end </li></ul><ul><li>points that need to be really, really fast. So fast that </li></ul><ul><li>the few milliseconds that a trip through the Rails router </li></ul><ul><li>and Action Controller path is too much. </li></ul><ul><li>For this scenario, we’ve built a thin wrapper around </li></ul><ul><li>the generic Rack middleware and given it a place in </li></ul><ul><li>the hierarchy along with the name “ Metal ” . </li></ul><ul><li>Introducing Rails Metal </li></ul><ul><li>—— DHH </li></ul>
  16. 16. Rails Metal: the code <ul><li>“Hello World” Metal: </li></ul><ul><li>class Poller < Rails::Rack::Metal </li></ul><ul><li>def call(env) </li></ul><ul><li>if env[&quot;PATH_INFO&quot;] =~ /^/poller/ </li></ul><ul><li>[200, {&quot;Content-Type&quot; => &quot;text/html&quot;}, &quot;Hello, World!&quot;] </li></ul><ul><li>else </li></ul><ul><li>[404, {&quot;Content-Type&quot; => &quot;text/html&quot;}, &quot;Not Found&quot;] </li></ul><ul><li>end </li></ul><ul><li>end </li></ul><ul><li>end </li></ul><ul><li>“ Hello World” controller: </li></ul><ul><li>class OldPollerController < ApplicationController </li></ul><ul><li>def poller </li></ul><ul><li>render :text => &quot;Hello World!&quot; </li></ul><ul><li>end </li></ul><ul><li>end </li></ul>
  17. 17. Rails Metal and sinatra <ul><li>1 require 'sinatra/base' </li></ul><ul><li>2 </li></ul><ul><li>3 class Hello < Sinatra::Base </li></ul><ul><li>4 # all options are available for the setting: </li></ul><ul><li>5 enable :static, :session </li></ul><ul><li>6 set :root, File.dirname(__FILE__) </li></ul><ul><li>7 </li></ul><ul><li>8 # each subclass has its own private middleware stack: </li></ul><ul><li>9 #use Rack::Deflater </li></ul><ul><li>10 </li></ul><ul><li>11 get '/sinatra' do </li></ul><ul><li>12 &quot;Hello, sinatra&quot; </li></ul><ul><li>13 end </li></ul><ul><li>14 end </li></ul>
  18. 18. Speed contrast: sequel and metal <ul><li>Ubuntu 8.04, Intel Atom N270 @1.60GHz, 100 requests, </li></ul><ul><li>development mode </li></ul><ul><li>Traditional Rails MVC </li></ul><ul><li>Requests per second: 16.69 [#/sec] (mean) </li></ul><ul><li>Time per request: 59.923 [ms] (mean) </li></ul><ul><li>Rails Metal </li></ul><ul><li>Requests per second: 71.78 [#/sec] (mean) </li></ul><ul><li>Time per request: 13.932 [ms] (mean) </li></ul><ul><li>Sinatra standalone ( 3 times of Rails Metal! ) </li></ul><ul><li>Requests per second: 208.39 [#/sec] (mean) </li></ul><ul><li>Time per request: 4.799 [ms] (mean) </li></ul>
  19. 19. Questions?
  20. 20. Thank you! <ul><li>http://masterwujiang.javaeye.com/ </li></ul><ul><li>my blog (Chinese only) </li></ul><ul><li>http://github.com/nouse/ruby-off-rails/tree/master </li></ul><ul><li>whole ruby files running in this presentation </li></ul><ul><li>Jiang Wu </li></ul><ul><li>Shanghai on Rails </li></ul><ul><li>2009.3.14 </li></ul>

×