luau/bench/bench_support.lua
Arseny Kapoulkine 48aa7a5162
bench: Implement first class support for callgrind (#570)
Since callgrind allows to control stats collection from the guest, this
allows us to reset the collection right before the benchmark starts.

This change exposes this to the benchmark runner and integrates
callgrind data parsing into bench.py, so that we can run bench.py with
--callgrind argument and, as long as the runner was built with callgrind
support, we get instruction counts from the run.

We convert instruction counts to seconds using 10G instructions/second
rate; there's no correct way to do this without simulating the full CPU
pipeline but it results in time units on a similar scale to real runs.
2022-07-04 11:13:07 -07:00

61 lines
1.5 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)
-- Under Callgrind, run the test only once and measure just the execution cost
if callgrind and callgrind("running") then
if collectgarbage then collectgarbage() end
callgrind("zero")
f() -- unfortunately we can't easily separate setup cost from runtime cost in f unless it calls callgrind()
callgrind("dump", description)
return
end
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