luau/bench/bench_support.lua
Arseny Kapoulkine d01addc625 Sync to upstream/release/501 (#20)
Co-authored-by: Rodactor <rodactor@roblox.com>
2021-11-01 14:52:34 -07:00

50 lines
1.1 KiB
Lua

-- This file is part of the Luau programming language and is licensed under MIT License; see LICENSE.txt for details
local bench = {}
bench.runs = 20
bench.extraRuns = 4
function bench.runCode(f, description)
local timeTable = {}
for i = 1,bench.runs + bench.extraRuns do
-- try to run GC if it's available
if collectgarbage then
pcall(function()
collectgarbage()
end)
end
local ts0 = os.clock()
local result = f()
local ts1 = os.clock()
-- If test case doesn't return a duration (if only a part of code is measured) we will measure full execution time here
if not result then
result = ts1 - ts0
end
table.insert(timeTable, result)
end
table.sort(timeTable)
for i = 1,bench.extraRuns do
table.remove(timeTable, #timeTable)
end
-- Output test name followed by each result
local report = "|><|"..description
for _,v in ipairs(timeTable) do
report = report .. "|><|" .. (v * 1000)
end
report = report .. "||_||"
print(report)
end
return bench