Browse Source

Merge pull request #1782 from mwpastore/master

Add sinatra-sequel benchmark
Nate 9 years ago
parent
commit
0a98beff63

+ 1 - 0
.travis.yml

@@ -157,6 +157,7 @@ env:
     - "TESTDIR=Ruby/rails"
     - "TESTDIR=Ruby/rails-stripped"
     - "TESTDIR=Ruby/sinatra"
+    - "TESTDIR=Ruby/sinatra-sequel"
     - "TESTDIR=Rust/iron"
     - "TESTDIR=Rust/nickel"
     - "TESTDIR=Rust/hyper"

+ 2 - 0
frameworks/Ruby/sinatra-sequel/.gitignore

@@ -0,0 +1,2 @@
+.bundle
+vendor/bundle

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

@@ -0,0 +1,15 @@
+source 'https://rubygems.org'
+
+platforms :ruby do
+  gem 'mysql2', '0.4.0'
+  gem 'json', '1.8.1', :require => 'json/ext'
+end
+
+gem 'puma', '2.15.3'
+
+gem 'sequel', '4.28.0'
+
+gem 'sinatra', '1.4.6', :require => 'sinatra/base'
+gem 'sinatra-contrib', '1.4.6', :require => 'sinatra/json'
+
+gem 'slim', '3.0.6'

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

@@ -0,0 +1,42 @@
+# 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.2.3](http://www.ruby-lang.org/)
+* [Sinatra 1.4.6](http://www.sinatrarb.com/)
+* [Puma 2.15.3](http://puma.io/)
+* [MySQL 5.5.29](https://dev.mysql.com/)
+* [Sequel 4.28.0](http://sequel.jeremyevans.net/documentation.html)
+
+## 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/))
+* `#sinatrarb` on the [Sinatra and Friends](http://sinatra-slack.herokuapp.com) Slack
+
+### Resources
+
+* [Sinatra Source Code](https://github.com/sinatra/sinatra)

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

@@ -0,0 +1,28 @@
+{
+  "framework": "sinatra-sequel",
+  "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-sequel",
+      "language": "Ruby",
+      "orm": "Full",
+      "platform": "Rack",
+      "webserver": "Puma",
+      "os": "Linux",
+      "database_os": "Linux",
+      "display_name": "sinatra-sequel-puma-mri",
+      "notes": "",
+      "versus": "rack-puma-mri"
+    }
+  }]
+}

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

@@ -0,0 +1,2 @@
+require_relative 'hello_world'
+run HelloWorld

+ 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;
+    }
+  }
+}

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

@@ -0,0 +1,116 @@
+MAX_PK = 10_000
+SEQUEL_NO_ASSOCIATIONS = true
+
+Bundler.require :default
+
+# Configure Slim templating engine
+Slim::Engine.set_options \
+  :format => :html,
+  :sort_attrs => false
+
+# Configure Sequel ORM
+DB = Sequel.connect \
+  :adapter => RUBY_PLATFORM == 'java' ? 'jdbc:mysql' : 'mysql2',
+  :host => ENV['DB_HOST'],
+  :database => 'hello_world',
+  :user => 'benchmarkdbuser',
+  :password => 'benchmarkdbpass',
+  :max_connections => 32, # == max worker threads per process
+  :pool_timeout => 5
+
+# Allow #to_json on models and arrays of models
+Sequel::Model.plugin :json_serializer
+
+class World < Sequel::Model(:World); end
+
+class Fortune < Sequel::Model(:Fortune)
+  # Allow setting id to zero (0) per benchmark requirements
+  unrestrict_primary_key
+end
+
+# Our Rack application to be executed by rackup
+class HelloWorld < Sinatra::Base
+  configure do
+    disable :protection
+
+    # Don't add ;charset= to any content types per the benchmark requirements
+    set :add_charset, []
+
+    # Specify the encoder - otherwise, sinatra/json inefficiently
+    # attempts to load one of several on each request
+    set :json_encoder, :to_json
+  end
+
+  helpers do
+    # Return a random number between 1 and MAX_PK
+    def rand1
+      Random.rand(MAX_PK) + 1
+    end
+
+    # Return an array of `n' unique random numbers between 1 and MAX_PK
+    def randn(n)
+      (1..MAX_PK).to_a.shuffle!.take(n)
+    end
+  end
+
+  after do
+    # Add mandatory HTTP headers to every response
+    response['Server'] = 'Puma'
+    response['Date'] = Time.now.to_s
+  end
+
+  get '/json' do
+    json :message => 'Hello, World!'
+  end
+
+  get '/plaintext' do
+    content_type 'text/plain'
+    'Hello, World!'
+  end
+
+  get '/db' do
+    json World[rand1]
+  end
+
+  get '/queries' do
+    queries = (params[:queries] || 1).to_i
+    queries = 1 if queries < 1
+    queries = 500 if queries > 500
+
+    json World.where(:id => randn(queries))
+  end
+
+  get '/fortunes' do
+    @fortunes = Fortune.all
+    @fortunes << Fortune.new(
+      :id => 0,
+      :message => 'Additional fortune added at request time.'
+    )
+    @fortunes.sort_by!(&:message)
+
+    slim :fortunes
+  end
+
+  get '/updates' do
+    queries = (params[:queries] || 1).to_i
+    queries = 1 if queries < 1
+    queries = 500 if queries > 500
+
+    # Prepare our updates in advance so transaction retries are idempotent
+    updates = randn(queries).sort!.map! { |id| [id, rand1] }
+
+    worlds = nil
+
+    World.db.transaction do
+      worlds = World
+        .where(:id => updates.transpose.first)
+        .for_update
+
+      World.dataset
+        .on_duplicate_key_update(:randomNumber)
+        .import([:id, :randomNumber], updates)
+    end
+
+    json worlds
+  end
+end

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


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

@@ -0,0 +1 @@
+ 

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

@@ -0,0 +1,10 @@
+#!/bin/bash
+
+MRI_VERSION=ruby-2.2.1
+
+fw_depends rvm $MRI_VERSION
+
+rvm $MRI_VERSION do bundle install --gemfile=$TROOT/Gemfile --path vendor/bundle
+
+DB_HOST=${DBHOST} \
+rvm $MRI_VERSION do bundle exec puma -w 8 -t 8:32 -b tcp://0.0.0.0:8080 -e production &

+ 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

+ 28 - 0
toolset/setup/linux/languages/ruby-2.2.1.sh

@@ -0,0 +1,28 @@
+#!/bin/bash
+
+fw_depends rvm
+
+VERSION=2.2.1
+RETCODE=$(fw_exists ${IROOT}/ruby-${VERSION}.installed)
+[ ! "$RETCODE" == 0 ] || { \
+  # Load environment variables
+  source $IROOT/ruby-$VERSION.installed
+  return 0; }
+
+# We assume single-user installation as 
+# done in our rvm.sh script and 
+# in Travis-CI
+if [ "$TRAVIS" = "true" ]
+then
+  rvmsudo rvm install $VERSION
+  # Bundler is SOMETIMES missing... not sure why.
+  rvmsudo rvm $VERSION do gem install bundler
+else
+  rvm install $VERSION
+  # Bundler is SOMETIMES missing... not sure why.
+  rvm $VERSION do gem install bundler
+fi
+
+echo "" > $IROOT/ruby-$VERSION.installed
+
+source $IROOT/ruby-$VERSION.installed