diff options
| author | Mark Pulford <mark@kyne.com.au> | 2012-01-04 07:57:21 +1030 |
|---|---|---|
| committer | Mark Pulford <mark@kyne.com.au> | 2012-03-04 18:54:34 +1030 |
| commit | c7fbb8e441b6a62e0d6d016add8ed6b44d90d981 (patch) | |
| tree | 945b22e73281a8885ea9ae8953fdb5f200ec57f8 /tests | |
| parent | 9f85048c49caca1d1774c96681546b178cb7ca78 (diff) | |
| download | lua-cjson-c7fbb8e441b6a62e0d6d016add8ed6b44d90d981.tar.gz lua-cjson-c7fbb8e441b6a62e0d6d016add8ed6b44d90d981.tar.bz2 lua-cjson-c7fbb8e441b6a62e0d6d016add8ed6b44d90d981.zip | |
Update bench.lua to support different JSON modules
- Select via JSON_MODULE environment variable (default "cjson")
- Custom runtime configuration can be stored in bench-MODNAME.lua
- Add run_script() to cjson-misc and update lua2cjson.lua
Diffstat (limited to 'tests')
| -rwxr-xr-x | tests/bench.lua | 43 | ||||
| -rw-r--r-- | tests/cjson-misc.lua | 26 | ||||
| -rwxr-xr-x | tests/lua2json.lua | 34 |
3 files changed, 62 insertions, 41 deletions
diff --git a/tests/bench.lua b/tests/bench.lua index 2b5177b..0709209 100755 --- a/tests/bench.lua +++ b/tests/bench.lua | |||
| @@ -6,10 +6,25 @@ | |||
| 6 | -- | 6 | -- |
| 7 | -- Mark Pulford <mark@kyne.com.au> | 7 | -- Mark Pulford <mark@kyne.com.au> |
| 8 | 8 | ||
| 9 | local json_module = os.getenv("JSON_MODULE") or "cjson" | ||
| 10 | |||
| 9 | require "socket" | 11 | require "socket" |
| 10 | local json = require "cjson" | 12 | local json = require(json_module) |
| 11 | local misc = require "cjson-misc" | 13 | local misc = require "cjson-misc" |
| 12 | 14 | ||
| 15 | local function find_func(mod, funcnames) | ||
| 16 | for _, v in ipairs(funcnames) do | ||
| 17 | if mod[v] then | ||
| 18 | return mod[v] | ||
| 19 | end | ||
| 20 | end | ||
| 21 | |||
| 22 | return nil | ||
| 23 | end | ||
| 24 | |||
| 25 | local json_encode = find_func(json, { "encode", "Encode", "to_string", "stringify", "json" }) | ||
| 26 | local json_decode = find_func(json, { "decode", "Decode", "to_value", "parse" }) | ||
| 27 | |||
| 13 | function benchmark(tests, seconds, rep) | 28 | function benchmark(tests, seconds, rep) |
| 14 | local function bench(func, iter) | 29 | local function bench(func, iter) |
| 15 | -- collectgarbage("stop") | 30 | -- collectgarbage("stop") |
| @@ -54,29 +69,33 @@ end | |||
| 54 | 69 | ||
| 55 | function bench_file(filename) | 70 | function bench_file(filename) |
| 56 | local data_json = misc.file_load(filename) | 71 | local data_json = misc.file_load(filename) |
| 57 | local data_obj = json.decode(data_json) | 72 | local data_obj = json_decode(data_json) |
| 58 | 73 | ||
| 59 | local function test_encode () | 74 | local function test_encode() |
| 60 | json.encode(data_obj) | 75 | json_encode(data_obj) |
| 61 | end | 76 | end |
| 62 | local function test_decode () | 77 | local function test_decode() |
| 63 | json.decode(data_json) | 78 | json_decode(data_json) |
| 64 | end | 79 | end |
| 65 | 80 | ||
| 66 | local tests = { | 81 | local tests = {} |
| 67 | encode = test_encode, | 82 | if json_encode then tests.encode = test_encode end |
| 68 | decode = test_decode | 83 | if json_decode then tests.decode = test_decode end |
| 69 | } | ||
| 70 | 84 | ||
| 71 | return benchmark(tests, 0.1, 5) | 85 | return benchmark(tests, 0.1, 5) |
| 72 | end | 86 | end |
| 73 | 87 | ||
| 74 | json.encode_keep_buffer(true) | 88 | -- Optionally load any custom configuration required for this module |
| 89 | local success, data = pcall(misc.file_load, string.format("bench-%s.lua", json_module)) | ||
| 90 | if success then | ||
| 91 | misc.run_script(data, _G) | ||
| 92 | configure(json) | ||
| 93 | end | ||
| 75 | 94 | ||
| 76 | for i = 1, #arg do | 95 | for i = 1, #arg do |
| 77 | local results = bench_file(arg[i]) | 96 | local results = bench_file(arg[i]) |
| 78 | for k, v in pairs(results) do | 97 | for k, v in pairs(results) do |
| 79 | print(string.format("%s: %s: %d", arg[i], k, v)) | 98 | print(string.format("%s\t%s\t%d", arg[i], k, v)) |
| 80 | end | 99 | end |
| 81 | end | 100 | end |
| 82 | 101 | ||
diff --git a/tests/cjson-misc.lua b/tests/cjson-misc.lua index d8bfe5e..c4462ca 100644 --- a/tests/cjson-misc.lua +++ b/tests/cjson-misc.lua | |||
| @@ -239,6 +239,29 @@ local function run_test_group(testgroup, tests) | |||
| 239 | end | 239 | end |
| 240 | end | 240 | end |
| 241 | 241 | ||
| 242 | -- Run a Lua script in a separate environment | ||
| 243 | local function run_script(script, env) | ||
| 244 | local env = env or {} | ||
| 245 | local func | ||
| 246 | |||
| 247 | -- Use setfenv() if it exists, otherwise assume Lua 5.2 load() exists | ||
| 248 | if _G.setfenv then | ||
| 249 | func = loadstring(script) | ||
| 250 | if func then | ||
| 251 | setfenv(func, env) | ||
| 252 | end | ||
| 253 | else | ||
| 254 | func = load(script, nil, nil, env) | ||
| 255 | end | ||
| 256 | |||
| 257 | if func == nil then | ||
| 258 | error("Invalid syntax.") | ||
| 259 | end | ||
| 260 | func() | ||
| 261 | |||
| 262 | return env | ||
| 263 | end | ||
| 264 | |||
| 242 | -- Export functions | 265 | -- Export functions |
| 243 | return { | 266 | return { |
| 244 | serialise_value = serialise_value, | 267 | serialise_value = serialise_value, |
| @@ -247,7 +270,8 @@ return { | |||
| 247 | compare_values = compare_values, | 270 | compare_values = compare_values, |
| 248 | run_test_summary = run_test_summary, | 271 | run_test_summary = run_test_summary, |
| 249 | run_test = run_test, | 272 | run_test = run_test, |
| 250 | run_test_group = run_test_group | 273 | run_test_group = run_test_group, |
| 274 | run_script = run_script | ||
| 251 | } | 275 | } |
| 252 | 276 | ||
| 253 | -- vi:ai et sw=4 ts=4: | 277 | -- vi:ai et sw=4 ts=4: |
diff --git a/tests/lua2json.lua b/tests/lua2json.lua index ebe7380..2a9ddf9 100755 --- a/tests/lua2json.lua +++ b/tests/lua2json.lua | |||
| @@ -9,34 +9,12 @@ | |||
| 9 | local json = require "cjson" | 9 | local json = require "cjson" |
| 10 | local misc = require "cjson-misc" | 10 | local misc = require "cjson-misc" |
| 11 | 11 | ||
| 12 | function get_lua_table(s) | 12 | local env = { |
| 13 | local env = {} | 13 | json = { null = json.null }, |
| 14 | local func | 14 | null = json.null |
| 15 | } | ||
| 15 | 16 | ||
| 16 | env.json = {} | 17 | local t = misc.run_script("data = " .. misc.file_load(arg[1]), env) |
| 17 | env.json.null = json.null | 18 | print(json.encode(t.data)) |
| 18 | env.null = json.null | ||
| 19 | s = "data = " .. s | ||
| 20 | |||
| 21 | -- Use setfenv() if it exists, otherwise assume Lua 5.2 load() exists | ||
| 22 | if _G.setfenv then | ||
| 23 | func = loadstring(s) | ||
| 24 | if func then | ||
| 25 | setfenv(func, env) | ||
| 26 | end | ||
| 27 | else | ||
| 28 | func = load(s, nil, nil, env) | ||
| 29 | end | ||
| 30 | |||
| 31 | if func == nil then | ||
| 32 | error("Invalid syntax. Failed to parse Lua table.") | ||
| 33 | end | ||
| 34 | func() | ||
| 35 | |||
| 36 | return env.data | ||
| 37 | end | ||
| 38 | |||
| 39 | local t = get_lua_table(misc.file_load(arg[1])) | ||
| 40 | print(json.encode(t)) | ||
| 41 | 19 | ||
| 42 | -- vi:ai et sw=4 ts=4: | 20 | -- vi:ai et sw=4 ts=4: |
