diff options
| author | Mark Pulford <mark@kyne.com.au> | 2011-05-16 19:21:06 +0930 |
|---|---|---|
| committer | Mark Pulford <mark@kyne.com.au> | 2011-05-16 19:21:06 +0930 |
| commit | a966d2fd2aaff7651d7928fb8381a17bf1f5f517 (patch) | |
| tree | f40aab098067d55f0a21cb1f2b2fa3271f218250 /tests/bench.lua | |
| parent | c88eb62e287fa99f78c942ba551f35b13916acdc (diff) | |
| download | lua-cjson-a966d2fd2aaff7651d7928fb8381a17bf1f5f517.tar.gz lua-cjson-a966d2fd2aaff7651d7928fb8381a17bf1f5f517.tar.bz2 lua-cjson-a966d2fd2aaff7651d7928fb8381a17bf1f5f517.zip | |
Move all benchmark code into bench.lua
Move benchmark() into bench.lua since it not used elsewhere.
Replace posix.gettimeofday() with socket.gettime() to improve portability.
Diffstat (limited to '')
| -rwxr-xr-x | tests/bench.lua | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/tests/bench.lua b/tests/bench.lua index 847365b..c1808cf 100755 --- a/tests/bench.lua +++ b/tests/bench.lua | |||
| @@ -7,8 +7,40 @@ | |||
| 7 | -- Mark Pulford <mark@kyne.com.au> | 7 | -- Mark Pulford <mark@kyne.com.au> |
| 8 | 8 | ||
| 9 | require "common" | 9 | require "common" |
| 10 | require "socket" | ||
| 10 | local json = require "cjson" | 11 | local json = require "cjson" |
| 11 | 12 | ||
| 13 | function benchmark(tests, iter, rep) | ||
| 14 | local function bench(func, iter) | ||
| 15 | collectgarbage("collect") | ||
| 16 | local t = socket.gettime() | ||
| 17 | for i = 1, iter do | ||
| 18 | func(i) | ||
| 19 | end | ||
| 20 | t = socket.gettime() - t | ||
| 21 | return (iter / t) | ||
| 22 | end | ||
| 23 | |||
| 24 | local test_results = {} | ||
| 25 | for name, func in pairs(tests) do | ||
| 26 | -- k(number), v(string) | ||
| 27 | -- k(string), v(function) | ||
| 28 | -- k(number), v(function) | ||
| 29 | if type(func) == "string" then | ||
| 30 | name = func | ||
| 31 | func = _G[name] | ||
| 32 | end | ||
| 33 | local result = {} | ||
| 34 | for i = 1, rep do | ||
| 35 | result[i] = bench(func, iter) | ||
| 36 | end | ||
| 37 | table.sort(result) | ||
| 38 | test_results[name] = result[rep] | ||
| 39 | end | ||
| 40 | |||
| 41 | return test_results | ||
| 42 | end | ||
| 43 | |||
| 12 | function bench_file(filename) | 44 | function bench_file(filename) |
| 13 | local data_json = file_load(filename) | 45 | local data_json = file_load(filename) |
| 14 | local data_obj = json.decode(data_json) | 46 | local data_obj = json.decode(data_json) |
| @@ -28,6 +60,8 @@ function bench_file(filename) | |||
| 28 | return benchmark(tests, 5000, 5) | 60 | return benchmark(tests, 5000, 5) |
| 29 | end | 61 | end |
| 30 | 62 | ||
| 63 | cjson.encode_keep_buffer(true) | ||
| 64 | |||
| 31 | for i = 1, #arg do | 65 | for i = 1, #arg do |
| 32 | local results = bench_file(arg[i]) | 66 | local results = bench_file(arg[i]) |
| 33 | for k, v in pairs(results) do | 67 | for k, v in pairs(results) do |
