diff options
Diffstat (limited to 'tests/bench.lua')
-rwxr-xr-x | tests/bench.lua | 43 |
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 | ||
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 | ||