summaryrefslogtreecommitdiff
path: root/Data/BuiltIn/Libraries/LiteJson/bench/bench_encode.lua
diff options
context:
space:
mode:
authorchai <chaifix@163.com>2021-11-15 11:54:17 +0800
committerchai <chaifix@163.com>2021-11-15 11:54:17 +0800
commit30f2f46474bf4eda5f10d4c64a07cde01d469f66 (patch)
tree6ff2ed3262037b3c9bae2d2b9059a1d65773f31c /Data/BuiltIn/Libraries/LiteJson/bench/bench_encode.lua
parent4c36bed53fe63ae6056730b3ecad2573f03d88f8 (diff)
*rename DefaultContent -> BuiltIn
Diffstat (limited to 'Data/BuiltIn/Libraries/LiteJson/bench/bench_encode.lua')
-rw-r--r--Data/BuiltIn/Libraries/LiteJson/bench/bench_encode.lua63
1 files changed, 63 insertions, 0 deletions
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)