-
Notifications
You must be signed in to change notification settings - Fork 8
/
bench.lua
142 lines (120 loc) · 2.91 KB
/
bench.lua
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
local simdjson = require("simdjson")
local cjson = require("cjson")
local dkjson = require("dkjson")
local rapidjson = require("rapidjson")
local ftcsv = require("ftcsv")
local inspect = require("inspect")
-- load an entire file into memory
local function loadFile(textFile)
local file = io.open(textFile, "r")
if not file then error("ftcsv: File not found at " .. textFile) end
local lines = file:read("*all")
file:close()
return lines
end
local csvfiles = {
"twitter_api_response.json",
"twitter_timeline.json",
"numbers.json",
"update-center.json",
"mesh.json",
"canada.json",
"gsoc-2018.json",
}
local function set(t)
local newSet = {}
for _, v in ipairs(t) do
newSet[v] = true
end
return newSet
end
local jsonchecker = {
"apache_builds.json",
"canada.json",
"citm_catalog.json",
"github_events.json",
"google_maps_api_compact_response.json",
"google_maps_api_response.json",
"gsoc-2018.json",
"instruments.json",
"marine_ik.json",
"mesh.json",
"mesh.pretty.json",
"numbers.json",
"random.json",
"repeat.json",
"twitter_api_compact_response.json",
"twitter_api_response.json",
"twitterescaped.json",
"twitter.json",
"twitter_timeline.json",
"update-center.json",
"small/adversarial.json",
"small/demo.json",
"small/flatadversarial.json",
"small/smalldemo.json",
"small/truenull.json"
}
local function sum(t)
local totalTime = 0
for _, time in ipairs(t) do
totalTime = totalTime + time
end
return totalTime
end
local function average(t)
return sum(t) / #t
end
local function timeIt(fn, contents)
local times = {}
local start, elapsed
for i=1,100 do
start = os.clock()
fn(contents)
elapsed = os.clock() - start
table.insert(times, elapsed)
end
return average(times)
end
local totalTimes = {
simdjson = 0,
cjson = 0,
dkjson = 0,
rapidjson = 0
}
local csvFileSet = set(csvfiles)
local outputCsv = {}
for i,filename in ipairs(jsonchecker) do
local row = {}
local testFile = "jsonexamples/" .. filename
local json_contents = loadFile(testFile)
local time
print(testFile, "Bytes: " .. #json_contents)
row["filename"] = filename
time = timeIt(simdjson.parse, json_contents)
print("simd", time)
row["simdjson"] = time
totalTimes["simdjson"] = totalTimes["simdjson"] + time
time = timeIt(cjson.decode, json_contents)
print("cjson", time)
row["cjson"] = time
totalTimes["cjson"] = totalTimes["cjson"] + time
time = timeIt(dkjson.decode, json_contents)
print("dkjson", time)
row["dkjson"] = time
totalTimes["dkjson"] = totalTimes["dkjson"] + time
time = timeIt(rapidjson.decode, json_contents)
print("rapidjson", time)
row["rapidjson"] = time
totalTimes["rapidjson"] = totalTimes["rapidjson"] + time
print("")
if csvFileSet[filename] then
table.insert(outputCsv, row)
end
end
local fileOutput = ftcsv.encode(outputCsv, ",")
local file = assert(io.open("lua_test.csv", "w"))
file:write(fileOutput)
file:close()
print("Totals:")
print(inspect(totalTimes))