diff options
-rwxr-xr-x | tests/bench.lua | 14 |
1 files changed, 12 insertions, 2 deletions
diff --git a/tests/bench.lua b/tests/bench.lua index 0e2a75c..fdd0bb0 100755 --- a/tests/bench.lua +++ b/tests/bench.lua | |||
@@ -8,9 +8,10 @@ | |||
8 | 8 | ||
9 | require "common" | 9 | require "common" |
10 | require "socket" | 10 | require "socket" |
11 | |||
11 | local json = require "cjson" | 12 | local json = require "cjson" |
12 | 13 | ||
13 | function benchmark(tests, iter, rep) | 14 | function benchmark(tests, seconds, rep) |
14 | local function bench(func, iter) | 15 | local function bench(func, iter) |
15 | -- collectgarbage("stop") | 16 | -- collectgarbage("stop") |
16 | collectgarbage("collect") | 17 | collectgarbage("collect") |
@@ -23,6 +24,14 @@ function benchmark(tests, iter, rep) | |||
23 | return (iter / t) | 24 | return (iter / t) |
24 | end | 25 | end |
25 | 26 | ||
27 | -- Roughly calculate the number of interations required | ||
28 | -- to obtain a particular time period. | ||
29 | local function calc_iter(func, seconds) | ||
30 | local base_iter = 10 | ||
31 | local rate = (bench(func, base_iter) + bench(func, base_iter)) / 2 | ||
32 | return math.ceil(seconds * rate) | ||
33 | end | ||
34 | |||
26 | local test_results = {} | 35 | local test_results = {} |
27 | for name, func in pairs(tests) do | 36 | for name, func in pairs(tests) do |
28 | -- k(number), v(string) | 37 | -- k(number), v(string) |
@@ -32,6 +41,7 @@ function benchmark(tests, iter, rep) | |||
32 | name = func | 41 | name = func |
33 | func = _G[name] | 42 | func = _G[name] |
34 | end | 43 | end |
44 | local iter = calc_iter(func, seconds) | ||
35 | local result = {} | 45 | local result = {} |
36 | for i = 1, rep do | 46 | for i = 1, rep do |
37 | result[i] = bench(func, iter) | 47 | result[i] = bench(func, iter) |
@@ -59,7 +69,7 @@ function bench_file(filename) | |||
59 | decode = test_decode | 69 | decode = test_decode |
60 | } | 70 | } |
61 | 71 | ||
62 | return benchmark(tests, 5000, 5) | 72 | return benchmark(tests, 0.1, 5) |
63 | end | 73 | end |
64 | 74 | ||
65 | cjson.encode_keep_buffer(true) | 75 | cjson.encode_keep_buffer(true) |