Skip to content

Instantly share code, notes, and snippets.

@locker
Created Jul 23, 2021
Embed
What would you like to do?
Tarantool net.box benchmark
clock = require('clock')
fiber = require('fiber')
net_box = require('net.box')
URI = '127.0.0.1:3301'
SPACE = 'T'
TIMEOUT = 5
ITERATIONS = 3000
CONCURRENCY = 2000
function prepare(conn)
conn:eval(string.gsub([[
box.schema.space.create('SPACE', {if_not_exists = true})
box.space.SPACE:create_index('primary', {if_not_exists = true})
box.space.SPACE:format{
{name= 'id', type = 'integer'},
{name = 'a', type = 'string'},
{name = 'b', type = 'number'},
}
box.space.SPACE:truncate()
for k = 1, ITERATIONS do
box.space.SPACE:replace({k, 'foo', 1.23})
end
box.snapshot()
function bench_func(...) return {...} end
]], '%w+', {
['SPACE'] = SPACE,
['ITERATIONS'] = ITERATIONS,
}), {})
end
function test_fiber(method)
local conn = net_box.connect(URI)
prepare(conn)
local space = conn.space[SPACE]
local opts = {timeout = TIMEOUT}
local wait_channel = fiber.channel(CONCURRENCY)
local wall_time_start = clock.monotonic()
local proc_time_start = clock.proc()
for i = 1, CONCURRENCY do
fiber.new(function()
for k = 1, ITERATIONS do
if method == 'REPLACE' then
space:replace({k, 'bar', i + k}, opts)
elseif method == 'UPDATE' then
space:update({k}, {{'=', 2, 'bar'}, {'=', 3, i + k}}, opts)
elseif method == 'SELECT' then
space:select({k}, opts)
elseif method == 'CALL' then
conn:call('bench_func', {1, 2, 3, 'foo', 'bar'}, opts)
else
assert(false)
end
end
wait_channel:put(true)
end)
end
for i = 1, CONCURRENCY do
wait_channel:get()
end
local wall_elapsed = clock.monotonic() - wall_time_start
local proc_elapsed = clock.proc() - proc_time_start
conn:close()
print(string.format('%10s: WALL %.3f PROC %.3f KRPS', method,
CONCURRENCY * ITERATIONS / wall_elapsed / 1000,
CONCURRENCY * ITERATIONS / proc_elapsed / 1000))
end
function test_future(method)
local conn = net_box.connect(URI)
prepare(conn)
local space = conn.space[SPACE]
local opts = {is_async = true, timeout = TIMEOUT}
local wall_time_start = clock.monotonic()
local proc_time_start = clock.proc()
for k = 1, ITERATIONS do
local futures = {}
for i = 1, CONCURRENCY do
local f
if method == 'REPLACE' then
f = space:replace({k, 'bar', i + k}, opts)
elseif method == 'UPDATE' then
f = space:update({k}, {{'=', 2, 'bar'}, {'=', 3, i + k}}, opts)
elseif method == 'SELECT' then
f = space:select({k}, opts)
elseif method == 'CALL' then
f = conn:call('bench_func', {1, 2, 3, 'foo', 'bar'}, opts)
else
assert(false)
end
table.insert(futures, f)
end
for i = 1, CONCURRENCY do
futures[i]:wait_result()
end
end
local wall_elapsed = clock.monotonic() - wall_time_start
local proc_elapsed = clock.proc() - proc_time_start
conn:close()
print(string.format('%9s: WALL %.3f PROC %.3f KRPS', method,
CONCURRENCY * ITERATIONS / wall_elapsed / 1000,
CONCURRENCY * ITERATIONS / proc_elapsed / 1000))
end
print('==== FIBER ====')
test_fiber('REPLACE')
test_fiber('UPDATE')
test_fiber('SELECT')
test_fiber('CALL')
print('==== FUTURE ====')
test_future('REPLACE')
test_future('UPDATE')
test_future('SELECT')
test_future('CALL')
os.exit(0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment