diff options
Diffstat (limited to 'Data/BuiltIn/Libraries/LiteJson/bench/util/bench.lua')
-rw-r--r-- | Data/BuiltIn/Libraries/LiteJson/bench/util/bench.lua | 62 |
1 files changed, 62 insertions, 0 deletions
diff --git a/Data/BuiltIn/Libraries/LiteJson/bench/util/bench.lua b/Data/BuiltIn/Libraries/LiteJson/bench/util/bench.lua new file mode 100644 index 0000000..9f02738 --- /dev/null +++ b/Data/BuiltIn/Libraries/LiteJson/bench/util/bench.lua @@ -0,0 +1,62 @@ +local bench = {} + +local unpack = unpack or table.unpack +local fmt = string.format + + +function bench.run(name, count, func) + -- Run bench + local res = {} + for i = 1, count do + local start_time = os.clock() + func() + table.insert(res, (os.clock() - start_time)) + end + -- Calculate average + local avg = 0 + for i, v in ipairs(res) do + avg = avg + v + end + avg = avg / #res + -- Build and return result table + return { + name = name, + avg = avg, + min = math.min(unpack(res)), + max = math.max(unpack(res)), + all = res, + } +end + + +function bench.get_cpu_name() + local fp = io.open("/proc/cpuinfo", "rb") + if not fp then + return "unknown" + end + local text = fp:read("*a") + return text:match("model name%s*:%s*(.-)\n") +end + + +function bench.print_system_info() + print( fmt("Lua version : %s", jit and jit.version or _VERSION) ) + print( fmt("CPU name : %s", bench.get_cpu_name()) ) +end + + +function bench.print_results(results) + -- Find best average + local best = math.huge + for i, v in ipairs(results) do + best = math.min(best, v.avg) + end + -- Print results + for i, v in ipairs(results) do + print( fmt("%-13s : %.03gs [x%1.3g] (min: %.03gs, max %.03gs)", + v.name, v.avg, v.avg / best, v.min, v.max) ) + end +end + + +return bench |