aboutsummaryrefslogtreecommitdiff
path: root/tests/bench.lua
diff options
context:
space:
mode:
Diffstat (limited to 'tests/bench.lua')
-rwxr-xr-xtests/bench.lua43
1 files changed, 31 insertions, 12 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
9local json_module = os.getenv("JSON_MODULE") or "cjson"
10
9require "socket" 11require "socket"
10local json = require "cjson" 12local json = require(json_module)
11local misc = require "cjson-misc" 13local misc = require "cjson-misc"
12 14
15local 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
23end
24
25local json_encode = find_func(json, { "encode", "Encode", "to_string", "stringify", "json" })
26local json_decode = find_func(json, { "decode", "Decode", "to_value", "parse" })
27
13function benchmark(tests, seconds, rep) 28function 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
55function bench_file(filename) 70function 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)
72end 86end
73 87
74json.encode_keep_buffer(true) 88-- Optionally load any custom configuration required for this module
89local success, data = pcall(misc.file_load, string.format("bench-%s.lua", json_module))
90if success then
91 misc.run_script(data, _G)
92 configure(json)
93end
75 94
76for i = 1, #arg do 95for 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
81end 100end
82 101