Bläddra i källkod

Merge pull request #1262 from kbrock/nginx_mruby

add ruby/ngx_mruby framework
Mike Smith 10 år sedan
förälder
incheckning
74bab3b1bd

+ 1 - 0
.travis.yml

@@ -125,6 +125,7 @@ env:
     - "TESTDIR=Python/wsgi"
     - "TESTDIR=Racket/racket-ws"
     - "TESTDIR=Ruby/grape"
+    - "TESTDIR=Ruby/ngx_mruby"
     - "TESTDIR=Ruby/padrino"
     - "TESTDIR=Ruby/rack"
     - "TESTDIR=Ruby/rails"

+ 21 - 0
frameworks/Ruby/ngx_mruby/README.md

@@ -0,0 +1,21 @@
+# [nginx + mruby](https://github.com/matsumoto-r/ngx_mruby) Benchmark Test
+
+The nginx app is distributes across a few files. most of it is in [nginx.conf](nginx.conf)
+The nginx conf is inside [nginx.conf](nginx.conf)
+Requires a nginx compiled with ngx_mruby module. It has been called nginx_mruby for lack of a better name
+
+
+## Test URLs
+### JSON Encoding
+
+http://localhost:8080/json
+
+### Single Row Random Query
+
+http://localhost:8080/db
+
+### Variable Row Query Test
+
+http://localhost:8080/db?queries=2
+
+Based upon the [openresty](../../Lua/openresty/) implementation.

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


+ 26 - 0
frameworks/Ruby/ngx_mruby/benchmark_config

@@ -0,0 +1,26 @@
+{
+  "framework": "ngx_mruby",
+  "tests": [{
+    "default": {
+      "setup_file": "setup",
+      "json_url": "/json",
+      "db_url": "/db",
+      "query_url": "/queries?queries=",
+      "plaintext_url": "/plaintext",
+      "port": 8080,
+      "approach": "Realistic",
+      "classification": "Micro",
+      "database": "MySQL",
+      "framework": "ngx_mruby",
+      "language": "Ruby",
+      "orm": "Raw",
+      "platform": "ngx_mruby",
+      "webserver": "nginx",
+      "os": "Linux",
+      "database_os": "Linux",
+      "display_name": "ngx_mruby",
+      "notes": "",
+      "versus": "ngx_mruby"
+    }
+  }]
+}

+ 8 - 0
frameworks/Ruby/ngx_mruby/db.rb

@@ -0,0 +1,8 @@
+# https://github.com/mattn/mruby-mysql/blob/master/example/example.rb
+db = Userdata.new("my_#{Process.pid}").db
+
+ret = nil
+db.execute("select * from World where id = ?", rand(10000)) do |row, fields|
+  ret = Hash[fields.zip(row)]
+end
+Nginx.rputs JSON::stringify(ret)

+ 20 - 0
frameworks/Ruby/ngx_mruby/install.sh

@@ -0,0 +1,20 @@
+#!/bin/bash
+
+PREFIX=${IROOT}/nginx_mruby
+
+RETCODE=$(fw_exists ${IROOT}/nginx_mruby.installed)
+[ ! "$RETCODE" == 0 ] || { return 0; }
+
+fw_depends rvm
+rvm install ruby-2.0.0-p0
+rvm use ruby-2.0.0-p0
+git clone git://github.com/matsumoto-r/ngx_mruby.git
+cd ngx_mruby
+git submodule init
+git submodule update
+[ -d mruby/mrbgems/mruby-mysql ] || git clone git://github.com/mattn/mruby-mysql.git mruby/mrbgems/mruby-mysql
+
+NGINX_CONFIG_OPT_ENV="--prefix=${PREFIX} --with-http_stub_status_module" sh build.sh
+make install
+
+touch ${IROOT}/nginx_mruby.installed

+ 52 - 0
frameworks/Ruby/ngx_mruby/nginx.conf

