Browse Source

Merge pull request #1039 from rjnienaber/grape_framework

Ruby: Added grape micro framework
Hamilton Turner 11 years ago
parent
commit
a2b0dc7b49

+ 1 - 0
.travis.yml

@@ -133,6 +133,7 @@ env:
     - "TESTDIR=Python/uwsgi"
     - "TESTDIR=Python/wsgi"
     - "TESTDIR=Racket/racket-ws"
+    - "TESTDIR=Ruby/grape"
     - "TESTDIR=Ruby/rack"
     - "TESTDIR=Ruby/rails"
     - "TESTDIR=Ruby/rails-stripped"

+ 20 - 0
frameworks/Ruby/grape/Gemfile

@@ -0,0 +1,20 @@
+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 'activerecord', '4.1.4', :require => 'active_record'
+gem "activerecord-import", "~> 0.5.0"
+gem "grape", "0.8.0"
+gem 'rack', '1.5.2'
+gem 'json', '1.8.1'
+gem "puma", "~> 2.9.0"

+ 15 - 0
frameworks/Ruby/grape/README.md

@@ -0,0 +1,15 @@
+# Ruby Grape Benchmarking Test
+
+This is the Ruby Grape portion of a [benchmarking test suite](../) comparing a variety of web servers along with JRuby/MRI.
+
+## 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/)
+* [Grape 0.8.0](http://intridea.github.io/grape/)
+* [Rack 1.5.2](http://rack.github.com/)
+* [Unicorn 4.8.3](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/)

+ 0 - 0
frameworks/Ruby/grape/__init__.py


+ 7 - 0
frameworks/Ruby/grape/bash_profile.sh

@@ -0,0 +1,7 @@
+#!/bin/bash
+
+# We assume single-user installation as 
+# done in our rvm.sh script and 
+# in Travis-CI
+source $HOME/.rvm/scripts/rvm
+

+ 137 - 0
frameworks/Ruby/grape/benchmark_config

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

+ 76 - 0
frameworks/Ruby/grape/config.ru

@@ -0,0 +1,76 @@
+require 'erb'
+require 'active_record'
+
+Bundler.require :default
+
+db_config = YAML.load(ERB.new(File.read("config/database.yml")).result)[ENV['RACK_ENV']]
+ActiveRecord::Base.establish_connection(db_config)
+
+class World < ActiveRecord::Base
+  self.table_name = "World"
+end
+
+module Acme
+  class HelloWorld < Grape::API
+    get '/json' do
+      {message:"Hello, World!"}
+    end
+  end
+
+  class PlainText < Grape::API
+    content_type :plain, "text/plain; charset=utf-8"
+    format :plain
+    get '/plaintext' do
+      "Hello, World!"
+    end
+  end
+
+  class DatabaseQueries < Grape::API
+    get '/db' do
+      ActiveRecord::Base.connection_pool.with_connection do
+        World.find(Random.rand(10000) + 1)
+      end
+    end
+
+    get '/query' do
+      queries = params[:queries].to_i
+      queries = 1 if queries < 1
+      queries = 500 if queries > 500
+
+      ActiveRecord::Base.connection_pool.with_connection do
+        results = (1..queries).map do
+          World.find(Random.rand(10000) + 1)
+        end
+      end
+    end
+
+    get '/updates' do
+      queries = params[:queries].to_i
+      queries = 1 if queries < 1
+      queries = 500 if queries > 500
+
+      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
+    end
+  end
+
+  class API < Grape::API
+    content_type :json, "application/json; charset=utf-8"
+    format :json
+    
+    mount ::Acme::HelloWorld
+    mount ::Acme::PlainText
+    mount ::Acme::DatabaseQueries
+  end
+end
+
+run Acme::API
+
+

+ 9 - 0
frameworks/Ruby/grape/config/database.yml

@@ -0,0 +1,9 @@
+production:
+  adapter: <%= RUBY_PLATFORM == 'java' ? 'jdbcmysql' : 'mysql2' %>
+  encoding: utf8
+  host: localhost
+  database: hello_world
+  username: benchmarkdbuser
+  password: benchmarkdbpass
+  pool: 256
+  timeout: 5000

+ 158 - 0
frameworks/Ruby/grape/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 stderr 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;
+    }
+  }
+}

+ 10 - 0
frameworks/Ruby/grape/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/grape/config/trinidad.yml

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

+ 11 - 0
frameworks/Ruby/grape/config/unicorn.rb

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

+ 32 - 0
frameworks/Ruby/grape/helper.py

