-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathbenchmark.lua
77 lines (59 loc) · 1.46 KB
/
benchmark.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
jit.opt.start("loopunroll=1000", "maxrecord=5000", "maxmcode=1024")
package.path = "lua/?.lua;proto/?.lua;" .. package.path
local test_capnp = require "handwritten_capnp"
local ffi = require "ffi"
local test_capnp = require "handwritten_capnp"
local capnp = require "capnp"
local cjson = require "cjson"
local times = arg[1] or 1000000
local data = {
i0 = 32,
i1 = 16,
i2 = 127,
b0 = true,
b1 = true,
i3 = 65536,
e0 = "enum3",
s0 = {
f0 = 3.14,
f1 = 3.14159265358979,
},
l0 = { 28, 29 },
t0 = "hello",
e1 = "enum7",
}
local size = test_capnp.T1.calc_size(data)
local buf = ffi.new("char[?]", size)
local bin = test_capnp.T1.serialize(data)
local json_data = cjson.encode(data)
local tab = {}
function run4()
return test_capnp.T1.serialize(data, buf, size)
end
function run3()
return test_capnp.T1.serialize(data)
end
function run2()
return cjson.encode(data)
end
function run4()
return cjson.decode(json_data)
end
function run1()
return test_capnp.T1.parse(bin, tab)
end
print("Benchmarking ", times .. " times.")
local res
function bench(name, func)
local t1 = os.clock()
for i=1, times do
res = func()
end
print(name, " Elapsed: ", (os.clock() - t1) .. "s")
end
bench("cjson encode", run2)
bench("capnp encode", run3)
bench("cjson decode", run4)
--bench("capnp-noalloc", run4)
bench("capnp decode", run1)
--print(cjson.encode(res))