整理代码
This commit is contained in:
@@ -15,21 +15,17 @@ local _ncalls = {}
|
|||||||
-- list of internal profiler functions
|
-- list of internal profiler functions
|
||||||
local _internal = {}
|
local _internal = {}
|
||||||
|
|
||||||
local getInfo=debug.getinfo
|
local getInfo = debug.getinfo
|
||||||
function profile.hooker(event, line, info)
|
function profile.hooker(event, line, info)
|
||||||
info = info or getInfo(2, 'fnS')
|
info = info or getInfo(2, 'fnS')
|
||||||
local f = info.func
|
local f = info.func
|
||||||
-- ignore the profiler itself
|
-- ignore the profiler itself
|
||||||
if _internal[f] or info.what ~= "Lua" then
|
if _internal[f] or info.what ~= "Lua" then return end
|
||||||
return
|
|
||||||
end
|
|
||||||
-- get the function name if available
|
-- get the function name if available
|
||||||
if info.name then
|
if info.name then _labeled[f] = info.name end
|
||||||
_labeled[f] = info.name
|
|
||||||
end
|
|
||||||
-- find the line definition
|
-- find the line definition
|
||||||
if not _defined[f] then
|
if not _defined[f] then
|
||||||
_defined[f] = info.short_src..":"..info.linedefined
|
_defined[f] = info.short_src .. ":" .. info.linedefined
|
||||||
_ncalls[f] = 0
|
_ncalls[f] = 0
|
||||||
_telapsed[f] = 0
|
_telapsed[f] = 0
|
||||||
end
|
end
|
||||||
@@ -49,13 +45,6 @@ function profile.hooker(event, line, info)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
--- Sets a clock function to be used by the profiler.
|
|
||||||
-- @param f Clock function that returns a number
|
|
||||||
function profile.setclock(f)
|
|
||||||
assert(type(f) == "function", "clock must be a function")
|
|
||||||
clock = f
|
|
||||||
end
|
|
||||||
|
|
||||||
--- Starts collecting data.
|
--- Starts collecting data.
|
||||||
function profile.start()
|
function profile.start()
|
||||||
if rawget(_G, 'jit') then
|
if rawget(_G, 'jit') then
|
||||||
@@ -68,15 +57,15 @@ end
|
|||||||
--- Stops collecting data.
|
--- Stops collecting data.
|
||||||
function profile.stop()
|
function profile.stop()
|
||||||
debug.sethook()
|
debug.sethook()
|
||||||
for f in next,_tcalled do
|
for f in next, _tcalled do
|
||||||
local dt = clock() - _tcalled[f]
|
local dt = clock() - _tcalled[f]
|
||||||
_telapsed[f] = _telapsed[f] + dt
|
_telapsed[f] = _telapsed[f] + dt
|
||||||
_tcalled[f] = nil
|
_tcalled[f] = nil
|
||||||
end
|
end
|
||||||
-- merge closures
|
-- merge closures
|
||||||
local lookup = {}
|
local lookup = {}
|
||||||
for f, d in next,_defined do
|
for f, d in next, _defined do
|
||||||
local id = (_labeled[f] or '?')..d
|
local id = (_labeled[f] or '?') .. d
|
||||||
local f2 = lookup[id]
|
local f2 = lookup[id]
|
||||||
if f2 then
|
if f2 then
|
||||||
_ncalls[f2] = _ncalls[f2] + (_ncalls[f] or 0)
|
_ncalls[f2] = _ncalls[f2] + (_ncalls[f] or 0)
|
||||||
@@ -92,31 +81,22 @@ end
|
|||||||
|
|
||||||
--- Resets all collected data.
|
--- Resets all collected data.
|
||||||
function profile.reset()
|
function profile.reset()
|
||||||
for f in next,_ncalls do
|
for f in next, _ncalls do _ncalls[f] = 0 end
|
||||||
_ncalls[f] = 0
|
for f in next, _telapsed do _telapsed[f] = 0 end
|
||||||
end
|
for f in next, _tcalled do _tcalled[f] = nil end
|
||||||
for f in next,_telapsed do
|
|
||||||
_telapsed[f] = 0
|
|
||||||
end
|
|
||||||
for f in next,_tcalled do
|
|
||||||
_tcalled[f] = nil
|
|
||||||
end
|
|
||||||
collectgarbage('collect')
|
collectgarbage('collect')
|
||||||
end
|
end
|
||||||
|
|
||||||
function profile.comp(a, b)
|
function profile.comp(a, b)
|
||||||
local dt = _telapsed[b] - _telapsed[a]
|
local dt = _telapsed[b] - _telapsed[a]
|
||||||
if dt == 0 then
|
return dt == 0 and _ncalls[b] < _ncalls[a] or dt < 0
|
||||||
return _ncalls[b] < _ncalls[a]
|
|
||||||
end
|
|
||||||
return dt < 0
|
|
||||||
end
|
end
|
||||||
|
|
||||||
--- Iterates all functions that have been called since the profile was started.
|
--- Iterates all functions that have been called since the profile was started.
|
||||||
-- @param n Number of results (optional)
|
-- @param n Number of results (optional)
|
||||||
function profile.query(limit)
|
function profile.query(limit)
|
||||||
local t = {}
|
local t = {}
|
||||||
for f, n in next,_ncalls do
|
for f, n in next, _ncalls do
|
||||||
if n > 0 then
|
if n > 0 then
|
||||||
t[#t + 1] = f
|
t[#t + 1] = f
|
||||||
end
|
end
|
||||||
@@ -132,12 +112,12 @@ function profile.query(limit)
|
|||||||
if _tcalled[f] then
|
if _tcalled[f] then
|
||||||
dt = clock() - _tcalled[f]
|
dt = clock() - _tcalled[f]
|
||||||
end
|
end
|
||||||
t[i] = { i, _labeled[f] or '?', _ncalls[f], _telapsed[f] + dt, _defined[f] }
|
t[i] = {i, _labeled[f] or '?', _ncalls[f], _telapsed[f] + dt, _defined[f]}
|
||||||
end
|
end
|
||||||
return t
|
return t
|
||||||
end
|
end
|
||||||
|
|
||||||
local cols = { 3, 29, 11, 24, 32 }
|
local cols = {3, 29, 11, 24, 32}
|
||||||
function profile.report(n)
|
function profile.report(n)
|
||||||
local out = {}
|
local out = {}
|
||||||
local report = profile.query(n)
|
local report = profile.query(n)
|
||||||
@@ -148,7 +128,7 @@ function profile.report(n)
|
|||||||
s = tostring(s)
|
s = tostring(s)
|
||||||
local l1 = s:len()
|
local l1 = s:len()
|
||||||
if l1 < l2 then
|
if l1 < l2 then
|
||||||
s = s..(' '):rep(l2-l1)
|
s = s .. (' '):rep(l2 - l1)
|
||||||
elseif l1 > l2 then
|
elseif l1 > l2 then
|
||||||
s = s:sub(l1 - l2 + 1, l1)
|
s = s:sub(l1 - l2 + 1, l1)
|
||||||
end
|
end
|
||||||
@@ -159,14 +139,14 @@ function profile.report(n)
|
|||||||
|
|
||||||
local row = " +-----+-------------------------------+-------------+--------------------------+----------------------------------+ \n"
|
local row = " +-----+-------------------------------+-------------+--------------------------+----------------------------------+ \n"
|
||||||
local col = " | # | Function | Calls | Time | Code | \n"
|
local col = " | # | Function | Calls | Time | Code | \n"
|
||||||
local sz = row..col..row
|
local sz = row .. col .. row
|
||||||
if #out > 0 then
|
if #out > 0 then
|
||||||
sz = sz..' | '..table.concat(out, ' | \n | ')..' | \n'
|
sz = sz .. ' | ' .. table.concat(out, ' | \n | ') .. ' | \n'
|
||||||
end
|
end
|
||||||
return '\n'..sz..row
|
return '\n' .. sz .. row
|
||||||
end
|
end
|
||||||
|
|
||||||
local switch=false
|
local switch = false
|
||||||
function profile.switch()
|
function profile.switch()
|
||||||
if switch then
|
if switch then
|
||||||
profile.stop()
|
profile.stop()
|
||||||
@@ -177,14 +157,12 @@ function profile.switch()
|
|||||||
PROFILE.start()
|
PROFILE.start()
|
||||||
LOG.print("profile start!")
|
LOG.print("profile start!")
|
||||||
end
|
end
|
||||||
switch=not switch
|
switch = not switch
|
||||||
end
|
end
|
||||||
|
|
||||||
-- store all internal profiler functions
|
-- store all internal profiler functions
|
||||||
for _, v in next,profile do
|
for _, v in next, profile do
|
||||||
if type(v) == "function" then
|
if type(v) == "function" then _internal[v] = true end
|
||||||
_internal[v] = true
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
return profile
|
return profile
|
||||||
|
|||||||
Reference in New Issue
Block a user