Browse Source

basic frameworks for mruby

Keenan Brock 10 years ago
parent
commit
fb6c01454f

+ 6 - 0
frameworks/Ruby/nginx_mruby/bash_profile.sh

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

+ 26 - 0
frameworks/Ruby/nginx_mruby/benchmark_config

@@ -0,0 +1,26 @@
+{
+  "framework": "nginx_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": "nginx_mruby",
+      "language": "Ruby",
+      "orm": "Raw",
+      "platform": "nginx_mruby",
+      "webserver": "nginx",
+      "os": "Linux",
+      "database_os": "Linux",
+      "display_name": "nginx_mruby",
+      "notes": "",
+      "versus": "nginx_mruby"
+    }
+  }]
+}

+ 16 - 21
frameworks/Ruby/nginx_mruby/nginx.conf

@@ -1,7 +1,5 @@
-#pid        /tmp/nginx.pid;
-
-#error_log  stderr error;
-
+pid        /tmp/nginx.pid;
+error_log stderr error;
 
 
 events {
 events {
     worker_connections  16384;
     worker_connections  16384;
@@ -9,15 +7,11 @@ events {
 
 
 http {
 http {
     resolver 127.0.0.1;
     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("DBHOSTNAME", "root", "", "benchmark")';
+    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';
     mruby_exit_worker_code 'db = Userdata.new("my_#{Process.pid}").db ; db.close if db';
     server {
     server {
-        listen       8000;
+        listen       8080;
         server_name  localhost;
         server_name  localhost;
 
 
         location / {
         location / {
@@ -27,30 +21,31 @@ http {
 
 
         location /plaintext {
         location /plaintext {
             default_type "text/plain";
             default_type "text/plain";
-            mruby_content_handler_code 'Nginx.rputs "Hello World!"';
+            mruby_content_handler_code 'Nginx.rputs "Hello, world!"';
         }
         }
 
 
         location /json {
         location /json {
             default_type "application/json";
             default_type "application/json";
-            mruby_content_handler_code 'Nginx.rputs JSON::stringify("message" => "Hello World!")';
+            mruby_content_handler_code 'Nginx.rputs JSON::stringify("message" => "Hello, World!")';
         }
         }
 
 
         location ~ /db {
         location ~ /db {
             default_type "application/json";
             default_type "application/json";
-            mruby_content_handler 'CWD/nginx_mruby/db.rb' cache;
+            #note: next line has cwd
+            mruby_content_handler 'CWD/db.rb' cache;
         }
         }
 
 
         location ~ /queries {
         location ~ /queries {
             default_type "application/json";
             default_type "application/json";
-            mruby_content_handler 'CWD/nginx_mruby/queries.rb' cache;
+            #note: next line has cwd
+            mruby_content_handler 'CWD/queries.rb' cache;
         }
         }
 
 
-        location ~ /fotunes {
-            default_type "application/html";
-            mruby_content_handler 'CWD/nginx_mruby/fotunes.rb' cache;
-        }
-
-
+#        location ~ /fortunes {
+#            default_type "application/html";
+#            #note: next line has cwd
+#            mruby_content_handler 'CWD/fortunes.rb' cache;
+#        }
 
 
         #location ~ \.rb$ {
         #location ~ \.rb$ {
         #    default_type "application/json";
         #    default_type "application/json";

+ 6 - 3
frameworks/Ruby/nginx_mruby/queries.rb

@@ -5,15 +5,17 @@ num_queries = 1 if num_queries < 0
 num_queries = 500 if num_queries > 500
 num_queries = 500 if num_queries > 500
 
 
 # https://github.com/mattn/mruby-mysql/blob/master/example/example.rb
 # https://github.com/mattn/mruby-mysql/blob/master/example/example.rb
-class Worlds
+class World
   def db
   def db
     @db ||= Userdata.new("my_#{Process.pid}").db
     @db ||= Userdata.new("my_#{Process.pid}").db
   end
   end
 
 
   def find(id)
   def find(id)
+    ret = nil
     db.execute("select * from World where id = ?", id) do |row, fields|
     db.execute("select * from World where id = ?", id) do |row, fields|
-      return Hash[fields.zip(row)]
+      ret = Hash[fields.zip(row)]
     end
     end
+    ret
   end
   end
 
 
   def save(world)
   def save(world)
@@ -21,5 +23,6 @@ class Worlds
   end
   end
 end
 end
 
 
-ret = num_queries.times.map { World.find(rand(10000)) }
+world = World.new
+ret = num_queries.times.map { world.find(rand(10000)) }
 Nginx.rputs JSON::stringify(ret)
 Nginx.rputs JSON::stringify(ret)

+ 2 - 2
frameworks/Ruby/nginx_mruby/setup.py

@@ -6,11 +6,11 @@ import setup_util
 def start(args, logfile, errfile):
 def start(args, logfile, errfile):
   setup_util.replace_text("nginx_mruby/nginx.conf", "CWD", args.troot)
   setup_util.replace_text("nginx_mruby/nginx.conf", "CWD", args.troot)
   setup_util.replace_text("nginx_mruby/nginx.conf", "DBHOSTNAME", args.database_host)
   setup_util.replace_text("nginx_mruby/nginx.conf", "DBHOSTNAME", args.database_host)
-  subprocess.Popen('sudo /usr/local/nginx_mruby/nginx/sbin/nginx -c $TROOT/nginx.conf -g "worker_processes ' + str((args.max_threads)) + ';"', shell=True, cwd="nginx_mruby", stderr=errfile, stdout=logfile)
+  subprocess.Popen('sudo /usr/local/nginx_mruby/sbin/nginx -c $TROOT/nginx.conf -g "worker_processes ' + str((args.max_threads)) + ';"', shell=True, cwd="nginx_mruby", stderr=errfile, stdout=logfile)
 
 
   return 0
   return 0
 
 
 def stop(logfile, errfile):
 def stop(logfile, errfile):
-  subprocess.Popen('sudo /usr/local/nginx_mruby/nginx/sbin/nginx -c $TROOT/nginx.conf -s stop', shell=True, cwd="nginx_mruby", stderr=errfile, stdout=logfile)
+  subprocess.Popen('sudo /usr/local/nginx_mruby/sbin/nginx -c $TROOT/nginx.conf -s stop', shell=True, cwd="nginx_mruby", stderr=errfile, stdout=logfile)
 
 
   return 0
   return 0

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

@@ -5,6 +5,7 @@ PREFIX=/usr/local/nginx_mruby
 RETCODE=$(fw_exists nginx_mruby.installed)
 RETCODE=$(fw_exists nginx_mruby.installed)
 [ ! "$RETCODE" == 0 ] || { return 0; }
 [ ! "$RETCODE" == 0 ] || { return 0; }
 
 
+sudo apt-get install -y libhiredis-dev
 fw_depends rvm
 fw_depends rvm
 rvm install ruby-2.0.0-p0
 rvm install ruby-2.0.0-p0
 rvm use ruby-2.0.0-p0
 rvm use ruby-2.0.0-p0