@@ -0,0 +1,52 @@
+pid        /tmp/nginx.pid;
+error_log stderr error;
+worker_processes auto;
+
+events {
+    worker_connections  16384;
+    multi_accept on;
+}
+
+http {
+    resolver 127.0.0.1;
+    access_log off;
+    mruby_init_worker_code 'Userdata.new("my_#{Process.pid}").db = MySQL::Database.new("DBHOSTNAME", "benchmarkdbuser", "benchmarkdbpass", "hello_world")';
+    mruby_exit_worker_code 'db = Userdata.new("my_#{Process.pid}").db ; db.close if db';
+    server {
+        listen       8080;
+        server_name  localhost;
+
+        location / {
+            root   html;
+            index  index.html index.htm;
+        }
+
+        location /plaintext {
+            default_type "text/plain";
+            mruby_content_handler_code 'Nginx.rputs "Hello, world!"';
+        }
+
+        location /json {
+            default_type "application/json";
+            mruby_content_handler_code 'Nginx.rputs JSON::stringify("message" => "Hello, World!")';
+        }
+
+        location ~ /db {
+            default_type "application/json";
+            #note: next line has cwd
+            mruby_content_handler 'CWD/db.rb' cache;
+        }
+
+        location ~ /queries {
+            default_type "application/json";
+            #note: next line has cwd
+            mruby_content_handler 'CWD/queries.rb' cache;
+        }
+
+#        location ~ /fortunes {
+#            default_type "application/html";
+#            #note: next line has cwd
+#            mruby_content_handler 'CWD/fortunes.rb' cache;
+#        }
+    }
+}

+ 27 - 0
frameworks/Ruby/ngx_mruby/queries.rb

@@ -0,0 +1,27 @@
+class World
+  def db
+    @db ||= Userdata.new("my_#{Process.pid}").db
+  end
+
+  def find(id)
+    ret = nil
+    db.execute("select * from World where id = ?", id) do |row, fields|
+      ret = Hash[fields.zip(row)]
+    end
+    ret
+  end
+
+  def save(world)
+    db.execute("update World set randomNumber = ? where id = ?", world["randomNumber"], world["randomNumber"])
+  end
+end
+
+r = Nginx::Request.new
+
+num_queries = r.var.arg_queries.to_i
+num_queries = 1 if num_queries < 1
+num_queries = 500 if num_queries > 500
+
+world = World.new
+ret = num_queries.times.map { world.find(rand(10000)) }
+Nginx.rputs JSON::stringify(ret)

+ 6 - 0
frameworks/Ruby/ngx_mruby/setup.sh

@@ -0,0 +1,6 @@
+#!/bin/bash
+
+sed -i 's|CWD|'"${TROOT}"'|g' nginx.conf
+sed -i 's|DBHOSTNAME|'"${DBHOST}"'|g' nginx.conf
+
+${IROOT}/nginx_mruby/sbin/nginx -c $TROOT/nginx.conf &

+ 4 - 0
frameworks/Ruby/ngx_mruby/source_code

@@ -0,0 +1,4 @@
+./ngx_mruby/db.rb
+./ngx_mruby/fortune.rb
+./ngx_mruby/queries.rb
+./ngx_mruby/updates.rb

+ 31 - 0
frameworks/Ruby/ngx_mruby/updates.rb

@@ -0,0 +1,31 @@
+r = Nginx::Request.new
+
+num_queries = r.var.arg_queries.to_i
+num_queries = 1 if num_queries < 0
+num_queries = 500 if num_queries > 500
+
+class Worlds
+  def db
+    @db ||= Userdata.new("my_#{Process.pid}").db
+  end
+
+  def find(id)
+    db.execute("select * from World where id = ?", id) do |row, fields|
+      return Hash[fields.zip(row)]
+    end
+  end
+
+  def save(world)
+    #TODO: consider execute_batch
+    db.execute("update World set randomNumber = ? where id = ?", world["randomNumber"], world["randomNumber"])
+  end
+end
+
+ret = num_queries.times.map { World.find(rand(10000)) }
+
+ret.each do |world|
+  world["randomNumber"] = rand(10000) + 1
+  World.save(world)
+end
+
+Nginx.rputs JSON::stringify(ret)

+ 1 - 0
toolset/setup/linux/prerequisites.sh

@@ -41,6 +41,7 @@ sudo apt-get -yq install -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::
   libjson0-dev libmcrypt-dev libicu-dev gettext \
   libpq-dev mlton \
   libjemalloc-dev                   `# Needed by lwan at least` \
+  libhiredis-dev                    `# Redis client - Needed by ngx_mruby at least` \
   cloc dstat                        `# Collect resource usage statistics`
 
 # Install gcc-4.8