@@ -0,0 +1,32 @@
+import os
+import subprocess
+from collections import namedtuple
+
+import setup_util
+
+Command = namedtuple('Command', ['command', 'wait_for_exit'])
+
+def set_database_host(args):
+  database_host = args.database_host or 'localhost'
+  database_file = os.path.join(args.troot, 'config', 'database.yml')
+  setup_util.replace_text(database_file, "  host:.*", "  host: " + database_host)
+
+def run(commands, logfile, errfile, cwd):
+  try:
+    for command in commands:      
+      if command.wait_for_exit:
+        subprocess.check_call(command.command, shell=True, cwd=cwd, stderr=errfile, stdout=logfile)
+      else:
+        subprocess.Popen(command.command, shell=True, cwd=cwd, stderr=errfile, stdout=logfile)
+  except subprocess.CalledProcessError:
+    return 1
+  return 0
+
+def stop(partial_command, logfile, errfile):
+  p = subprocess.Popen(['ps', 'aux'], stdout=subprocess.PIPE)
+  out, err = p.communicate()
+  for line in out.splitlines():
+    if partial_command in line and 'run-tests' not in line:
+      pid = int(line.split(None, 2)[1])
+      os.kill(pid, 15)
+  return 0

+ 9 - 0
frameworks/Ruby/grape/install.sh

@@ -0,0 +1,9 @@
+#!/bin/bash
+
+fw_depends rvm nginx java
+
+rvm install ruby-2.0.0-p0
+rvm ruby-2.0.0-p0 do bundle install --gemfile=$TROOT/Gemfile
+
+rvm install jruby-1.7.8
+rvm jruby-1.7.8 do bundle install --gemfile=$TROOT/Gemfile

+ 10 - 0
frameworks/Ruby/grape/run_jruby_puma.py

@@ -0,0 +1,10 @@
+import helper
+from helper import Command
+
+def start(args, logfile, errfile):
+  helper.set_database_host(args)
+  command = Command("rvm jruby-1.7.8 do bundle exec puma -b tcp://0.0.0.0:8080 -e production", False)
+  return helper.run([command], logfile, errfile, args.troot)
+
+def stop(logfile, errfile):
+  return helper.stop('puma', logfile, errfile)

+ 10 - 0
frameworks/Ruby/grape/run_mri_puma.py

@@ -0,0 +1,10 @@
+import helper
+from helper import Command
+
+def start(args, logfile, errfile):
+  helper.set_database_host(args)
+  command = Command("rvm ruby-2.0.0-p0 do bundle exec puma -t 8:32 -w 8 --preload -b tcp://0.0.0.0:8080 -e production", False)
+  return helper.run([command], logfile, errfile, args.troot)
+
+def stop(logfile, errfile):
+  return helper.stop('puma', logfile, errfile)

+ 11 - 0
frameworks/Ruby/grape/run_thin.py

@@ -0,0 +1,11 @@
+import helper
+from helper import Command
+
+def start(args, logfile, errfile):
+  helper.set_database_host(args)
+  command = Command("rvm ruby-2.0.0-p0 do bundle exec thin start -C config/thin.yml", False)
+  return helper.run([command], logfile, errfile, args.troot)
+
+def stop(logfile, errfile):
+  helper.run([Command('rm -rf tmp/*', True)], logfile, errfile, '.')  
+  return helper.stop('thin', logfile, errfile)

+ 10 - 0
frameworks/Ruby/grape/run_torqbox.py

@@ -0,0 +1,10 @@
+import helper
+from helper import Command
+
+def start(args, logfile, errfile):
+  helper.set_database_host(args)
+  command = Command("rvm jruby-1.7.8 do bundle exec torqbox -b 0.0.0.0 -E production", False)
+  return helper.run([command], logfile, errfile, args.troot)
+
+def stop(logfile, errfile):
+  return helper.stop('torqbox', logfile, errfile)

+ 10 - 0
frameworks/Ruby/grape/run_trinidad.py

@@ -0,0 +1,10 @@
+import helper
+from helper import Command
+
+def start(args, logfile, errfile):
+  helper.set_database_host(args)
+  command = Command("rvm jruby-1.7.8 do bundle exec trinidad --config config/trinidad.yml", False)
+  return helper.run([command], logfile, errfile, args.troot)
+
+def stop(logfile, errfile):
+  return helper.stop('trinidad', logfile, errfile)

+ 14 - 0
frameworks/Ruby/grape/run_unicorn.py

@@ -0,0 +1,14 @@
+import helper
+from helper import Command
+
+def start(args, logfile, errfile):
+  helper.set_database_host(args)
+  commands = [Command("sudo /usr/local/nginx/sbin/nginx -c $TROOT/config/nginx.conf", True),
+              Command("rvm ruby-2.0.0-p0 do bundle exec unicorn -E production -c config/unicorn.rb", False)]
+
+  return helper.run(commands, logfile, errfile, args.troot)
+
+def stop(logfile, errfile):
+  command = Command("sudo /usr/local/nginx/sbin/nginx -c $TROOT/config/nginx.conf -s stop ", True)
+  helper.run([command], logfile, errfile, '.')  
+  return helper.stop('unicorn', logfile, errfile)

+ 1 - 0
frameworks/Ruby/grape/source_code

@@ -0,0 +1 @@
+./config.ru