Browse Source

Add openresty benchmark

Tor Hveem 12 years ago
parent
commit
a46cc5767a
5 changed files with 97 additions and 0 deletions
  1. 21 0
      openresty/README.md
  2. 30 0
      openresty/app.lua
  3. 13 0
      openresty/benchmark_config
  4. 17 0
      openresty/nginx.conf
  5. 16 0
      openresty/setup.py

+ 21 - 0
openresty/README.md

@@ -0,0 +1,21 @@
+# Openresty (Nginx + Lua(jit) Benchmark Test
+
+The lua app is inside [app.lua](app.lua)
+The nginx conf is inside [nginx.conf](nginx.conf)
+Requires a nginx compiled with ngx_lua module, see [openresty.org](http://openresty.org)
+
+Requires mysql hostname specified as IP address, if not possible then add resolver conf to nginx.conf.
+
+
+## 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

+ 30 - 0
openresty/app.lua

@@ -0,0 +1,30 @@
+local cjson = require "cjson"
+local mysql = require "resty.mysql"
+local math = require "math"
+
+local mysqlconn = {
+    host = "DBHOSTNAME",
+    port = 3306,
+    database = "hello_world",
+    user = "benchmarkdbuser",
+    password = "benchmarkdbpass"
+}
+
+ngx.header.content_type = 'application/json'
+
+if ngx.var.uri == '/json' then
+    local resp = {message = "Hello, World!"}
+    ngx.print( cjson.encode(resp) )
+
+elseif ngx.var.uri == '/db' then
+    local db, err = mysql:new()
+    local ok, err = db:connect(mysqlconn)
+    local num_queries = tonumber(ngx.req.get_uri_args()["queries"]) or 1
+    local worlds = {}
+    for i=1, num_queries do
+        local wid = math.random(1, 10000)
+        table.insert(worlds, db:query('SELECT * FROM World WHERE id = '..wid)[1])
+    end
+    ngx.print( cjson.encode(worlds) )
+    local ok, err = db:set_keepalive(0, 256)
+end

+ 13 - 0
openresty/benchmark_config

@@ -0,0 +1,13 @@
+{
+  "framework": "openresty",
+  "tests": [{
+    "default": {
+      "setup_file": "setup",
+      "json_url": "/json",
+      "db_url": "/db",
+      "query_url": "/db?queries=",
+      "port": 8080,
+      "sort": 42
+    }
+  }]
+}

+ 17 - 0
openresty/nginx.conf

@@ -0,0 +1,17 @@
+worker_processes  1;
+pid        /tmp/nginx.pid;
+error_log /dev/null crit;
+#error_log /tmp/test.log error;
+events {
+    worker_connections  1024;
+}
+
+http {
+    access_log off;
+    server {
+        listen       8080;
+        location / {
+            content_by_lua_file 'CWD/openresty/app.lua';
+        }
+    }
+}

+ 16 - 0
openresty/setup.py

@@ -0,0 +1,16 @@
+import subprocess
+import sys
+import setup_util
+import os
+
+def start(args):
+  setup_util.replace_text("openresty/nginx.conf", "CWD", os.getcwd())
+  setup_util.replace_text("openresty/app.lua", "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="openresty")
+
+  return 0
+
+def stop():
+  subprocess.Popen('/usr/local/openresty/nginx/sbin/nginx -c `pwd`/nginx.conf -s stop', shell=True, cwd="openresty")
+
+  return 0