forked from katur239/SimpleAssistedCombatIcon
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDebug.lua
More file actions
71 lines (58 loc) · 1.73 KB
/
Debug.lua
File metadata and controls
71 lines (58 loc) · 1.73 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
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,
}
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
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 = {}
for name, e in pairs(self.data) do
table.insert(list, {
name = name,
calls = e.calls,
total = e.total,
avg = e.total / e.calls,
max = e.max,
})
end
table.sort(list, function(a, b)
return a.total > b.total
end)
print("------ PROFILER REPORT ------")
for i = 1, math.min(limit, #list) do
local e = list[i]
print(string.format(
"%2d. %-40s total: %.3fms avg: %.5fms max: %.3fms calls: %d",
i, e.name, e.total, e.avg, e.max, e.calls
))
end
print("-----------------------------------")
end
_G.SACIProfiler = SACIProfiler