From a966d2fd2aaff7651d7928fb8381a17bf1f5f517 Mon Sep 17 00:00:00 2001 From: Mark Pulford Date: Mon, 16 May 2011 19:21:06 +0930 Subject: 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. --- tests/bench.lua | 34 ++++++++++++++++++++++++++++++++++ tests/common.lua | 39 --------------------------------------- 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 @@ -- Mark Pulford require "common" +require "socket" local json = require "cjson" +function benchmark(tests, iter, rep) + local function bench(func, iter) + collectgarbage("collect") + local t = socket.gettime() + for i = 1, iter do + func(i) + end + t = socket.gettime() - t + return (iter / t) + end + + local test_results = {} + for name, func in pairs(tests) do + -- k(number), v(string) + -- k(string), v(function) + -- k(number), v(function) + if type(func) == "string" then + name = func + func = _G[name] + end + local result = {} + for i = 1, rep do + result[i] = bench(func, iter) + end + table.sort(result) + test_results[name] = result[rep] + end + + return test_results +end + function bench_file(filename) local data_json = file_load(filename) local data_obj = json.decode(data_json) @@ -28,6 +60,8 @@ function bench_file(filename) return benchmark(tests, 5000, 5) end +cjson.encode_keep_buffer(true) + for i = 1, #arg do local results = bench_file(arg[i]) 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 @@ require "cjson" -require "posix" -- Misc routines to assist with CJSON testing -- @@ -120,44 +119,6 @@ function file_save(filename, data) file:close() end -function gettimeofday() - local tv_sec, tv_usec = posix.gettimeofday() - - return tv_sec + tv_usec / 1000000 -end - -function benchmark(tests, iter, rep) - local function bench(func, iter) - collectgarbage("stop") - local t = gettimeofday() - for i = 1, iter do - func(i) - end - t = gettimeofday() - t - collectgarbage("restart") - return (iter / t) - end - - local test_results = {} - for name, func in pairs(tests) do - -- k(number), v(string) - -- k(string), v(function) - -- k(number), v(function) - if type(func) == "string" then - name = func - func = _G[name] - end - local result = {} - for i = 1, rep do - result[i] = bench(func, iter) - end - table.sort(result) - test_results[name] = result[rep] - end - - return test_results -end - function compare_values(val1, val2) local type1 = type(val1) local type2 = type(val2) -- cgit v1.2.3-55-g6feb