Browse Source

Merge pull request #1585 from torhve/openresty-fortunes

Lua: Add fortunes to OpenResty
Hamilton Turner 10 years ago
parent
commit
49aeacfb68

+ 51 - 21
frameworks/Lua/openresty/app.lua

@@ -3,6 +3,12 @@ local mysql = mysql
 local encode = encode
 local encode = encode
 local random = math.random
 local random = math.random
 local min = math.min
 local min = math.min
+local insert = table.insert
+local sort = table.sort
+local template = require'resty.template'
+template.caching(false)
+-- Compile template, disable cache, enable plain text view to skip filesystem loading
+local view = template.compile([[<!DOCTYPE html><html><head><title>Fortunes</title></head><body><table><tr><th>id</th><th>message</th></tr>{% for _,f in ipairs(fortunes) do %}<tr><td>{{ f.id }}</td><td>{{ f.message }}</td></tr>{% end %}</table></body></html>]], nil, true)
 
 
 local mysqlconn = {
 local mysqlconn = {
 	host = "DBHOSTNAME",
 	host = "DBHOSTNAME",
@@ -11,24 +17,48 @@ local mysqlconn = {
 	user = "benchmarkdbuser",
 	user = "benchmarkdbuser",
 	password = "benchmarkdbpass"
 	password = "benchmarkdbpass"
 }
 }
-return function(ngx)
-	local db = mysql:new()
-	assert(db:connect(mysqlconn))
-	local num_queries = tonumber(ngx.var.arg_queries) or 1
-	-- May seem like a stupid branch, but since we know that
-	-- at a benchmark it will always be taken one way,
-	-- it doesn't matter. For me, after a small warmup, the performance
-	-- is identical to a version without the branch
-	-- http://wiki.luajit.org/Numerical-Computing-Performance-Guide
-	if num_queries == 1 then
-		ngx.print(encode(db:query('SELECT * FROM World WHERE id = '..random(1,10000))[1]))
-	else
-		local worlds = {}
-		num_queries = min(500, num_queries)
-		for i=1, num_queries do
-			worlds[#worlds+1] = db:query('SELECT * FROM World WHERE id = '..random(1,10000))[1]
-		end
-		ngx.print( encode(worlds) )
-	end
-	db:set_keepalive(0, 256)
-end
+return {
+    db = function(ngx)
+        local db = mysql:new()
+        assert(db:connect(mysqlconn))
+        ngx.print(encode(db:query('SELECT * FROM World WHERE id = '..random(1,10000))[1]))
+        db:set_keepalive(0, 256)
+    end,
+    queries = function(ngx)
+        local db = mysql:new()
+        assert(db:connect(mysqlconn))
+        local num_queries = tonumber(ngx.var.arg_queries) or 1
+        -- May seem like a stupid branch, but since we know that
+        -- at a benchmark it will always be taken one way,
+        -- it doesn't matter. For me, after a small warmup, the performance
+        -- is identical to a version without the branch
+        -- http://wiki.luajit.org/Numerical-Computing-Performance-Guide
+        if num_queries < 2 then
+            ngx.print(encode({db:query('SELECT * FROM World WHERE id = '..random(1,10000))[1]}))
+        else
+            local worlds = {}
+            num_queries = min(500, num_queries)
+            for i=1, num_queries do
+                worlds[#worlds+1] = db:query('SELECT * FROM World WHERE id = '..random(1,10000))[1]
+            end
+            ngx.print( encode(worlds) )
+        end
+        db:set_keepalive(0, 256)
+    end,
+    fortunes = function(ngx)
+        local db = mysql:new()
+        assert(db:connect(mysqlconn))
+        local fortunes = db:query('SELECT * FROM Fortune')
+        insert(fortunes, {
+            id = 0,
+            message = "Additional fortune added at request time."
+        })
+        sort(fortunes, function(a, b)
+            return a.message < b.message
+        end)
+        local res = view{fortunes=fortunes}
+        ngx.header['Content-Length'] = #res
+        ngx.print(res)
+        db:set_keepalive(0, 256)
+    end
+}

+ 2 - 1
frameworks/Lua/openresty/benchmark_config.json

@@ -5,7 +5,8 @@
       "setup_file": "setup",
       "setup_file": "setup",
       "json_url": "/json",
       "json_url": "/json",
       "db_url": "/db",
       "db_url": "/db",
-      "query_url": "/db?queries=",
+      "query_url": "/queries?queries=",
+      "fortune_url": "/fortunes",
       "plaintext_url": "/plaintext",
       "plaintext_url": "/plaintext",
       "port": 8080,
       "port": 8080,
       "approach": "Realistic",
       "approach": "Realistic",

+ 3 - 1
frameworks/Lua/openresty/install.sh

@@ -1,3 +1,5 @@
 #!/bin/bash
 #!/bin/bash
 
 
-fw_depends lua nginx openresty 
+fw_depends lua openresty
+
+sudo luarocks install --server=http://rocks.moonscript.org lua-resty-template

+ 11 - 2
frameworks/Lua/openresty/nginx.conf

@@ -9,7 +9,7 @@ http {
     resolver 127.0.0.1;
     resolver 127.0.0.1;
     access_log off;
     access_log off;
     lua_package_path 'CWD/?.lua;;';
     lua_package_path 'CWD/?.lua;;';
-    init_by_lua 'jit.opt.start("minstitch=10"); require "resty.core" encode = require("cjson").encode mysql = require("resty.mysql")';
+    init_by_lua 'jit.opt.start("minstitch=10"); require "resty.core" encode = require("cjson").encode mysql = require("resty.mysql") app = require("app")';
     server {
     server {
         listen       8080;
         listen       8080;
         location /plaintext {
         location /plaintext {
@@ -21,8 +21,17 @@ http {
             default_type "application/json";
             default_type "application/json";
             content_by_lua 'ngx.print(encode({message = "Hello, World!"}))';
             content_by_lua 'ngx.print(encode({message = "Hello, World!"}))';
         }
         }
+        location /fortunes {
+            default_type "text/html; charset=UTF-8";
+            content_by_lua 'app.fortunes(ngx)';
+        }
+        location /db {
+            default_type "application/json";
+            content_by_lua 'app.db(ngx)';
+        }
         location / {
         location / {
-            content_by_lua 'require("app")(ngx)';
+            default_type "application/json";
+            content_by_lua 'app.queries(ngx)';
         }
         }
     }
     }
 }
 }