Browse Source

Clone sinatra into sinatra-sequel

Mike Pastore 9 years ago
parent
commit
6a9aec2313

+ 24 - 0
frameworks/Ruby/sinatra-sequel/Gemfile

@@ -0,0 +1,24 @@
+source 'http://rubygems.org'
+
+platforms :jruby do
+  gem "activerecord-jdbcmysql-adapter", "~> 1.3.9", :require => false
+  gem 'torqbox', '0.1.7'
+  gem "trinidad", '1.4.6'
+end
+
+platforms :ruby do
+  gem 'mysql2', '0.3.16'
+  gem "unicorn", '4.8.3'
+  gem "thin", '1.6.2'
+end
+
+gem "puma", '2.9.0'
+
+gem "activerecord-import", '0.5.0'
+gem 'activerecord', '4.1.4', :require => 'active_record'
+
+gem "sinatra-activerecord", '2.0.2'
+gem 'sinatra', '1.4.5'
+gem 'sinatra-contrib', '1.4.2'
+
+gem 'slim', '2.0.3'

+ 46 - 0
frameworks/Ruby/sinatra-sequel/README.md

@@ -0,0 +1,46 @@
+# Ruby [Sinatra](http://www.sinatrarb.com/) Benchmarking Test
+
+The information below contains information specific to Sinatra. 
+For further guidance, review the 
+[documentation](http://frameworkbenchmarks.readthedocs.org/en/latest/). 
+Also note the additional information provided in the [Ruby README](../).
+
+This is the Ruby Sinatra portion of a [benchmarking test suite](../../) 
+comparing a variety of web platforms.
+
+## Infrastructure Software Versions
+The tests were run with:
+* [Ruby 2.0.0-p0](http://www.ruby-lang.org/)
+* [JRuby 1.7.8](http://jruby.org/)
+* [Rubinius 2.2.10](http://rubini.us/)
+* [Sinatra 1.3.4](http://www.sinatrarb.com/)
+* [Unicorn 4.6.2](http://unicorn.bogomips.org/)
+* [TorqBox 0.1.7](http://torquebox.org/torqbox/)
+* [Puma 2.9.0](http://puma.io/)
+* [Thin 1.6.2](http://code.macournoyer.com/thin/)
+* [MySQL 5.5.29](https://dev.mysql.com/)
+
+## Paths & Source for Tests
+
+* [JSON Serialization](hello_world.rb): "/json"
+* [Single Database Query](hello_world.rb): "/db"
+* [Multiple Database Queries](hello_world.rb): "/db?queries={#}"
+* [Fortunes](hello_world.rb): "/fortune"
+* [Database Updates](hello_world.rb): "/update?queries={#}"
+* [Plaintext](hello_world.rb): "/plaintext"
+
+## Get Help
+
+### Experts
+
+_No experts listed, yet. If you're an expert, add yourself!_
+
+### Community
+
+* [Sinatra Google Group](https://groups.google.com/forum/#!forum/sinatrarb)
+* `#sinatra` IRC Channel ([irc.freenode.net](http://freenode.net/))
+
+### Resources
+
+* [Sinatra Source Code](https://github.com/sinatra/sinatra)
+* [PR: passenger-install-apache2-module doesn't work on ruby 2.0](https://github.com/FooBarWidget/passenger/pull/71)

+ 166 - 0
frameworks/Ruby/sinatra-sequel/benchmark_config.json

@@ -0,0 +1,166 @@
+{
+  "framework": "sinatra",
+  "tests": [{
+    "default": {
+      "setup_file": "run_mri_puma",
+      "json_url": "/json",
+      "db_url": "/db",
+      "query_url": "/queries?queries=",
+      "fortune_url": "/fortunes",
+      "update_url": "/updates?queries=", 
+      "plaintext_url": "/plaintext",
+      "port": 8080,
+      "approach": "Realistic",
+      "classification": "Micro",
+      "database": "MySQL",
+      "framework": "sinatra",
+      "language": "Ruby",
+      "orm": "Full",
+      "platform": "Rack",
+      "webserver": "Puma",
+      "os": "Linux",
+      "database_os": "Linux",
+      "display_name": "sinatra-puma-mri",
+      "notes": "",
+      "versus": "rack-puma-mri"
+    },
+    "puma-jruby": {
+      "setup_file": "run_jruby_puma",
+      "json_url": "/json",
+      "db_url": "/db",
+      "query_url": "/queries?queries=",
+      "fortune_url": "/fortunes",
+      "update_url": "/updates?queries=", 
+      "plaintext_url": "/plaintext",
+      "port": 8080,
+      "approach": "Realistic",
+      "classification": "Micro",
+      "database": "MySQL",
+      "framework": "sinatra",
+      "language": "Ruby",
+      "orm": "Full",
+      "platform": "JRuby",
+      "webserver": "Puma",
+      "os": "Linux",
+      "database_os": "Linux",
+      "display_name": "sinatra-puma-jruby",
+      "notes": "",
+      "versus": "rack-puma-jruby"
+    },
+    "puma-rbx": {
+      "setup_file": "run_rbx_puma",
+      "json_url": "/json",
+      "db_url": "/db",
+      "query_url": "/queries?queries=",
+      "fortune_url": "/fortunes",
+      "update_url": "/updates?queries=", 
+      "plaintext_url": "/plaintext",
+      "port": 8080,
+      "approach": "Realistic",
+      "classification": "Micro",
+      "database": "MySQL",
+      "framework": "sinatra",
+      "language": "Ruby",
+      "orm": "Full",
+      "platform": "Rubinius",
+      "webserver": "Puma",
+      "os": "Linux",
+      "database_os": "Linux",
+      "display_name": "sinatra-puma-rbx",
+      "notes": "",
+      "versus": "rack-puma-rbx"
+    },
+    "thin": {
+      "setup_file": "run_thin",
+      "json_url": "/json",
+      "db_url": "/db",
+      "query_url": "/queries?queries=",
+      "fortune_url": "/fortunes",
+      "update_url": "/updates?queries=", 
+      "plaintext_url": "/plaintext",
+      "port": 8080,
+      "approach": "Realistic",
+      "classification": "Micro",
+      "database": "MySQL",
+      "framework": "sinatra",
+      "language": "Ruby",
+      "orm": "Full",
+      "platform": "Rack",
+      "webserver": "Thin",
+      "os": "Linux",
+      "database_os": "Linux",
+      "display_name": "sinatra-thin",
+      "notes": "",
+      "versus": "rack-thin"
+    },
+    "torqbox-jruby": {
+      "setup_file": "run_torqbox",
+      "json_url": "/json",
+      "db_url": "/db",
+      "query_url": "/queries?queries=",
+      "fortune_url": "/fortunes",
+      "update_url": "/updates?queries=", 
+      "plaintext_url": "/plaintext",
+      "port": 8080,
+      "approach": "Realistic",
+      "classification": "Micro",
+      "database": "MySQL",
+      "framework": "sinatra",
+      "language": "Ruby",
+      "orm": "Full",
+      "platform": "JRuby",
+      "webserver": "TorqBox",
+      "os": "Linux",
+      "database_os": "Linux",
+      "display_name": "sinatra-torqbox-jruby",
+      "notes": "",
+      "versus": "rack-torqbox-jruby"
+    },
+    "trinidad-jruby": {
+      "setup_file": "run_trinidad",
+      "json_url": "/json",
+      "db_url": "/db",
+      "query_url": "/queries?queries=",
+      "fortune_url": "/fortunes",
+      "update_url": "/updates?queries=", 
+      "plaintext_url": "/plaintext",
+      "port": 8080,
+      "approach": "Realistic",
+      "classification": "Micro",
+      "database": "MySQL",
+      "framework": "sinatra",
+      "language": "Ruby",
+      "orm": "Full",
+      "platform": "JRuby",
+      "webserver": "Trinidad",
+      "os": "Linux",
+      "database_os": "Linux",
+      "display_name": "sinatra-trinidad-jruby",
+      "notes": "",
+      "versus": "rack-trinidad-jruby"
+    },
+    "unicorn": {
+      "setup_file": "run_unicorn",
+      "json_url": "/json",
+      "db_url": "/db",
+      "query_url": "/queries?queries=",
+      "fortune_url": "/fortunes",
+      "update_url": "/updates?queries=", 
+      "plaintext_url": "/plaintext",
+      "port": 8080,
+      "approach": "Realistic",
+      "classification": "Micro",
+      "database": "MySQL",
+      "framework": "sinatra",
+      "language": "Ruby",
+      "orm": "Full",
+      "platform": "Rack",
+      "webserver": "Unicorn",
+      "os": "Linux",
+      "database_os": "Linux",
+      "display_name": "sinatra-unicorn",
+      "notes": "",
+      "versus": "rack-unicorn"
+    }    
+  }]
+}

+ 2 - 0
frameworks/Ruby/sinatra-sequel/config.ru

@@ -0,0 +1,2 @@
+require './hello_world'
+run Sinatra::Application

+ 158 - 0
frameworks/Ruby/sinatra-sequel/config/nginx.conf

@@ -0,0 +1,158 @@
+# This is example contains the bare mininum to get nginx going with
+# Unicorn or Rainbows! servers.  Generally these configuration settings
+# are applicable to other HTTP application servers (and not just Ruby
+# ones), so if you have one working well for proxying another app
+# server, feel free to continue using it.
+#
+# The only setting we feel strongly about is the fail_timeout=0
+# directive in the "upstream" block.  max_fails=0 also has the same
+# effect as fail_timeout=0 for current versions of nginx and may be
+# used in its place.
+#
+# Users are strongly encouraged to refer to nginx documentation for more
+# details and search for other example configs.
+
+# you generally only need one nginx worker unless you're serving
+# large amounts of static files which require blocking disk reads
+worker_processes 8;
+
+# # drop privileges, root is needed on most systems for binding to port 80
+# # (or anything < 1024).  Capability-based security may be available for
+# # your system and worth checking out so you won't need to be root to
+# # start nginx to bind on 80
+# user nobody nogroup; # for systems with a "nogroup"
+#user nobody nobody; # for systems with "nobody" as a group instead
+
+# Feel free to change all paths to suite your needs here, of course
+pid /tmp/nginx.pid;
+#error_log /tmp/nginx.error.log;
+error_log /dev/null error;
+
+events {
+  worker_connections 4096; # increase if you have lots of clients
+  accept_mutex off; # "on" if nginx worker_processes > 1
+  use epoll; # enable for Linux 2.6+
+  # use kqueue; # enable for FreeBSD, OSX
+}
+
+http {
+  # nginx will find this file in the config directory set at nginx build time
+  include /usr/local/nginx/conf/mime.types;
+
+  # fallback in case we can't determine a type
+  default_type application/octet-stream;
+
+  # click tracking!
+  #access_log /tmp/nginx.access.log combined;
+  access_log off;
+
+  # you generally want to serve static files with nginx since neither
+  # Unicorn nor Rainbows! is optimized for it at the moment
+  sendfile on;
+
+  tcp_nopush on; # off may be better for *some* Comet/long-poll stuff
+  tcp_nodelay off; # on may be better for some Comet/long-poll stuff
+
+  # we haven't checked to see if Rack::Deflate on the app server is
+  # faster or not than doing compression via nginx.  It's easier
+  # to configure it all in one place here for static files and also
+  # to disable gzip for clients who don't get gzip/deflate right.
+  # There are other gzip settings that may be needed used to deal with
+  # bad clients out there, see http://wiki.nginx.org/NginxHttpGzipModule
+  #gzip on;
+  #gzip_http_version 1.0;
+  #gzip_proxied any;
+  #gzip_min_length 500;
+  #gzip_disable "MSIE [1-6]\.";
+  #gzip_types text/plain text/html text/xml text/css
+  #           text/comma-separated-values
+  #           text/javascript application/x-javascript
+  #           application/atom+xml;
+
+  # this can be any application server, not just Unicorn/Rainbows!
+  upstream app_server {
+    # fail_timeout=0 means we always retry an upstream even if it failed
+    # to return a good HTTP response (in case the Unicorn master nukes a
+    # single worker for timing out).
+
+    # for UNIX domain socket setups:
+    server unix:/tmp/.sock fail_timeout=0;
+
+    # for TCP setups, point these to your backend servers
+    # server 192.168.0.7:8080 fail_timeout=0;
+    # server 192.168.0.8:8080 fail_timeout=0;
+    # server 192.168.0.9:8080 fail_timeout=0;
+  }
+
+  server {
+    # enable one of the following if you're on Linux or FreeBSD
+    listen 8080 default deferred; # for Linux
+    # listen 80 default accept_filter=httpready; # for FreeBSD
+
+    # If you have IPv6, you'll likely want to have two separate listeners.
+    # One on IPv4 only (the default), and another on IPv6 only instead
+    # of a single dual-stack listener.  A dual-stack listener will make
+    # for ugly IPv4 addresses in $remote_addr (e.g ":ffff:10.0.0.1"
+    # instead of just "10.0.0.1") and potentially trigger bugs in
+    # some software.
+    # listen [::]:80 ipv6only=on; # deferred or accept_filter recommended
+
+    client_max_body_size 4G;
+    server_name _;
+
+    # ~2 seconds is often enough for most folks to parse HTML/CSS and
+    # retrieve needed images/icons/frames, connections are cheap in
+    # nginx so increasing this is generally safe...
+    keepalive_timeout 10;
+
+    # path for static files
+    root /path/to/app/current/public;
+
+    # Prefer to serve static files directly from nginx to avoid unnecessary
+    # data copies from the application server.
+    #
+    # try_files directive appeared in in nginx 0.7.27 and has stabilized
+    # over time.  Older versions of nginx (e.g. 0.6.x) requires
+    # "if (!-f $request_filename)" which was less efficient:
+    # http://bogomips.org/unicorn.git/tree/examples/nginx.conf?id=v3.3.1#n127
+    try_files $uri/index.html $uri.html $uri @app;
+
+    location @app {
+      # an HTTP header important enough to have its own Wikipedia entry:
+      #   http://en.wikipedia.org/wiki/X-Forwarded-For
+      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
+
+      # enable this if you forward HTTPS traffic to unicorn,
+      # this helps Rack set the proper URL scheme for doing redirects:
+      # proxy_set_header X-Forwarded-Proto $scheme;
+
+      # pass the Host: header from the client right along so redirects
+      # can be set properly within the Rack application
+      proxy_set_header Host $http_host;
+
+      # we don't want nginx trying to do something clever with
+      # redirects, we set the Host: header above already.
+      proxy_redirect off;
+
+      # set "proxy_buffering off" *only* for Rainbows! when doing
+      # Comet/long-poll/streaming.  It's also safe to set if you're using
+      # only serving fast clients with Unicorn + nginx, but not slow
+      # clients.  You normally want nginx to buffer responses to slow
+      # clients, even with Rails 3.1 streaming because otherwise a slow
+      # client can become a bottleneck of Unicorn.
+      #
+      # The Rack application may also set "X-Accel-Buffering (yes|no)"
+      # in the response headers do disable/enable buffering on a
+      # per-response basis.
+      # proxy_buffering off;
+
+      proxy_pass http://app_server;
+    }
+
+    # Rails error pages
+    error_page 500 502 503 504 /500.html;
+    location = /500.html {
+      root /path/to/app/current/public;
+    }
+  }
+}

+ 3 - 0
frameworks/Ruby/sinatra-sequel/config/puma.rb

@@ -0,0 +1,3 @@
+environment 'production'
+threads 8, 32
+bind 'tcp://0.0.0.0:8080'

+ 10 - 0
frameworks/Ruby/sinatra-sequel/config/thin.yml

@@ -0,0 +1,10 @@
+--- 
+timeout: 30
+wait: 30
+max_conns: 1024
+max_persistent_conns: 512
+environment: production
+port: 8080
+servers: 8
+log: /tmp/thin.log
+quiet: true

+ 8 - 0
frameworks/Ruby/sinatra-sequel/config/trinidad.yml

@@ -0,0 +1,8 @@
+---
+  port: 8080
+  threadsafe: true
+  environment: production
+  http:
+    address: '*'
+  logging:
+    level: SEVERE

+ 7 - 0
frameworks/Ruby/sinatra-sequel/config/unicorn.rb

@@ -0,0 +1,7 @@
+worker_processes 8
+listen "/tmp/.sock", :backlog => 256
+preload_app true
+GC.respond_to?(:copy_on_write_friendly=) and GC.copy_on_write_friendly = true
+
+before_fork { |server, worker| }
+after_fork { |server, worker| }

+ 91 - 0
frameworks/Ruby/sinatra-sequel/hello_world.rb

@@ -0,0 +1,91 @@
+require 'active_record'
+Bundler.require :default
+
+set :logging, false
+ActiveRecord::Base.logger = nil
+set :activerecord_logger, nil
+set :static, false
+set :template_engine, :slim
+Slim::Engine.set_default_options format: :html5, sort_attrs: false
+
+# Specify the encoder - otherwise, sinatra/json inefficiently
+# attempts to load one of several on each request
+set :json_encoder => :to_json
+
+# Don't prefix JSON results with { "world": {...} }
+ActiveRecord::Base.include_root_in_json = false
+
+db_config = { :database => 'hello_world', :username => 'benchmarkdbuser', :password => 'benchmarkdbpass', :pool => 256, :timeout => 5000 }
+adapter = RUBY_PLATFORM == 'java' ? 'jdbcmysql' : 'mysql2'
+set :database, db_config.merge(:adapter => adapter, :host => ENV['DB_HOST'])
+
+# The sinatra-activerecord gem registers before and after filters that
+# call expensive synchronized ActiveRecord methods on every request to
+# verify every connection in the pool, even for routes that don't use
+# the database. Clear those filters and handle connection management
+# ourselves, which is what applications seeking high throughput with
+# ActiveRecord need to do anyway.
+settings.filters[:before].clear
+settings.filters[:after].clear
+
+class World < ActiveRecord::Base
+  self.table_name = "World"
+end
+
+class Fortune < ActiveRecord::Base
+  self.table_name = "Fortune"
+end
+
+get '/json' do
+  json :message => 'Hello, World!'
+end
+
+get '/plaintext' do
+  content_type 'text/plain'
+  'Hello, World!'
+end
+
+get '/db' do
+  worlds = ActiveRecord::Base.connection_pool.with_connection do
+    World.find(Random.rand(10000) + 1)
+  end
+  json(worlds)
+end
+
+get '/queries' do
+  queries = (params[:queries] || 1).to_i
+  queries = 1 if queries < 1
+  queries = 500 if queries > 500
+
+  worlds = ActiveRecord::Base.connection_pool.with_connection do
+    (1..queries).map do
+      World.find(Random.rand(10000) + 1)
+    end
+  end
+  json(worlds)
+end
+
+get '/fortunes' do
+  @fortunes = Fortune.all
+  @fortunes << Fortune.new(:id => 0, :message => "Additional fortune added at request time.")
+  @fortunes = @fortunes.sort_by { |x| x.message }
+
+  slim :fortunes
+end
+
+get '/updates' do
+  queries = (params[:queries] || 1).to_i
+  queries = 1 if queries < 1
+  queries = 500 if queries > 500
+
+  worlds = ActiveRecord::Base.connection_pool.with_connection do
+    worlds = (1..queries).map do
+      world = World.find(Random.rand(10000) + 1)
+      world.randomNumber = Random.rand(10000) + 1
+      world
+    end
+    World.import worlds, :on_duplicate_key_update => [:randomNumber]
+    worlds
+  end
+  json(worlds)
+end

+ 0 - 0
frameworks/Ruby/sinatra-sequel/public/.gitkeep


+ 1 - 0
frameworks/Ruby/sinatra-sequel/public/500.html

@@ -0,0 +1 @@
+ 

+ 9 - 0
frameworks/Ruby/sinatra-sequel/run_jruby_puma.sh

@@ -0,0 +1,9 @@
+#!/bin/bash
+
+fw_depends rvm java7 jruby-1.7.8
+
+rvm jruby-1.7.8 do bundle install --gemfile=$TROOT/Gemfile
+
+rvm jruby-1.7.8 do bundle --jobs 4
+
+DB_HOST=${DBHOST} rvm jruby-1.7.8 do bundle exec puma -C config/puma.rb &

+ 9 - 0
frameworks/Ruby/sinatra-sequel/run_mri_puma.sh

@@ -0,0 +1,9 @@
+#!/bin/bash
+
+fw_depends rvm ruby-2.0.0
+
+rvm jruby-1.7.8 do bundle install --gemfile=$TROOT/Gemfile
+
+rvm ruby-2.0.0-p0 do bundle --jobs 4
+
+DB_HOST=${DBHOST} rvm ruby-2.0.0-p0 do bundle exec puma -C config/puma.rb -w 8 --preload &

+ 7 - 0
frameworks/Ruby/sinatra-sequel/run_rbx_puma.sh

@@ -0,0 +1,7 @@
+#!/bin/bash
+
+fw_depends rvm rbx-2.2.10
+
+rvm rbx-2.2.10 do bundle install --gemfile=$TROOT/Gemfile
+
+DB_HOST=${DBHOST} rvm rbx-2.2.10 do bundle exec puma &

+ 9 - 0
frameworks/Ruby/sinatra-sequel/run_thin.sh

@@ -0,0 +1,9 @@
+#!/bin/bash
+
+fw_depends rvm ruby-2.0.0
+
+rvm ruby-2.0.0-p0 do bundle install --gemfile=$TROOT/Gemfile --path=vendor/bundle
+
+rvm ruby-2.0.0-p0 do bundle --jobs 4
+
+DB_HOST=${DBHOST} rvm ruby-2.0.0-p0 do bundle exec thin start -C config/thin.yml &

+ 9 - 0
frameworks/Ruby/sinatra-sequel/run_torqbox.sh

@@ -0,0 +1,9 @@
+#!/bin/bash
+
+fw_depends rvm java7 jruby-1.7.8
+
+rvm jruby-1.7.8 do bundle install --gemfile=$TROOT/Gemfile
+
+rvm jruby-1.7.8 do bundle --jobs 4
+
+DB_HOST=${DBHOST} rvm jruby-1.7.8 do bundle exec torqbox -b 0.0.0.0 -E production &

+ 9 - 0
frameworks/Ruby/sinatra-sequel/run_trinidad.sh

@@ -0,0 +1,9 @@
+#!/bin/bash
+
+fw_depends rvm java7 jruby-1.7.8
+
+rvm jruby-1.7.8 do bundle install --gemfile=$TROOT/Gemfile
+
+rvm jruby-1.7.8 do bundle --jobs 4
+
+DB_HOST=${DBHOST} rvm jruby-1.7.8 do bundle exec trinidad --config config/trinidad.yml &

+ 13 - 0
frameworks/Ruby/sinatra-sequel/run_unicorn.sh

@@ -0,0 +1,13 @@
+#!/bin/bash
+
+fw_depends rvm ruby-2.0.0 nginx
+
+sed -i 's|/usr/local/nginx/|'"${IROOT}"'/nginx/|g' config/nginx.conf
+
+rvm ruby-2.0.0-p0 do bundle install --gemfile=$TROOT/Gemfile --path=vendor/bundle
+
+rvm ruby-2.0.0-p0 do bundle --jobs 4
+
+nginx -c $TROOT/config/nginx.conf
+
+DB_HOST=${DBHOST} rvm ruby-2.0.0-p0 do bundle exec unicorn -E production -c config/unicorn.rb &

+ 1 - 0
frameworks/Ruby/sinatra-sequel/source_code

@@ -0,0 +1 @@
+./hello_world.rb

+ 8 - 0
frameworks/Ruby/sinatra-sequel/views/fortunes.slim

@@ -0,0 +1,8 @@
+table
+  tr
+    th id
+    th message
+  - for fortune in @fortunes
+    tr
+      td =fortune.id
+      td =fortune.message

+ 6 - 0
frameworks/Ruby/sinatra-sequel/views/layout.slim

@@ -0,0 +1,6 @@
+doctype html
+html
+  head
+    title Fortunes
+  body
+    == yield