openresty 테스트

This commit is contained in:
freestrings 2019-08-23 22:09:09 +09:00
parent b5c5d6b88e
commit 422a23ee57
8 changed files with 124 additions and 54 deletions

View File

@ -1,25 +0,0 @@
#!/bin/bash
set -e
# http://luajit.org/index.html
#cargo clean && \
cargo build --release
export JSONPATH_LIB_PATH="${PWD}/target/release/deps"
export LUA_PATH="${PWD}/?.lua;"
echo
time cargo run --release --bin bench -- 1000
echo
time luajit bench_lua_vs_rust/example.lua 1000
echo
time cargo run --release --bin bench -- 5000
echo
time luajit bench_lua_vs_rust/example.lua 5000
echo
time cargo run --release --bin bench -- 10000
echo
time luajit bench_lua_vs_rust/example.lua 10000

View File

@ -1,4 +1,4 @@
require("lib")
local jsonpath = require("jsonpath")
local iter;
if arg[1] == nil or arg[1] == '' then
@ -9,11 +9,13 @@ end
print(string.format("%s - %u", "lua iter", iter));
local file = io.open("../benchmark/example.json", "r");
local file = io.open("../../benchmark/example.json", "r");
io.input(file)
local data = io.read("*a");
io.close(file);
local template = compile("$..book[?(@.price<30 && @.category==\"fiction\")]");
jsonpath.init('../target/release/deps/libjsonpath_lib.so')
local template = jsonpath.compile("$..book[?(@.price<30 && @.category==\"fiction\")]");
for i = 0, iter do
local r = template(data);
-- print(r);

View File

@ -14,7 +14,7 @@ fn read_json(path: &str) -> String {
}
fn get_string() -> String {
read_json("../benchmark/example.json")
read_json("../../benchmark/example.json")
}
fn get_json() -> Value {

27
lua/bench_lua_vs_rust/run.sh Executable file
View File

@ -0,0 +1,27 @@
#!/bin/bash
# cd lua/bench_lua_vs_rust && ./run.sh
set -e
# http://luajit.org/index.html
# cargo clean && \
cargo build --release
export JSONPATH_LIB_PATH="${PWD}/../target/release/deps"
export LUA_PATH="${PWD}/../?.lua;"
echo
time cargo run --release --bin bench -- 1000
echo
time luajit example.lua 1000
echo
time cargo run --release --bin bench -- 5000
echo
time luajit example.lua 5000
echo
time cargo run --release --bin bench -- 10000
echo
time luajit example.lua 10000

View File

@ -0,0 +1,25 @@
lua_package_path '/etc/jsonpath/?.lua;;';
error_log /var/log/nginx_error.log info;
server {
listen 80;
server_name localhost;
location / {
default_type text/html;
content_by_lua '
local jsonpath = require("jsonpath")
jsonpath.init("/etc/jsonpath/libjsonpath_lib.so")
local data = ngx.location.capture("/example.json")
local template = jsonpath.compile("$..book[?(@.price<30 && @.category==\'fiction\')]")
local result = template(data.body)
ngx.say(result)
';
}
location /example {
root /etc/jsonpath/example;
}
}

15
lua/docker_example/run.sh Executable file
View File

@ -0,0 +1,15 @@
#!/usr/bin/env bash
# cd lua/docker_example && ./run.sh
set -v
docker run -d --rm --name jsonpath \
-v "${PWD}/../../benchmark/example.json":/etc/jsonpath/example/example.json:ro \
-v "${PWD}/../jsonpath.lua":/etc/jsonpath/jsonpath.lua:ro \
-v "${PWD}/../target/release/deps/libjsonpath_lib.so":/etc/jsonpath/libjsonpath_lib.so:ro \
-v "${PWD}/default.conf":/etc/nginx/conf.d/default.conf \
-p 8080:80 \
openresty/openresty:bionic
docker exec -it jsonpath bash -c "curl localhost"

51
lua/jsonpath.lua Normal file
View File

@ -0,0 +1,51 @@
local ffi = require('ffi')
ffi.cdef [[
const char* ffi_select(const char *json_str, const char *path);
void *ffi_path_compile(const char *path);
const char* ffi_select_with_compiled_path(void *ptr, const char *json_str);
]]
local jsonpath
local cache = {}
local module = {}
local function existsVaiable(var)
for k, _ in pairs(_G) do
if k == var then
return true
end
end
end
local _ngx
if existsVaiable('ngx') then
_ngx = ngx
else
_ngx = {}
_ngx.log = function() end
end
function module.compile(path)
assert(jsonpath, '"libjsonpath_lib" is not loaded')
if(cache[path] == nil) then
cache[path] = jsonpath.ffi_path_compile(path)
_ngx.log(_ngx.INFO, 'compile : [' .. path .. ']')
end
local compiledPath = cache[path]
return function(jsonStr)
local result = jsonpath.ffi_select_with_compiled_path(compiledPath, jsonStr)
return ffi.string(result);
end
end
function module.init(path)
if jsonpath == nil then
jsonpath = ffi.load(path)
_ngx.log(_ngx.INFO, '"' .. path .. '" initialized')
end
end
return module

View File

@ -1,25 +0,0 @@
local ffi = require('ffi')
local ext
if ffi.os == 'Linux' then
ext = 'so'
else
ext = 'dylib'
end
ffi.cdef [[
const char* ffi_select(const char *json_str, const char *path);
void *ffi_path_compile(const char *path);
const char* ffi_select_with_compiled_path(void *ptr, const char *json_str);
]]
local jsonpathLibPath = os.getenv("JSONPATH_LIB_PATH");
local jsonpath = ffi.load(jsonpathLibPath .. '/libjsonpath_lib.' .. ext);
function compile(path)
local compiledPath = jsonpath.ffi_path_compile(path);
return function(jsonStr)
return ffi.string(jsonpath.ffi_select_with_compiled_path(compiledPath, jsonStr));
end
end