Browse Source

Merge branch 'lapis2' of https://github.com/torhve/FrameworkBenchmarks into PR769

Mike Smith 11 years ago
parent
commit
8d78a131fb
7 changed files with 220 additions and 174 deletions
  1. 2 2
      lapis/config.lua
  2. 1 0
      lapis/loader.lua
  3. 1 0
      lapis/loader.moon
  4. 36 35
      lapis/nginx.conf
  5. 0 1
      lapis/setup.py
  6. 150 119
      lapis/web.lua
  7. 30 17
      lapis/web.moon

+ 2 - 2
lapis/config.lua

@@ -1,7 +1,7 @@
 local config
 do
-  local _table_0 = require("lapis.config")
-  config = _table_0.config
+  local _obj_0 = require("lapis.config")
+  config = _obj_0.config
 end
 config("development", function()
   return postgresql_url("postgres://benchmarkdbuser:[email protected]/hello_world")

+ 1 - 0
lapis/loader.lua

@@ -0,0 +1 @@
+return lapis.serve(require("web"))

+ 1 - 0
lapis/loader.moon

@@ -0,0 +1 @@
+lapis.serve(require("web"))

+ 36 - 35
lapis/nginx.conf

@@ -1,43 +1,44 @@
-#worker_processes  1;
-pid        /tmp/nginx.pid;
-error_log stderr error;
-#error_log /tmp/test.log error;
-env LAPIS_ENVIRONMENT;
+#    pid        /tmp/nginx.pid;
+    error_log stderr notice;
+    #error_log /tmp/test.log error;
+    env LAPIS_ENVIRONMENT;
+    daemon off;
 
-events {
-    worker_connections 16384;
-}
-
-http {
-    access_log off;
-    include mime.types;
-    lua_package_path 'CWD/lapis/?.lua;;';	
-
-    upstream database {
-        postgres_server DBHOSTNAME dbname=hello_world user=benchmarkdbuser password=benchmarkdbpass;
+    events {
+        worker_connections 16384;
     }
-    server {
-        listen 8080;
-        lua_code_cache on;
-        charset "utf-8";
 
-        location / {
-            default_type text/html;
-            set $_url "";
-            content_by_lua_file "web.lua";
-        }
+    http {
+        variables_hash_max_size 1024;
+        access_log off;
+        include mime.types;
 
-        location /static/ {
-            alias static/;
+        upstream database {
+            postgres_server DBHOSTNAME dbname=hello_world user=benchmarkdbuser password=benchmarkdbpass;
         }
+        init_by_lua 'require "resty.core"; lapis = require "lapis.init"';
+        server {
+            listen 8880;
+            lua_code_cache on;
+            charset "utf-8";
 
-        location /favicon.ico {
-          alias static/favicon.ico;
-        }
-        location = /query {
-            internal;
-            postgres_pass database;
-            postgres_query $echo_request_body;
+            location / {
+                default_type text/html;
+                set $_url "";
+                content_by_lua_file "loader.lua";
+            }
+
+            location /static/ {
+                alias static/;
+            }
+
+            location /favicon.ico {
+              alias static/favicon.ico;
+            }
+            location = /query {
+                internal;
+                postgres_pass database;
+                postgres_query $echo_request_body;
+            }
         }
     }
-}

+ 0 - 1
lapis/setup.py

@@ -4,7 +4,6 @@ import setup_util
 import os
 
 def start(args, logfile, errfile):
-  setup_util.replace_text("lapis/nginx.conf", "CWD", os.getcwd())
   setup_util.replace_text("lapis/nginx.conf", "DBHOSTNAME", args.database_host)
   subprocess.Popen('/usr/local/openresty/nginx/sbin/nginx -c `pwd`/nginx.conf -g "worker_processes ' + str((args.max_threads)) + ';"', shell=True, cwd="lapis", stderr=errfile, stdout=logfile)
 

+ 150 - 119
lapis/web.lua

@@ -2,30 +2,38 @@ local lapis = require("lapis")
 local db = require("lapis.db")
 local Model
 do
-  local _table_0 = require("lapis.db.model")
-  Model = _table_0.Model
+  local _obj_0 = require("lapis.db.model")
+  Model = _obj_0.Model
 end
 local config
 do
-  local _table_0 = require("lapis.config")
-  config = _table_0.config
+  local _obj_0 = require("lapis.config")
+  config = _obj_0.config
+end
+local insert
+do
+  local _obj_0 = table
+  insert = _obj_0.insert
+end
+local sort
+do
+  local _obj_0 = table
+  sort = _obj_0.sort
+end
+local random
+do
+  local _obj_0 = math
+  random = _obj_0.random
 end
-local insert = table.insert
-local sort = table.sort
-local random = math.random
 local Fortune
 do
   local _parent_0 = Model
   local _base_0 = { }
   _base_0.__index = _base_0
-  if _parent_0 then
-    setmetatable(_base_0, _parent_0.__base)
-  end
+  setmetatable(_base_0, _parent_0.__base)
   local _class_0 = setmetatable({
     __init = function(self, ...)
-      if _parent_0 then
-        return _parent_0.__init(self, ...)
-      end
+      return _parent_0.__init(self, ...)
     end,
     __base = _base_0,
     __name = "Fortune",
@@ -33,7 +41,7 @@ do
   }, {
     __index = function(cls, name)
       local val = rawget(_base_0, name)
-      if val == nil and _parent_0 then
+      if val == nil then
         return _parent_0[name]
       else
         return val
@@ -46,7 +54,7 @@ do
     end
   })
   _base_0.__class = _class_0
-  if _parent_0 and _parent_0.__inherited then
+  if _parent_0.__inherited then
     _parent_0.__inherited(_parent_0, _class_0)
   end
   Fortune = _class_0
@@ -56,14 +64,10 @@ do
   local _parent_0 = Model
   local _base_0 = { }
   _base_0.__index = _base_0
-  if _parent_0 then
-    setmetatable(_base_0, _parent_0.__base)
-  end
+  setmetatable(_base_0, _parent_0.__base)
   local _class_0 = setmetatable({
     __init = function(self, ...)
-      if _parent_0 then
-        return _parent_0.__init(self, ...)
-      end
+      return _parent_0.__init(self, ...)
     end,
     __base = _base_0,
     __name = "World",
@@ -71,7 +75,7 @@ do
   }, {
     __index = function(cls, name)
       local val = rawget(_base_0, name)
-      if val == nil and _parent_0 then
+      if val == nil then
         return _parent_0[name]
       else
         return val
@@ -84,122 +88,149 @@ do
     end
   })
   _base_0.__class = _class_0
-  if _parent_0 and _parent_0.__inherited then
+  if _parent_0.__inherited then
     _parent_0.__inherited(_parent_0, _class_0)
   end
   World = _class_0
 end
-return lapis.serve((function()
-  local Benchmark
-  do
-    local _parent_0 = lapis.Application
-    local _base_0 = {
-      ["/"] = function(self)
+local Benchmark
+do
+  local _parent_0 = lapis.Application
+  local _base_0 = {
+    ["/"] = function(self)
+      return {
+        json = {
+          message = "Hello, World!"
+        }
+      }
+    end,
+    ["/db"] = function(self)
+      local num_queries = tonumber(self.params.queries) or 1
+      if num_queries < 2 then
+        local w = World:find(random(1, 10000))
         return {
           json = {
-            message = "Hello, World!"
+            id = w.id,
+            randomNumber = w.randomnumber
           }
         }
-      end,
-      ["/db"] = function(self)
-        local num_queries = tonumber(self.params.queries) or 1
-        local worlds = { }
-        for i = 1, num_queries do
-          insert(worlds, World:find(random(1, 10000)))
-        end
-        return {
-          json = worlds
-        }
-      end,
-      ["/fortunes"] = function(self)
-        self.fortunes = Fortune:select("")
-        insert(self.fortunes, {
-          id = 0,
-          message = "Additional fortune added at request time."
+      end
+      local worlds = { }
+      for i = 1, num_queries do
+        local w = World:find(random(1, 10000))
+        insert(worlds, {
+          id = w.id,
+          randomNumber = w.randomnumber
         })
-        sort(self.fortunes, function(a, b)
-          return a.message < b.message
-        end)
-        self.title = "Fortunes"
-        return self:html(function()
-          return element("table", function()
-            tr(function()
-              th(function()
-                return text("id")
-              end)
-              return th(function()
-                return text("message")
-              end)
-            end)
-            local _list_0 = self.fortunes
-            for _index_0 = 1, #_list_0 do
-              local fortune = _list_0[_index_0]
+      end
+      return {
+        json = worlds
+      }
+    end,
+    ["/fortunes"] = function(self)
+      self.fortunes = Fortune:select("")
+      insert(self.fortunes, {
+        id = 0,
+        message = "Additional fortune added at request time."
+      })
+      sort(self.fortunes, function(a, b)
+        return a.message < b.message
+      end)
+      return {
+        layout = false
+      }, self:html(function()
+        raw('<!DOCTYPE HTML>')
+        return html(function()
+          head(function()
+            return title("Fortunes")
+          end)
+          return body(function()
+            return element("table", function()
               tr(function()
-                td(function()
-                  return text(fortune.id)
+                th(function()
+                  return text("id")
                 end)
-                return td(function()
-                  return text(fortune.message)
+                return th(function()
+                  return text("message")
                 end)
               end)
-            end
+              local _list_0 = self.fortunes
+              for _index_0 = 1, #_list_0 do
+                local fortune = _list_0[_index_0]
+                tr(function()
+                  td(function()
+                    return text(fortune.id)
+                  end)
+                  return td(function()
+                    return text(fortune.message)
+                  end)
+                end)
+              end
+            end)
           end)
         end)
-      end,
-      ["/update"] = function(self)
-        local num_queries = tonumber(self.params.queries) or 1
-        local worlds = { }
-        for i = 1, num_queries do
-          local wid = random(1, 10000)
-          local world = World:find(wid)
-          world.randomnumber = random(1, 10000)
-          world:update("randomnumber")
-          insert(worlds, world)
-        end
+      end)
+    end,
+    ["/update"] = function(self)
+      local num_queries = tonumber(self.params.queries) or 1
+      if num_queries == 0 then
+        num_queries = 1
+      end
+      local worlds = { }
+      for i = 1, num_queries do
+        local wid = random(1, 10000)
+        local world = World:find(wid)
+        world.randomnumber = random(1, 10000)
+        world:update("randomnumber")
+        insert(worlds, {
+          id = world.id,
+          randomNumber = world.randomnumber
+        })
+      end
+      if num_queries < 2 then
         return {
-          json = worlds
+          json = worlds[1]
         }
-      end,
-      ["/plaintext"] = function(self)
-        return {
-          content_type = "text/plain",
-          layout = false
-        }, "Hello, World!"
       end
-    }
-    _base_0.__index = _base_0
-    if _parent_0 then
-      setmetatable(_base_0, _parent_0.__base)
+      return {
+        json = worlds
+      }
+    end,
+    ["/plaintext"] = function(self)
+      return {
+        content_type = "text/plain",
+        layout = false
+      }, "Hello, World!"
     end
-    local _class_0 = setmetatable({
-      __init = function(self, ...)
-        if _parent_0 then
-          return _parent_0.__init(self, ...)
-        end
-      end,
-      __base = _base_0,
-      __name = "Benchmark",
-      __parent = _parent_0
-    }, {
-      __index = function(cls, name)
-        local val = rawget(_base_0, name)
-        if val == nil and _parent_0 then
-          return _parent_0[name]
-        else
-          return val
-        end
-      end,
-      __call = function(cls, ...)
-        local _self_0 = setmetatable({}, _base_0)
-        cls.__init(_self_0, ...)
-        return _self_0
+  }
+  _base_0.__index = _base_0
+  setmetatable(_base_0, _parent_0.__base)
+  local _class_0 = setmetatable({
+    __init = function(self, ...)
+      return _parent_0.__init(self, ...)
+    end,
+    __base = _base_0,
+    __name = "Benchmark",
+    __parent = _parent_0
+  }, {
+    __index = function(cls, name)
+      local val = rawget(_base_0, name)
+      if val == nil then
+        return _parent_0[name]
+      else
+        return val
       end
-    })
-    _base_0.__class = _class_0
-    if _parent_0 and _parent_0.__inherited then
-      _parent_0.__inherited(_parent_0, _class_0)
+    end,
+    __call = function(cls, ...)
+      local _self_0 = setmetatable({}, _base_0)
+      cls.__init(_self_0, ...)
+      return _self_0
     end
-    Benchmark = _class_0
-    return _class_0
+  })
+  _base_0.__class = _class_0
+  if _parent_0.__inherited then
+    _parent_0.__inherited(_parent_0, _class_0)
   end
-end)())
+  Benchmark = _class_0
+  return _class_0
+end

+ 30 - 17
lapis/web.moon

@@ -10,15 +10,20 @@ class Fortune extends Model
 
 class World extends Model
 
-lapis.serve class Benchmark extends lapis.Application
+class Benchmark extends lapis.Application
   "/": =>
     json: {message: "Hello, World!"}
 
   "/db": =>
     num_queries = tonumber(@params.queries) or 1
+    if num_queries < 2 
+      w = World\find random(1, 10000)
+      return json: {id:w.id,randomNumber:w.randomnumber}
+
     worlds = {}
     for i = 1, num_queries
-      insert worlds, World\find random(1, 10000)
+      w = World\find random(1, 10000)
+      insert worlds, {id:w.id,randomNumber:w.randomnumber} 
     json: worlds
 
   "/fortunes": =>
@@ -26,30 +31,38 @@ lapis.serve class Benchmark extends lapis.Application
     insert @fortunes, {id:0, message:"Additional fortune added at request time."}
     sort @fortunes, (a, b) -> a.message < b.message
 
-    @title = "Fortunes"
-    @html ->
-      element "table", ->
-        tr ->
-          th ->
-            text "id"
-          th ->
-            text "message"
-        for fortune in *@fortunes
-          tr ->
-            td ->
-              text fortune.id
-            td ->
-              text fortune.message
+    layout:false, @html ->
+      raw '<!DOCTYPE HTML>'
+      html ->
+        head ->
+          title "Fortunes"
+        body ->
+          element "table", ->
+            tr ->
+              th ->
+                text "id"
+              th ->
+                text "message"
+            for fortune in *@fortunes
+              tr ->
+                td ->
+                  text fortune.id
+                td ->
+                  text fortune.message
 
   "/update": =>
     num_queries = tonumber(@params.queries) or 1
+    if num_queries == 0
+      num_queries = 1
     worlds = {}
     for i = 1, num_queries
       wid = random(1, 10000)
       world = World\find wid
       world.randomnumber = random(1, 10000)
       world\update "randomnumber"
-      insert worlds, world
+      insert worlds, {id:world.id,randomNumber:world.randomnumber} 
+    if num_queries < 2
+      return json: worlds[1]
     json: worlds
 
   "/plaintext": =>