-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathDebug.lua
More file actions
92 lines (74 loc) · 2.33 KB
/
Debug.lua
File metadata and controls
92 lines (74 loc) · 2.33 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
local SACIProfiler = {
data = {},
wrapped = setmetatable({}, { __mode = "k" }), -- weak keys so GC safe
}
local function WrapFunction(self, func, name)
self.data[name] = self.data[name] or {
calls = 0,
total = 0,
max = 0,
min = 999,
}
local entry = self.data[name]
return function(...)
local t0 = debugprofilestop()
local a,b,c,d,e,f = func(...)
local dt = debugprofilestop() - t0
entry.calls = entry.calls + 1
entry.total = entry.total + dt
if dt > entry.max then entry.max = dt end
if dt < entry.min then entry.min = dt end
return a,b,c,d,e,f
end
end
function SACIProfiler:HookMixin(mixin, mixinName)
if self.wrapped[mixin] then return end
self.wrapped[mixin] = true
mixinName = mixinName or tostring(mixin)
for key, value in pairs(mixin) do
if type(value) == "function" then
local fullName = mixinName .. "." .. key
mixin[key] = WrapFunction(self, value, fullName)
end
end
end
function SACIProfiler:Report(limit)
limit = limit or 40
local list = {}
local totalTime = 0
local totalCalls = 0
for name, e in pairs(self.data) do
local calls = e.calls or 0
local total = e.total or 0
totalTime = totalTime + total
totalCalls = totalCalls + calls
table.insert(list, {
name = name,
calls = calls,
total = total,
avg = calls > 0 and (total / calls) or 0,
max = e.max,
min = e.min
})
end
table.sort(list, function(a, b)
return a.max > b.max
end)
print("------ PROFILER REPORT ------")
for i = 1, math.min(limit, #list) do
local e = list[i]
print(string.format(
"%2d. %-30s max: %.3fms min: %.3fms avg: %.5fms total: %.3fms calls: %d",
i, e.name, e.max, e.min, e.avg, e.total, e.calls
))
end
local overallAvg = totalCalls > 0 and (totalTime / totalCalls) or 0
print("-----------------------------------")
print(string.format(
"TOTALS: total: %.3fms avg: %.5fms calls: %d",
totalTime, overallAvg, totalCalls
))
end
_G.SACIProfiler = SACIProfiler
SACIProfiler:HookMixin(AssistedCombatIconFrame, "SACI")
print("SACI Profiler enabled!")