aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xtests/bench.lua34
-rw-r--r--tests/common.lua39
2 files changed, 34 insertions, 39 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
9require "common" 9require "common"
10require "socket"
10local json = require "cjson" 11local json = require "cjson"
11 12
13function 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
42end
43
12function bench_file(filename) 44function 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)
29end 61end
30 62
63cjson.encode_keep_buffer(true)
64
31for i = 1, #arg do 65for 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
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 @@
1require "cjson" 1require "cjson"
2require "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()
121end 120end
122 121
123function gettimeofday()
124 local tv_sec, tv_usec = posix.gettimeofday()
125
126 return tv_sec + tv_usec / 1000000
127end
128
129function 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
159end
160
161function compare_values(val1, val2) 122function compare_values(val1, val2)
162 local type1 = type(val1) 123 local type1 = type(val1)
163 local type2 = type(val2) 124 local type2 = type(val2)