Browse Source

inital take on nginx_mruby

Keenan Brock 10 years ago
parent
commit
5f50daf1a2

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

@@ -0,0 +1,21 @@
+# [nginx + mruby](https://github.com/matsumoto-r/ngx_mruby) Benchmark Test
+
+The nginx app is inside [app.rb](app.rb)
+The nginx conf is inside [nginx.conf](nginx.conf)
+Requires a nginx compiled with ngx_mruby module
+
+
+## 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/nginx_mruby/__init__.py


+ 8 - 0
frameworks/Ruby/nginx_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)

+ 15 - 0
frameworks/Ruby/nginx_mruby/fortune.rb

@@ -0,0 +1,15 @@
+r = Nginx::Request.new
+
+# https://github.com/mattn/mruby-mysql/blob/master/example/example.rb
+db = Userdata.new("my_#{Process.pid}").db
+
+fortunes = []
+db.execute("select * from Fortune") do |row, fields|
+  fortunes << Hash[fields.zip(row)]
+end
+fortunes << { "id" => 0, "message" => "Additional fortune added at request time." }
+
+fortunes = fortunes.sort_by { |x| x.message }
+
+#TODO: use "erb" to render template
+Nginx.rputs JSON::stringify(fortunes)

+ 4 - 0
frameworks/Ruby/nginx_mruby/install.sh

@@ -0,0 +1,4 @@
+#!/bin/bash
+
+#? m_ruby nginx
+fw_depends nginx_mruby

+ 72 - 0
frameworks/Ruby/nginx_mruby/nginx.conf

@@ -0,0 +1,72 @@
+#pid        /tmp/nginx.pid;
+
+#error_log  stderr error;
+
+
+events {
+    worker_connections  16384;
+}
+
+http {
+    resolver 127.0.0.1;
+    #access_log off;
+
+#        https://github.com/matsumoto-r/ngx_mruby/wiki/Class-and-Method#nginxrequest-class
+
+    # db connection pool
+    mruby_init_worker_code 'Userdata.new("my_#{Process.pid}").db = MySQL::Database.new("localhost", "root", "", "benchmark")';
+    mruby_exit_worker_code 'db = Userdata.new("my_#{Process.pid}").db ; db.close if db';
+    server {
+        listen       8000;
+        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";
+            mruby_content_handler '/usr/local/nginx-1.4.7mruby/html/db.rb' cache;
+        }
+
+        location ~ /queries {
+            default_type "application/json";
+            mruby_content_handler '/usr/local/nginx-1.4.7mruby/html/queries.rb' cache;
+        }
+
+        location ~ /fotunes {
+            default_type "application/html";
+            mruby_content_handler '/usr/local/nginx-1.4.7mruby/html/fotunes.rb' cache;
+        }
+
+
+
+        #location ~ \.rb$ {
+        #    default_type "application/json";
+        #    mruby_add_handler on;
+        #}
+
+        #error_page  404              /404.html;
+        error_page   500 502 503 504  /50x.html;
+        location = /50x.html {
+            root   html;
+        }
+
+        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
+        #
+        #location ~ \.php$ {
+        #    proxy_pass   http://127.0.0.1;
+        #}
+    }
+}

+ 4 - 0
frameworks/Ruby/nginx_mruby/source_code

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

+ 25 - 0
toolset/setup/linux/webservers/nginx_mruby.sh

@@ -0,0 +1,25 @@
+#!/bin/bash
+
+PREFIX=/src/local/nginx_mruby
+
+RETCODE=$(fw_exists ${PREFIX})
+[ "$RETCODE" != 0 ] || { return 0; }
+
+#fw_depends nginx lua
+
+#fw_get http://openresty.org/download/ngx_openresty-1.7.4.1.tar.gz
+#fw_untar ngx_openresty-1.7.4.1.tar.gz
+git clone git://github.com/matsumoto-r/ngx_mruby.git
+cd ngx_mruby
+git submodule init
+git submodule update
+
+RETCODE=$(fw_exists mruby/mrbgems/mruby-mysql)
+if [ "$RETCODE" != 0 ] ; then
+  git clone [email protected]:mattn/mruby-mysql.git mruby/mrbgems/mruby-mysql
+  # cd mruby/mrbgems
+  # git clone [email protected]:mattn/mruby-mysql.git mruby/mrbgems/m
+  # cd ../..
+fi
+
+NGINX_CONFIG_OPT_ENV="--prefix=${PREFIX} --with-http_stub_status_module" sh build.sh