Added benchmark scripts

pull/6/head
rxi 2015-08-13 19:04:55 +01:00
parent 31564c4195
commit c8e460b442
5 changed files with 206 additions and 0 deletions

6
bench/bench_all.lua Normal file
View File

@ -0,0 +1,6 @@
print("[decode]")
loadfile("bench_decode.lua")()
print()
print("[encode]")
loadfile("bench_encode.lua")()

63
bench/bench_decode.lua Normal file
View File

@ -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
bench/bench_encode.lua Normal file
View File

@ -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
bench/get_json_libs.sh Executable file
View File

@ -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
bench/util/bench.lua Normal file
View File

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