summaryrefslogtreecommitdiff
path: root/Data/BuiltIn/Libraries/LiteJson/bench
diff options
context:
space:
mode:
Diffstat (limited to 'Data/BuiltIn/Libraries/LiteJson/bench')
-rw-r--r--Data/BuiltIn/Libraries/LiteJson/bench/bench_all.lua6
-rw-r--r--Data/BuiltIn/Libraries/LiteJson/bench/bench_decode.lua75
-rw-r--r--Data/BuiltIn/Libraries/LiteJson/bench/bench_encode.lua63
-rw-r--r--Data/BuiltIn/Libraries/LiteJson/bench/get_json_libs.sh15
-rw-r--r--Data/BuiltIn/Libraries/LiteJson/bench/util/bench.lua62
5 files changed, 221 insertions, 0 deletions
diff --git a/Data/BuiltIn/Libraries/LiteJson/bench/bench_all.lua b/Data/BuiltIn/Libraries/LiteJson/bench/bench_all.lua
new file mode 100644
index 0000000..3f99e99
--- /dev/null
+++ b/Data/BuiltIn/Libraries/LiteJson/bench/bench_all.lua
@@ -0,0 +1,6 @@
+
+print("[decode]")
+loadfile("bench_decode.lua")()
+print()
+print("[encode]")
+loadfile("bench_encode.lua")()
diff --git a/Data/BuiltIn/Libraries/LiteJson/bench/bench_decode.lua b/Data/BuiltIn/Libraries/LiteJson/bench/bench_decode.lua
new file mode 100644
index 0000000..6c2e93a
--- /dev/null
+++ b/Data/BuiltIn/Libraries/LiteJson/bench/bench_decode.lua
@@ -0,0 +1,75 @@
+local bench = require "util.bench"
+
+
+local libs = {
+ "../json.lua", -- https://github.com/rxi/json.lua
+ "dkjson.lua", -- https://github.com/LuaDist/dkjson
+ "jfjson.lua", -- http://regex.info/blog/lua/json
+ --"json4lua.lua", -- https://github.com/craigmj/json4lua
+}
+
+
+-- JSON string: wikipedia example stored 1000 times in an array
+local text = "[" .. string.rep([[{
+ "firstName": "John",
+ "lastName": "Smith",
+ "isAlive": true,
+ "age": 25,
+ "address": {
+ "streetAddress": "21 2nd Street",
+ "city": "New York",
+ "state": "NY",
+ "postalCode": "10021-3100"
+ },
+ "phoneNumbers": [
+ {
+ "type": "home",
+ "number": "212 555-1234"
+ },
+ {
+ "type": "office",
+ "number": "646 555-4567"
+ }
+ ],
+ "children": [],
+ "spouse": null
+}, ]], 1000):sub(1, -3) .. "]"
+
+
+-- As this is meant to be a pure Lua benchmark, we remove the ability to
+-- require 'lpeg' so dkjson doesn't use it for parsing. (Incidentally json.lua
+-- seems to outperform libraries which use lpeg when both are using LuaJIT)
+local _require = require
+require = function(modname)
+ if modname == "lpeg" then error() end
+ return _require(modname)
+end
+
+-- Run benchmarks, store results
+local results = {}
+
+for i, name in ipairs(libs) do
+ local f = loadfile(name)
+ if not f then
+ error( "failed to load '" .. name .. "'; run './get_json_libs.sh'" )
+ end
+ local json = f()
+
+ -- Remap functions to work for jfjson.lua
+ if name == "jfjson.lua" then
+ local _encode, _decode = json.encode, json.decode
+ json.encode = function(...) return _encode(json, ...) end
+ json.decode = function(...) return _decode(json, ...) end
+ end
+
+ -- Warmup (for LuaJIT)
+ bench.run(name, 1, function() json.decode(text) end)
+
+ -- Run and push results
+ local res = bench.run(name, 10, function() json.decode(text) end)
+ table.insert(results, res)
+end
+
+
+bench.print_system_info()
+bench.print_results(results)
diff --git a/Data/BuiltIn/Libraries/LiteJson/bench/bench_encode.lua b/Data/BuiltIn/Libraries/LiteJson/bench/bench_encode.lua
new file mode 100644
index 0000000..426f7c8
--- /dev/null
+++ b/Data/BuiltIn/Libraries/LiteJson/bench/bench_encode.lua
@@ -0,0 +1,63 @@
+local bench = require "util.bench"
+
+
+local libs = {
+ "../json.lua", -- https://github.com/rxi/json.lua
+ "dkjson.lua", -- https://github.com/LuaDist/dkjson
+ "jfjson.lua", -- http://regex.info/blog/lua/json
+ "json4lua.lua", -- https://github.com/craigmj/json4lua
+}
+
+
+-- Build table which will be encoded: wikipedia example stored 1000 times
+local data = {}
+for i = 1, 1000 do
+ table.insert(data, {
+ firstName = "John",
+ lastName = "Smith",
+ isAlive = true,
+ age = 25,
+ address = {
+ streetAddress = "21 2nd Street",
+ city = "New York",
+ state = "NY",
+ postalCode = "10021-3100"
+ },
+ phoneNumbers = {
+ { type = "home", number = "212 555-1234" },
+ { type = "office", number = "646 555-4567" }
+ },
+ children = {},
+ spouse = nil
+ })
+end
+
+
+-- Run benchmarks
+local results = {}
+
+for i, name in ipairs(libs) do
+ local f = loadfile(name)
+ if not f then
+ error( "failed to load '" .. name .. "'; run './get_json_libs.sh'" )
+ end
+ local json = f()
+
+ -- Handle special cases
+ if name == "jfjson.lua" then
+ local _encode, _decode = json.encode, json.decode
+ json.encode = function(...) return _encode(json, ...) end
+ json.decode = function(...) return _decode(json, ...) end
+ end
+
+ -- Warmup (for LuaJIT)
+ bench.run(name, 1, function() json.encode(data) end)
+
+ -- Run and push results
+ local res = bench.run(name, 10, function() json.encode(data) end)
+ table.insert(results, res)
+end
+
+
+bench.print_system_info()
+bench.print_results(results)
diff --git a/Data/BuiltIn/Libraries/LiteJson/bench/get_json_libs.sh b/Data/BuiltIn/Libraries/LiteJson/bench/get_json_libs.sh
new file mode 100644
index 0000000..ecb4525
--- /dev/null
+++ b/Data/BuiltIn/Libraries/LiteJson/bench/get_json_libs.sh
@@ -0,0 +1,15 @@
+#!/bin/bash
+# Downloads other JSON libraries for use in the benchmark scripts
+
+# Remove libraries
+rm dkjson.lua 2>/dev/null
+rm jfjson.lua 2>/dev/null
+rm json4lua.lua 2>/dev/null
+
+# Get libraries
+echo "Downloading json libs..."
+curl -sS -o dkjson.lua "https://raw.githubusercontent.com/LuaDist/dkjson/master/dkjson.lua"
+curl -sS -o json4lua.lua "https://raw.githubusercontent.com/craigmj/json4lua/master/json/json.lua"
+curl -sS -o jfjson.lua "http://regex.info/code/JSON.lua"
+
+echo "Done"
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