Skip to content

Instantly share code, notes, and snippets.

@erikdubbelboer
Last active December 12, 2015 04:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save erikdubbelboer/5dbecf2b8315baf82ee8 to your computer and use it in GitHub Desktop.
Save erikdubbelboer/5dbecf2b8315baf82ee8 to your computer and use it in GitHub Desktop.
-- Write an unsigned 32 bit integer v to position i in b in little endian encoding.
local function PutUint32(b, i, v)
b[i] = v % 256
b[i+1] = math.floor(v/256) % 256
b[i+2] = math.floor(v/65536) % 256
b[i+3] = math.floor(v/16777216) % 256
end
-- Read an unsigned 32 bit integer from position i in b in little endian encoding.
local function Uint32(b, i)
return b[i] + (b[i+1] * 256) + (b[i+2] * 65536) + (b[i+3] * 16777216)
end
local function bench_write_lua_(r, n, a, b)
local s = {} -- There is no proper way to preallocate a Lua table.
for i=1,n,1 do
PutUint32(s, #s+1, a)
PutUint32(s, #s+1, b)
end
local by = bytes(#s)
bytes.set_string(by, 1, string.char(unpack(s)))
r["bench"] = by
aerospike:update(r)
end
local function bench_write_as_(r, n, a, b)
local by = bytes(8*n) -- preallocate, we know how the max is we need.
local used = 0
for i=1,n,1 do
bytes.set_int32_le(by, used+1, a)
bytes.set_int32_le(by, used+5, b)
used = used + 8
end
bytes.set_size(by, used)
r["bench"] = by
aerospike:update(r)
end
local function bench_read_lua_(r)
local by = r["bench"]
local s = {bytes.get_string(by, 1, bytes.size(by)):byte(1, -1)}
for i=1,#s,8 do
Uint32(s, i)
Uint32(s, i+4)
end
end
local function bench_read_as_(r)
local by = r["bench"]
local size = bytes.size(by)
for i=1,size,8 do
bytes.get_int32_le(by, i)
bytes.get_int32_le(by, i+4)
end
end
function bench_write_lua(r, a, b)
for i=1,10000,1 do
bench_write_lua_(r, 10, a, b)
end
end
function bench_write_as(r, a, b)
for i=1,10000,1 do
bench_write_as_(r, 10, a, b)
end
end
function bench_read_lua(r)
for i=1,20000,1 do
bench_read_lua_(r)
end
end
function bench_read_as(r)
for i=1,20000,1 do
bench_read_as_(r)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment