diff --git a/benchmark/example.json b/benchmark/example.json index cb88a7b..ce90f3a 100644 --- a/benchmark/example.json +++ b/benchmark/example.json @@ -34,4 +34,4 @@ } }, "expensive": 10 -} \ No newline at end of file +} diff --git a/lua/docker_example/default.conf b/lua/docker_example/default.conf index 558eb66..95870e5 100644 --- a/lua/docker_example/default.conf +++ b/lua/docker_example/default.conf @@ -3,30 +3,112 @@ lua_package_path '/etc/jsonpath/?.lua;;'; access_log /var/log/nginx_access.log; error_log /var/log/nginx_error.log info; +init_by_lua_block { + jp = require("jsonpath") + jp.init("/etc/jsonpath/libjsonpath_lib.so") + + BOOK = "$..book" + jp.compile(BOOK) + + COMMENT_COUNT = "$..commit[?(@.author.name == 'Thibault Charbonnier')]" + jp.compile(COMMENT_COUNT) +} + server { - listen 80; - server_name localhost; + listen 80; + server_name localhost; - location /0 { - default_type text/html; + gzip on; + gzip_types text/plain application/json; + #gzip_comp_level 6; + #gzip_vary on; - 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 ~ \.json { + 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 /1 { - default_type text/html; + default_type text/plain; + content_by_lua_file /etc/jsonpath/testa.lua; + } - content_by_lua_file /etc/jsonpath/testa.lua; + location /2 { + # https://developer.mozilla.org/ko/docs/Web/HTTP/Headers/Accept-Encoding + proxy_set_header Accept-Encoding "*"; + + default_type 'text/plain'; + + proxy_pass 'http://localhost/example.json'; + + header_filter_by_lua_block { + ngx.header.content_length = nil } - location /example { - root /etc/jsonpath/example; + 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 json = buf .. chunk + local template = jp.compile(BOOK) + 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 + } + } + + location /3 { + # https://developer.mozilla.org/ko/docs/Web/HTTP/Headers/Accept-Encoding + proxy_set_header Accept-Encoding "*"; + + default_type 'text/plain'; + + proxy_pass 'https://api.github.com/repos/openresty/lua-nginx-module/commits'; + + header_filter_by_lua_block { + ngx.header.content_length = nil + } + + 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 json = buf .. chunk + local template = jp.compile(COMMENT_COUNT) + 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 + } } } \ No newline at end of file diff --git a/lua/docker_example/run.sh b/lua/docker_example/run.sh index 8b4d680..8e6274b 100755 --- a/lua/docker_example/run.sh +++ b/lua/docker_example/run.sh @@ -1,10 +1,10 @@ #!/usr/bin/env bash -# cd lua/docker_example && ./run.sh +# cd lua && cargo build --release && cd docker_example && ./run.sh set -v -docker kill jsonpath +[ "$(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 \ @@ -16,35 +16,5 @@ docker run -d --rm --name jsonpath \ -p 8080:80 \ openresty/openresty:bionic -paths=( - "$.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 )]" -) - -encoded_paths=() - -for i in "${!paths[@]}" -do - encoded_paths+=("$(node --eval "console.log(encodeURIComponent('${paths[$i]}'))")") - curl http://localhost:8080/1?path="${encoded_paths[$i]}" -done - -for i in "${!encoded_paths[@]}" -do - ab -n 1000 -c 10 http://localhost:8080/1?path="${encoded_paths[$i]}" > ab_results/"${paths[$i]}".txt -done - +sleep 1 +curl http://localhost:8080/3 diff --git a/lua/jsonpath.lua b/lua/jsonpath.lua index 4dba7ad..0c04b23 100644 --- a/lua/jsonpath.lua +++ b/lua/jsonpath.lua @@ -23,7 +23,9 @@ if existsVaiable('ngx') then _ngx = ngx else _ngx = {} - _ngx.log = function() end + _ngx.log = function(level, msg) + print('['..level..'] ' .. msg) + end end function module.compile(path)