fix bench basics

This commit is contained in:
BlackMATov
2025-01-06 05:32:46 +07:00
parent 1ebeb5d84b
commit 50e4372311

View File

@@ -14,21 +14,31 @@ end)()
---@param loop fun(...): ...
---@param init? fun(): ...
function basics.describe_bench(name, loop, init)
collectgarbage('collect')
collectgarbage('stop')
print(string.format('| %s ... |', name))
local iters = 0
local state = init and __table_pack(init()) or {}
pcall(function()
do
local warmup_s = os.clock()
local success, result = pcall(function()
repeat
loop(__table_unpack(state))
until os.clock() - warmup_s > 0.1
end)
if not success then
print('|-- FAIL: ' .. result)
return
end
end
collectgarbage('collect')
collectgarbage('stop')
do
local iters = 0
local start_s = os.clock()
local start_kb = collectgarbage('count')
@@ -42,14 +52,16 @@ function basics.describe_bench(name, loop, init)
local finish_s = os.clock()
local finish_kb = collectgarbage('count')
print(string.format(' %s | us: %.2f | op/s: %.2f | kb/i: %.2f | iters: %d',
success and 'PASS' or 'FAIL',
if success then
print(string.format('|-- PASS | us: %.2f | op/s: %.2f | kb/i: %.2f | iters: %d',
(finish_s - start_s) * 1e6 / iters,
iters / (finish_s - start_s),
(finish_kb - start_kb) / iters,
iters))
if not success then print(' ' .. result) end
else
print('|-- FAIL: ' .. result)
end
end
collectgarbage('restart')
collectgarbage('collect')