summaryrefslogtreecommitdiff
path: root/Data/DefaultContent/Libraries/LiteJson/bench/bench_encode.lua
blob: 426f7c8c16da71219205b4c6a17689472526c088 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
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)