Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Roblox
GitHub Repository: Roblox/luau
Path: blob/master/bench/bench_support.lua
2723 views
1
-- This file is part of the Luau programming language and is licensed under MIT License; see LICENSE.txt for details
2
local bench = {}
3
4
bench.runs = 20
5
bench.extraRuns = 4
6
7
function bench.runCode(f, description)
8
-- Under Callgrind, run the test only once and measure just the execution cost
9
if callgrind and callgrind("running") then
10
if collectgarbage then collectgarbage() end
11
12
callgrind("zero")
13
f() -- unfortunately we can't easily separate setup cost from runtime cost in f unless it calls callgrind()
14
callgrind("dump", description)
15
return
16
end
17
18
local timeTable = {}
19
20
for i = 1,bench.runs + bench.extraRuns do
21
-- try to run GC if it's available
22
if collectgarbage then
23
pcall(function()
24
collectgarbage()
25
end)
26
end
27
28
local ts0 = os.clock()
29
30
local result = f()
31
32
local ts1 = os.clock()
33
34
-- If test case doesn't return a duration (if only a part of code is measured) we will measure full execution time here
35
if not result then
36
result = ts1 - ts0
37
end
38
39
table.insert(timeTable, result)
40
end
41
42
table.sort(timeTable)
43
44
for i = 1,bench.extraRuns do
45
table.remove(timeTable, #timeTable)
46
end
47
48
-- Output test name followed by each result
49
local report = "|><|"..description
50
51
for _,v in ipairs(timeTable) do
52
report = report .. "|><|" .. (v * 1000)
53
end
54
55
report = report .. "||_||"
56
57
print(report)
58
end
59
60
-- This function acts a bit like a Unix "fork" operation
61
-- When it is first called it clones `scriptInstance` and starts executing
62
-- the cloned script parented to an Actor. When the cloned script calls "runScriptCodeUnderActor"
63
-- it will run 'f' and print out the provided 'description'.
64
--
65
-- The function returns 'true' if it was invoked from a script running under an Actor
66
-- and 'false' otherwise.
67
--
68
-- Example usage:
69
-- local function prequire(name) local success, result = pcall(require, name); return success and result end
70
-- local bench = script and require(script.Parent.bench_support) or prequire("bench_support") or require("../bench_support")
71
-- function testFunc()
72
-- ...
73
-- end
74
-- bench.runScriptCodeUnderActor(script, testFunc, "test function")
75
function bench.runScriptCodeUnderActor(scriptInstance, f, description)
76
if scriptInstance:GetActor() then
77
-- If this function was called from an Actor script, just run the function provided using runCode
78
bench.runCode(f, description)
79
return true
80
else
81
-- If this function was not called from an Actor script, clone the script and place it under
82
-- Actor instance.
83
84
-- Create an Actor to run the script under
85
local actor = Instance.new("Actor")
86
-- Clone this script (i.e. the bench_support module) and place it under the Actor where
87
-- the script script would expect it to be when using 'require'.
88
local benchModule = script:Clone()
89
benchModule.Parent = actor
90
-- Clone the scriptInstance
91
local actorScript = scriptInstance:Clone()
92
-- Enable the script since `scriptInstance` may be started by roblox-cli without ever being enabled.
93
actorScript.Disabled = false
94
actorScript.Parent = actor
95
-- Add the actor to the workspace which will start executing the cloned script.
96
-- Note: the script needs to be placed under a instance that implements 'IScriptFilter'
97
-- (which workspace does) or it will never start executing.
98
actor.Parent = workspace
99
return false
100
end
101
end
102
103
return bench
104
105