Browse Source

upgrade openresty

zhuyan 8 years ago
parent
commit
8425e668b4

+ 50 - 43
frameworks/Lua/openresty/app.lua

@@ -6,6 +6,8 @@ local min = math.min
 local insert = table.insert
 local sort = table.sort
 local template = require'resty.template'
+local ngx = ngx
+local ngx_print = ngx.print
 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)
@@ -17,48 +19,53 @@ local mysqlconn = {
 	user = "benchmarkdbuser",
 	password = "benchmarkdbpass"
 }
-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) )
+
+local _M = {}
+
+function _M.db()
+    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
+
+function _M.queries()
+    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
-        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)
+        ngx_print( encode(worlds) )
     end
-}
+    db:set_keepalive(0, 256)
+end
+
+function _M.fortunes()
+    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
+
+return _M

+ 23 - 6
frameworks/Lua/openresty/nginx.conf

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

+ 5 - 5
toolset/setup/linux/webservers/openresty.sh

@@ -7,15 +7,15 @@ RETCODE=$(fw_exists ${IROOT}/openresty.installed)
   source $IROOT/openresty.installed
   return 0; }
 
-OPENRESTY_VERSION="1.7.10.1"
+OPENRESTY_VERSION="1.11.2.1"
 OPENRESTY=$IROOT/openresty
 OPENRESTY_HOME=$OPENRESTY-$OPENRESTY_VERSION
 
-fw_get -O http://openresty.org/download/ngx_openresty-$OPENRESTY_VERSION.tar.gz
-fw_untar ngx_openresty-$OPENRESTY_VERSION.tar.gz
+fw_get -O http://openresty.org/download/openresty-$OPENRESTY_VERSION.tar.gz
+fw_untar openresty-$OPENRESTY_VERSION.tar.gz
 
-cd ngx_openresty-$OPENRESTY_VERSION
-./configure --with-luajit-xcflags=-DLUAJIT_NUMMODE=2 --with-http_postgres_module --prefix=$OPENRESTY_HOME -j4
+cd openresty-$OPENRESTY_VERSION
+./configure --with-http_postgres_module --prefix=$OPENRESTY_HOME --with-luajit-xcflags="-DLUAJIT_NUMMODE=2 -O3" --with-cc-opt="-O3" -j4
 make -j4 --quiet
 make --quiet install