mirror of
https://github.com/fluencelabs/jsonpath
synced 2025-05-07 15:02:14 +00:00
add lua + openresty example
This commit is contained in:
commit
5b878d7ba7
@ -27,7 +27,8 @@ array_tool = "1.0.3"
|
|||||||
[lib]
|
[lib]
|
||||||
name = "jsonpath_lib"
|
name = "jsonpath_lib"
|
||||||
path = "src/lib.rs"
|
path = "src/lib.rs"
|
||||||
|
crate-type = ["cdylib", "rlib"]
|
||||||
|
|
||||||
[profile.release]
|
[profile.release]
|
||||||
#debug = true
|
#debug = true
|
||||||
#lto = false
|
#lto = false
|
4501
benchmark/big_example.json
Normal file
4501
benchmark/big_example.json
Normal file
File diff suppressed because it is too large
Load Diff
@ -34,4 +34,4 @@
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
"expensive": 10
|
"expensive": 10
|
||||||
}
|
}
|
||||||
|
5
lua/.gitignore
vendored
Normal file
5
lua/.gitignore
vendored
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
.idea/*
|
||||||
|
.vscode
|
||||||
|
/target/
|
||||||
|
Cargo.lock
|
||||||
|
docker_example/ab_results/**
|
14
lua/Cargo.toml
Normal file
14
lua/Cargo.toml
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
[package]
|
||||||
|
name = "jsonpath_lua"
|
||||||
|
version = "0.1.0"
|
||||||
|
authors = ["Changseok Han <freestrings@gmail.com>"]
|
||||||
|
license = "MIT"
|
||||||
|
[dependencies]
|
||||||
|
serde = { version = "1.0", features = ["derive"] }
|
||||||
|
serde_json = { version = "1.0", features = ["preserve_order"] }
|
||||||
|
jsonpath_lib = { path = "../" }
|
||||||
|
|
||||||
|
[[bin]]
|
||||||
|
name = "bench"
|
||||||
|
path = "bench_lua_vs_rust/example.rs"
|
||||||
|
|
22
lua/bench_lua_vs_rust/example.lua
Normal file
22
lua/bench_lua_vs_rust/example.lua
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
local jsonpath = require("jsonpath")
|
||||||
|
|
||||||
|
local iter;
|
||||||
|
if arg[1] == nil or arg[1] == '' then
|
||||||
|
iter = 5000;
|
||||||
|
else
|
||||||
|
iter = tonumber(arg[1]);
|
||||||
|
end
|
||||||
|
|
||||||
|
print(string.format("%s - %u", "lua iter", iter));
|
||||||
|
|
||||||
|
local file = io.open("../../benchmark/example.json", "r");
|
||||||
|
io.input(file)
|
||||||
|
local data = io.read("*a");
|
||||||
|
io.close(file);
|
||||||
|
|
||||||
|
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);
|
||||||
|
end
|
46
lua/bench_lua_vs_rust/example.rs
Normal file
46
lua/bench_lua_vs_rust/example.rs
Normal file
@ -0,0 +1,46 @@
|
|||||||
|
extern crate jsonpath_lib as jsonpath;
|
||||||
|
extern crate serde;
|
||||||
|
extern crate serde_json;
|
||||||
|
|
||||||
|
use std::io::Read;
|
||||||
|
|
||||||
|
use serde_json::Value;
|
||||||
|
|
||||||
|
fn read_json(path: &str) -> String {
|
||||||
|
let mut f = std::fs::File::open(path).unwrap();
|
||||||
|
let mut contents = String::new();
|
||||||
|
f.read_to_string(&mut contents).unwrap();
|
||||||
|
contents
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_string() -> String {
|
||||||
|
read_json("../../benchmark/example.json")
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_json() -> Value {
|
||||||
|
let string = get_string();
|
||||||
|
serde_json::from_str(string.as_str()).unwrap()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_path() -> &'static str {
|
||||||
|
r#"$..book[?(@.price<30 && @.category=="fiction")]"#
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let args: Vec<String> = std::env::args().collect();
|
||||||
|
let iter = if args.len() < 2 { 5000_usize } else { args[1].as_str().parse::<usize>().unwrap() };
|
||||||
|
|
||||||
|
println!("rust iter - {}", iter);
|
||||||
|
|
||||||
|
let json = get_json();
|
||||||
|
for _ in 0..iter {
|
||||||
|
let mut selector = jsonpath::Selector::default();
|
||||||
|
let _ = selector.str_path(get_path());
|
||||||
|
selector.value(&json);
|
||||||
|
let r = selector.select();
|
||||||
|
if r.is_err() {
|
||||||
|
panic!();
|
||||||
|
}
|
||||||
|
// println!("{:?}", serde_json::to_string(&r.expect("")).unwrap());
|
||||||
|
}
|
||||||
|
}
|
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
|
||||||
|
|
107
lua/docker_example/default.conf
Normal file
107
lua/docker_example/default.conf
Normal file
@ -0,0 +1,107 @@
|
|||||||
|
lua_package_path '/etc/jsonpath/?.lua;;';
|
||||||
|
|
||||||
|
access_log /var/log/access.log;
|
||||||
|
error_log /var/log/error.log info;
|
||||||
|
|
||||||
|
lua_shared_dict jsonpaths 1m;
|
||||||
|
|
||||||
|
init_by_lua_block {
|
||||||
|
local pathStrings = {
|
||||||
|
"$.store.book[*].author",
|
||||||
|
"$..author",
|
||||||
|
"$.store.*",
|
||||||
|
"$.store..price",
|
||||||
|
"$..book[2]",
|
||||||
|
"$..book[-2]",
|
||||||
|
"$..book[0,1]",
|
||||||
|
"$..book[:2]",
|
||||||
|
"$..book[1:2]",
|
||||||
|
"$..book[-2:]",
|
||||||
|
"$..book[2:]",
|
||||||
|
"$..book[?(@.isbn)]",
|
||||||
|
"$.store.book[?(@.price == 10)]",
|
||||||
|
"$..*",
|
||||||
|
"$..book[ ?( (@.price < 13 || $.store.bicycle.price < @.price) && @.price <=10 ) ]",
|
||||||
|
"$.store.book[?( (@.price < 10 || @.price > 10) && @.price > 10 )]",
|
||||||
|
"$..[?(@.originPrice > 1)]",
|
||||||
|
"$.pickBanner[?(@.originPrice > 1)]"
|
||||||
|
}
|
||||||
|
|
||||||
|
local jp = require("jsonpath")
|
||||||
|
jp.init("/etc/jsonpath/libjsonpath_lib.so")
|
||||||
|
local jsonpaths = ngx.shared.jsonpaths
|
||||||
|
|
||||||
|
for i, path in ipairs(pathStrings) do
|
||||||
|
jsonpaths:set(i, path)
|
||||||
|
jp.compile(path)
|
||||||
|
end
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
server {
|
||||||
|
listen 80;
|
||||||
|
server_name localhost;
|
||||||
|
|
||||||
|
gzip on;
|
||||||
|
gzip_types text/plain application/json;
|
||||||
|
#gzip_comp_level 6;
|
||||||
|
#gzip_vary on;
|
||||||
|
|
||||||
|
location / {
|
||||||
|
add_header 'Cache-Control' 'no-store, no-cache, must-revalidate, proxy-revalidate, max-age=0';
|
||||||
|
expires off;
|
||||||
|
|
||||||
|
default_type 'text/plain';
|
||||||
|
root /etc/jsonpath/example;
|
||||||
|
}
|
||||||
|
|
||||||
|
location /filter {
|
||||||
|
# https://developer.mozilla.org/ko/docs/Web/HTTP/Headers/Accept-Encoding
|
||||||
|
proxy_set_header Accept-Encoding "*";
|
||||||
|
|
||||||
|
default_type 'text/plain';
|
||||||
|
|
||||||
|
rewrite /filter/(.*) /$1 break;
|
||||||
|
proxy_pass http://localhost;
|
||||||
|
|
||||||
|
header_filter_by_lua_block {
|
||||||
|
ngx.header["content-length"] = nil
|
||||||
|
|
||||||
|
local args = ngx.req.get_uri_args()
|
||||||
|
local jsonpaths = ngx.shared.jsonpaths
|
||||||
|
local path = jsonpaths:get(args['path'])
|
||||||
|
|
||||||
|
if path == nil then
|
||||||
|
ngx.exit(ngx.HTTP_BAD_REQUEST)
|
||||||
|
end
|
||||||
|
}
|
||||||
|
|
||||||
|
body_filter_by_lua_block {
|
||||||
|
local chunk, eof = ngx.arg[1], ngx.arg[2]
|
||||||
|
local buf = ngx.ctx.buf
|
||||||
|
|
||||||
|
if eof then
|
||||||
|
if buf then
|
||||||
|
local args = ngx.req.get_uri_args()
|
||||||
|
local path = ngx.shared.jsonpaths:get(args['path'])
|
||||||
|
local jsonpath = require("jsonpath")
|
||||||
|
local template = jsonpath.exec(path)
|
||||||
|
local json = buf .. chunk
|
||||||
|
local result = template(json)
|
||||||
|
ngx.arg[1] = result
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
|
if buf then
|
||||||
|
ngx.ctx.buf = buf .. chunk
|
||||||
|
else
|
||||||
|
ngx.ctx.buf = chunk
|
||||||
|
end
|
||||||
|
|
||||||
|
ngx.arg[1] = nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
3
lua/docker_example/init.lua
Normal file
3
lua/docker_example/init.lua
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
local jsonpath = require("jsonpath")
|
||||||
|
jsonpath.init("/etc/jsonpath/libjsonpath_lib.so")
|
||||||
|
ngx.log(ngx.INFO, "loaded libjsonpath_lib.so")
|
25
lua/docker_example/run.sh
Executable file
25
lua/docker_example/run.sh
Executable file
@ -0,0 +1,25 @@
|
|||||||
|
#!/usr/bin/env bash
|
||||||
|
|
||||||
|
# cd lua && cargo build --release && cd docker_example && ./run.sh
|
||||||
|
|
||||||
|
set -v
|
||||||
|
|
||||||
|
[ "$(docker ps -a | grep jsonpath)" ] && docker kill jsonpath
|
||||||
|
|
||||||
|
docker run -d --rm --name jsonpath \
|
||||||
|
-v "${PWD}/../../benchmark/example.json":/etc/jsonpath/example/example.json:ro \
|
||||||
|
-v "${PWD}/../../benchmark/big_example.json":/etc/jsonpath/example/big_example.json:ro \
|
||||||
|
-v "${PWD}/../jsonpath.lua":/etc/jsonpath/jsonpath.lua:ro \
|
||||||
|
-v "${PWD}/init.lua":/etc/jsonpath/init.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
|
||||||
|
|
||||||
|
#for i in {1..16}; do
|
||||||
|
# curl http://localhost:8080/filter/example.json?path=${i}
|
||||||
|
# echo
|
||||||
|
#done
|
||||||
|
|
||||||
|
#ab -n 1000 -c 10 http://localhost:8080/filter/big_example.json?path=17
|
||||||
|
#ab -n 1000 -c 10 http://localhost:8080/filter/big_example.json?path=18
|
60
lua/jsonpath.lua
Normal file
60
lua/jsonpath.lua
Normal file
@ -0,0 +1,60 @@
|
|||||||
|
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(level, msg)
|
||||||
|
print('['..level..'] ' .. msg)
|
||||||
|
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
|
||||||
|
end
|
||||||
|
|
||||||
|
function module.exec(path)
|
||||||
|
local compiledPath = cache[path]
|
||||||
|
|
||||||
|
if(cache[path] == nil) then
|
||||||
|
assert(jsonpath, path .. ": is not compiled")
|
||||||
|
end
|
||||||
|
|
||||||
|
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
|
59
src/ffi/mod.rs
Normal file
59
src/ffi/mod.rs
Normal file
@ -0,0 +1,59 @@
|
|||||||
|
use std::ffi::{CStr, CString};
|
||||||
|
use std::os::raw::{c_char, c_void};
|
||||||
|
|
||||||
|
use {parser, select, select_as_str};
|
||||||
|
|
||||||
|
const INVALID_PATH: &str = "invalid path";
|
||||||
|
const INVALID_JSON: &str = "invalud json";
|
||||||
|
|
||||||
|
fn to_str(v: *const c_char, err_msg: &str) -> &str {
|
||||||
|
unsafe { CStr::from_ptr(v) }.to_str().expect(err_msg)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn to_char_ptr(v: &str) -> *const c_char {
|
||||||
|
let s = CString::new(v).unwrap_or_else(|_| panic!("invalid string: {}", v));
|
||||||
|
let ptr = s.as_ptr();
|
||||||
|
std::mem::forget(s);
|
||||||
|
ptr
|
||||||
|
}
|
||||||
|
|
||||||
|
#[no_mangle]
|
||||||
|
pub extern "C" fn ffi_select(json_str: *const c_char, path: *const c_char) -> *const c_char {
|
||||||
|
let json_str = to_str(json_str, INVALID_JSON);
|
||||||
|
let path = to_str(path, INVALID_PATH);
|
||||||
|
match select_as_str(json_str, path) {
|
||||||
|
Ok(v) => to_char_ptr(v.as_str()),
|
||||||
|
Err(e) => {
|
||||||
|
panic!("{:?}", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[no_mangle]
|
||||||
|
#[allow(clippy::forget_copy)]
|
||||||
|
pub extern "C" fn ffi_path_compile(path: *const c_char) -> *mut c_void {
|
||||||
|
let path = to_str(path, INVALID_PATH);
|
||||||
|
let ref_node = Box::into_raw(Box::new(parser::Parser::compile(path).unwrap()));
|
||||||
|
let ptr = ref_node as *mut c_void;
|
||||||
|
std::mem::forget(ref_node);
|
||||||
|
ptr
|
||||||
|
}
|
||||||
|
|
||||||
|
#[no_mangle]
|
||||||
|
pub extern "C" fn ffi_select_with_compiled_path(
|
||||||
|
path_ptr: *mut c_void,
|
||||||
|
json_ptr: *const c_char,
|
||||||
|
) -> *const c_char {
|
||||||
|
let node = unsafe { Box::from_raw(path_ptr as *mut parser::Node) };
|
||||||
|
let json_str = to_str(json_ptr, INVALID_JSON);
|
||||||
|
let json = serde_json::from_str(json_str)
|
||||||
|
.unwrap_or_else(|_| panic!("invalid json string: {}", json_str));
|
||||||
|
|
||||||
|
let mut selector = select::Selector::default();
|
||||||
|
let found = selector.compiled_path(&node).value(&json).select().unwrap();
|
||||||
|
std::mem::forget(node);
|
||||||
|
|
||||||
|
let result = serde_json::to_string(&found)
|
||||||
|
.unwrap_or_else(|_| panic!("json serialize error: {:?}", found));
|
||||||
|
to_char_ptr(result.as_str())
|
||||||
|
}
|
@ -136,6 +136,8 @@ pub use parser::Parser;
|
|||||||
pub use select::JsonPathError;
|
pub use select::JsonPathError;
|
||||||
pub use select::{Selector, SelectorMut};
|
pub use select::{Selector, SelectorMut};
|
||||||
|
|
||||||
|
#[doc(hidden)]
|
||||||
|
mod ffi;
|
||||||
#[doc(hidden)]
|
#[doc(hidden)]
|
||||||
mod parser;
|
mod parser;
|
||||||
#[doc(hidden)]
|
#[doc(hidden)]
|
||||||
|
@ -2,10 +2,10 @@ use std::collections::HashSet;
|
|||||||
use std::fmt;
|
use std::fmt;
|
||||||
|
|
||||||
use array_tool::vec::{Intersect, Union};
|
use array_tool::vec::{Intersect, Union};
|
||||||
|
use serde_json::map::Entry;
|
||||||
use serde_json::{Number, Value};
|
use serde_json::{Number, Value};
|
||||||
|
|
||||||
use parser::*;
|
use parser::*;
|
||||||
use serde_json::map::Entry;
|
|
||||||
|
|
||||||
fn to_f64(n: &Number) -> f64 {
|
fn to_f64(n: &Number) -> f64 {
|
||||||
if n.is_i64() {
|
if n.is_i64() {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user