Skip to content

Commit

Permalink
Added benchmark scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
rxi committed Aug 13, 2015
1 parent 31564c4 commit c8e460b
Show file tree
Hide file tree
Showing 5 changed files with 206 additions and 0 deletions.
6 changes: 6 additions & 0 deletions bench/bench_all.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

print("[decode]")
loadfile("bench_decode.lua")()
print()
print("[encode]")
loadfile("bench_encode.lua")()
63 changes: 63 additions & 0 deletions bench/bench_decode.lua
Original file line number Diff line number Diff line change
@@ -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
}


-- 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) .. "]"


-- 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()

-- 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

-- 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)
60 changes: 60 additions & 0 deletions bench/bench_encode.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
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

-- 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)
15 changes: 15 additions & 0 deletions bench/get_json_libs.sh
Original file line number Diff line number Diff line change
@@ -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"
62 changes: 62 additions & 0 deletions bench/util/bench.lua
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit c8e460b

Please sign in to comment.