-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathbench_lib.lua
More file actions
63 lines (59 loc) · 1.71 KB
/
Copy pathbench_lib.lua
File metadata and controls
63 lines (59 loc) · 1.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
-- Shared timing helpers for the wall-clock benchmark runners.
--
-- os.clock() measures CPU time, not wall time. For these CPU-bound loops
-- that is the right metric (time the process spends scheduled out does not
-- pollute samples), but it would silently under-report anything I/O-bound.
-- luacheck: read globals jit
local M = {}
-- Runs fn(n) once untimed (lets LuaJIT compile traces: a loop becomes a
-- tracing candidate after 56 iterations, a side exit after 10), then times
-- `samples` runs with the collector stopped, starting each sample from a
-- freshly collected heap. Reports the median so a stray outlier sample
-- cannot move the headline number.
function M.measure(fn, n, samples)
samples = samples or 5
assert(samples >= 1, "samples must be >= 1")
local checksum = fn(n)
local times = {}
for s = 1, samples do
collectgarbage("collect")
collectgarbage("stop")
local t0 = os.clock()
fn(n)
local t1 = os.clock()
collectgarbage("restart")
times[s] = t1 - t0
end
table.sort(times)
local mid = math.floor((#times + 1) / 2)
local median = times[mid]
if #times % 2 == 0 then
median = (times[mid] + times[mid + 1]) / 2
end
return {
median = median,
min = times[1],
max = times[#times],
checksum = checksum,
}
end
function M.runtime_tag()
if jit then
return jit.status() and "luajit" or "luajit-joff"
end
return (_VERSION:gsub("%s", ""):lower())
end
function M.report(bench_name, variant_name, n, r)
io.write(string.format(
"%-13s %-8s %-12s n=%-9.0f median=%8.4fs min=%8.4fs max=%8.4fs result=%s\n",
bench_name,
variant_name,
M.runtime_tag(),
n,
r.median,
r.min,
r.max,
tostring(r.checksum)
))
end
return M