Repository navigation
Expand file tree
/
Copy pathPerformance.lua
More file actions
49 lines (45 loc) · 1.38 KB
/
Copy pathPerformance.lua
File metadata and controls
49 lines (45 loc) · 1.38 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
-- Opt-in session profiling. No clock reads or metric allocations while disabled.
local _, ns = ...
local Performance = { enabled = false }
ns.Performance = Performance
local metrics = {}
function Performance:Record(name, duration)
if not self.enabled or type(duration) ~= "number" or duration < 0 then return end
local metric = metrics[name]
if not metric then
metric = { calls = 0, totalMs = 0, peakMs = 0, lastMs = 0 }
metrics[name] = metric
end
metric.calls = metric.calls + 1
metric.totalMs = metric.totalMs + duration
metric.peakMs = math.max(metric.peakMs, duration)
metric.lastMs = duration
end
function Performance:SetEnabled(enabled)
self.enabled = enabled == true
-- Existing event instrumentation checks this before reading the clock.
if self.enabled then
ns.RecordPerformance = function(name, startedAt)
if type(startedAt) == "number" then
Performance:Record(name, debugprofilestop() - startedAt)
end
end
else
ns.RecordPerformance = nil
end
end
function Performance:Reset()
metrics = {}
end
function Performance:GetMetrics()
local result = {}
for name, metric in pairs(metrics) do
result[#result + 1] = {
name = name, calls = metric.calls, totalMs = metric.totalMs,
peakMs = metric.peakMs, lastMs = metric.lastMs,
averageMs = metric.totalMs / metric.calls,
}
end
table.sort(result, function(a, b) return a.name < b.name end)
return result
end