simple benchmarks template

This commit is contained in:
BlackMATov
2025-01-03 00:45:25 +07:00
parent 93696f242b
commit efc644f180
2 changed files with 64 additions and 0 deletions

View File

@@ -1 +1,64 @@
package.loaded['evolved'] = nil
local evo = require 'evolved'
local __table_pack = (function()
return table.pack or function(...)
return { n = select('#', ...), ... }
end
end)()
local __table_unpack = (function()
return table.unpack or unpack
end)()
---@param name string
---@param loop fun(...): ...
---@param init? fun(): ...
local function __bench_describe(name, loop, init)
collectgarbage('collect')
collectgarbage('stop')
print(string.format('| %s ... |', name))
local iters = 0
local state = init and __table_pack(init()) or {}
local start_s = os.clock()
local start_kb = collectgarbage('count')
local success, result = pcall(function()
repeat
iters = iters + 1
loop(__table_unpack(state))
until os.clock() - start_s > 0.2
end)
local finish_s = os.clock()
local finish_kb = collectgarbage('count')
print(string.format(' %s | us: %.2f | op/s: %.2f | kb/i: %.2f',
success and 'PASS' or 'FAIL',
(finish_s - start_s) * 1e6 / iters,
iters / (finish_s - start_s),
(finish_kb - start_kb) / iters))
if not success then print(' ' .. result) end
collectgarbage('restart')
collectgarbage('collect')
end
__bench_describe('create and destroy 10k evolved ids', function()
local id = evo.id
local destroy = evo.destroy
local ids = {}
for i = 1, 10000 do
ids[i] = id()
end
for i = 1, 10000 do
destroy(ids[i])
end
end)

View File

@@ -1,3 +1,4 @@
package.loaded['evolved'] = nil
local evo = require 'evolved'
do