mirror of
https://github.com/fluencelabs/jsonpath
synced 2025-04-24 17:02:16 +00:00
openresty 테스트
This commit is contained in:
parent
b5c5d6b88e
commit
422a23ee57
@ -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
|
||||
|
@ -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);
|
||||
|
@ -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
27
lua/bench_lua_vs_rust/run.sh
Executable 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
|
||||
|
25
lua/docker_example/default.conf
Normal file
25
lua/docker_example/default.conf
Normal 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
15
lua/docker_example/run.sh
Executable 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
51
lua/jsonpath.lua
Normal 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
|
25
lua/lib.lua
25
lua/lib.lua
@ -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
|
Loading…
x
Reference in New Issue
Block a user