Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/mpx/lua-cjson.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorMark Pulford <mark@kyne.com.au>2012-01-04 01:27:21 +0400
committerMark Pulford <mark@kyne.com.au>2012-03-04 12:24:34 +0400
commitc7fbb8e441b6a62e0d6d016add8ed6b44d90d981 (patch)
tree945b22e73281a8885ea9ae8953fdb5f200ec57f8 /tests
parent9f85048c49caca1d1774c96681546b178cb7ca78 (diff)
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-xtests/bench.lua43
-rw-r--r--tests/cjson-misc.lua26
-rwxr-xr-xtests/lua2json.lua34
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 @@
--
-- Mark Pulford <mark@kyne.com.au>
+local json_module = os.getenv("JSON_MODULE") or "cjson"
+
require "socket"
-local json = require "cjson"
+local json = require(json_module)
local misc = require "cjson-misc"
+local function find_func(mod, funcnames)
+ for _, v in ipairs(funcnames) do
+ if mod[v] then
+ return mod[v]
+ end
+ end
+
+ return nil
+end
+
+local json_encode = find_func(json, { "encode", "Encode", "to_string", "stringify", "json" })
+local json_decode = find_func(json, { "decode", "Decode", "to_value", "parse" })
+
function benchmark(tests, seconds, rep)
local function bench(func, iter)
-- collectgarbage("stop")
@@ -54,29 +69,33 @@ end
function bench_file(filename)
local data_json = misc.file_load(filename)
- local data_obj = json.decode(data_json)
+ local data_obj = json_decode(data_json)
- local function test_encode ()
- json.encode(data_obj)
+ local function test_encode()
+ json_encode(data_obj)
end
- local function test_decode ()
- json.decode(data_json)
+ local function test_decode()
+ json_decode(data_json)
end
- local tests = {
- encode = test_encode,
- decode = test_decode
- }
+ local tests = {}
+ if json_encode then tests.encode = test_encode end
+ if json_decode then tests.decode = test_decode end
return benchmark(tests, 0.1, 5)
end
-json.encode_keep_buffer(true)
+-- Optionally load any custom configuration required for this module
+local success, data = pcall(misc.file_load, string.format("bench-%s.lua", json_module))
+if success then
+ misc.run_script(data, _G)
+ configure(json)
+end
for i = 1, #arg do
local results = bench_file(arg[i])
for k, v in pairs(results) do
- print(string.format("%s: %s: %d", arg[i], k, v))
+ print(string.format("%s\t%s\t%d", arg[i], k, v))
end
end
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)
end
end
+-- Run a Lua script in a separate environment
+local function run_script(script, env)
+ local env = env or {}
+ local func
+
+ -- Use setfenv() if it exists, otherwise assume Lua 5.2 load() exists
+ if _G.setfenv then
+ func = loadstring(script)
+ if func then
+ setfenv(func, env)
+ end
+ else
+ func = load(script, nil, nil, env)
+ end
+
+ if func == nil then
+ error("Invalid syntax.")
+ end
+ func()
+
+ return env
+end
+
-- Export functions
return {
serialise_value = serialise_value,
@@ -247,7 +270,8 @@ return {
compare_values = compare_values,
run_test_summary = run_test_summary,
run_test = run_test,
- run_test_group = run_test_group
+ run_test_group = run_test_group,
+ run_script = run_script
}
-- 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 @@
local json = require "cjson"
local misc = require "cjson-misc"
-function get_lua_table(s)
- local env = {}
- local func
+local env = {
+ json = { null = json.null },
+ null = json.null
+}
- env.json = {}
- env.json.null = json.null
- env.null = json.null
- s = "data = " .. s
-
- -- Use setfenv() if it exists, otherwise assume Lua 5.2 load() exists
- if _G.setfenv then
- func = loadstring(s)
- if func then
- setfenv(func, env)
- end
- else
- func = load(s, nil, nil, env)
- end
-
- if func == nil then
- error("Invalid syntax. Failed to parse Lua table.")
- end
- func()
-
- return env.data
-end
-
-local t = get_lua_table(misc.file_load(arg[1]))
-print(json.encode(t))
+local t = misc.run_script("data = " .. misc.file_load(arg[1]), env)
+print(json.encode(t.data))
-- vi:ai et sw=4 ts=4: