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/common.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 '')
| -rw-r--r-- | tests/common.lua | 39 |
1 files changed, 0 insertions, 39 deletions
diff --git a/tests/common.lua b/tests/common.lua index 47e3f56..e08b6c2 100644 --- a/tests/common.lua +++ b/tests/common.lua | |||
| @@ -1,5 +1,4 @@ | |||
| 1 | require "cjson" | 1 | require "cjson" |
| 2 | require "posix" | ||
| 3 | 2 | ||
| 4 | -- Misc routines to assist with CJSON testing | 3 | -- Misc routines to assist with CJSON testing |
| 5 | -- | 4 | -- |
| @@ -120,44 +119,6 @@ function file_save(filename, data) | |||
| 120 | file:close() | 119 | file:close() |
| 121 | end | 120 | end |
| 122 | 121 | ||
| 123 | function gettimeofday() | ||
| 124 | local tv_sec, tv_usec = posix.gettimeofday() | ||
| 125 | |||
| 126 | return tv_sec + tv_usec / 1000000 | ||
| 127 | end | ||
| 128 | |||
| 129 | function benchmark(tests, iter, rep) | ||
| 130 | local function bench(func, iter) | ||
| 131 | collectgarbage("stop") | ||
| 132 | local t = gettimeofday() | ||
| 133 | for i = 1, iter do | ||
| 134 | func(i) | ||
| 135 | end | ||
| 136 | t = gettimeofday() - t | ||
| 137 | collectgarbage("restart") | ||
| 138 | return (iter / t) | ||
| 139 | end | ||
| 140 | |||
| 141 | local test_results = {} | ||
| 142 | for name, func in pairs(tests) do | ||
| 143 | -- k(number), v(string) | ||
| 144 | -- k(string), v(function) | ||
| 145 | -- k(number), v(function) | ||
| 146 | if type(func) == "string" then | ||
| 147 | name = func | ||
| 148 | func = _G[name] | ||
| 149 | end | ||
| 150 | local result = {} | ||
| 151 | for i = 1, rep do | ||
| 152 | result[i] = bench(func, iter) | ||
| 153 | end | ||
| 154 | table.sort(result) | ||
| 155 | test_results[name] = result[rep] | ||
| 156 | end | ||
| 157 | |||
| 158 | return test_results | ||
| 159 | end | ||
| 160 | |||
| 161 | function compare_values(val1, val2) | 122 | function compare_values(val1, val2) |
| 162 | local type1 = type(val1) | 123 | local type1 = type(val1) |
| 163 | local type2 = type(val2) | 124 | local type2 = type(val2) |
