Lua-Json解析的性能问题,lua-json解析性能


1:环境

基于openresty建站

openresty版本1.11.2.5

LuaJIT 版本2.1.0

Luarocks版本2.4.4

通过Luarocks安装cjson()openresty官网说自带cjson模块,这个没有测试,反正我这里是通过Luarocks安装cjson。

2:解析Json

采用ngx.say输出json,在body_filter_by_lua_file中解析

local cjson_safe = require("cjson.safe")

local chunk = ngx.arg[1]

local table = cjaon_safe.decode(chunk)

if table ~= nil and next(table) ~= nil then

print(table['status'])

end

QPS ab压测

curl "127.0.0.1/"

ab -n 5000 -c 50 "127.0.0.1"

不需要解析Json的: qps:7500(较为复杂的Json),qps:8000(较为简单的Json)

需要解析Json的:qps:5300(较为复杂的Json), qps:6400(较为简单的Json)

从相对值来看,Json解析性能消耗巨大

3:查阅资料

春哥推荐线上使用lua-resty-json

https://groups.google.com/forum/#!topic/openresty/mxJgETB_INI

lua-resty-json

https://github.com/cloudflare/lua-resty-json

不标准的基准测试

https://github.com/bungle/lua-resty-libcjson/issues/3

4:结论

lua-resty-json is the fastest, but can crash LuaJIT VM.

lua cjson lib is almost as fast, but more reliable on larger files

推荐使用cjson

https://github.com/openresty/lua-cjson/

5:lua-resty-json的使用手册,github上的用例没看懂,而且项目也没怎么维护了,所以不是很推荐使用,这里写个我的用例。

下载:https://github.com/openresty/lua-cjson

解包: unzip

cd to path

make

cp json_decoder.lua /usr/local/openresty/lualib/

cp libljson.so /usr/local/openresty/lualib/

example of lua usage(in body_filter_by_lua_file):

--以个人的json_decoder的位置为准

local resty_decoder = require("resty.json_decoder")

local instance = resty_decoder.new()

local chunk = ngx.arg[1]

local table, err = instance.decode(instance, chunk)

 

相关内容

    暂无相关文章