Browse Source

Added grape micro framework

Richard Nienaber 11 years ago
parent
commit
1086fa02aa

+ 1 - 0
.travis.yml

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


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


+ 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

+ 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 8080
+
+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

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

@@ -0,0 +1,33 @@
+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(os.path.dirname(os.path.realpath(__file__)), 'config/database.yml')
+  setup_util.replace_text(database_file, "  host:.*", "  host: " + database_host)
+
+def run(commands, logfile, errfile):
+  cwd = os.path.expanduser(os.path.basename(os.path.normpath(os.path.dirname(os.path.realpath(__file__)))))
+  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

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

@@ -0,0 +1,3 @@
+#!/bin/bash
+
+fw_depends java ruby

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

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

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

@@ -0,0 +1,14 @@
+import helper
+from helper import Command
+
+def start(args, logfile, errfile):
+  helper.set_database_host(args)
+  commands = [
+    Command("rvm ruby-2.0.0-p0 do bundle", True),
+    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(commands, logfile, errfile)
+
+def stop(logfile, errfile):
+  return helper.stop('puma', logfile, errfile)

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

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

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

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

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

@@ -0,0 +1,14 @@
+import helper
+from helper import Command
+
+def start(args, logfile, errfile):
+  helper.set_database_host(args)
+  commands = [
+    Command("rvm jruby-1.7.8 do bundle", True),
+    Command("rvm jruby-1.7.8 do bundle exec trinidad --config config/trinidad.yml", False)
+  ]
+
+  return helper.run(commands, logfile, errfile)
+
+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("rvm ruby-2.0.0-p0 do bundle", 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)
+
+def stop(logfile, errfile):
+  return helper.stop('unicorn', logfile, errfile)

+ 1 - 0
frameworks/Ruby/grape/source_code

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