profile库输出排版微调

This commit is contained in:
MrZ626
2020-12-26 15:56:59 +08:00
parent 33e71dbfa9
commit 315de60e79

View File

@@ -20,7 +20,7 @@ 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 return end if _internal[f] then return end
-- get the function name if available -- get the function name if available
if info.name then _labeled[f] = info.name end if info.name then _labeled[f] = info.name end
-- find the line definition -- find the line definition
@@ -81,9 +81,11 @@ end
--- Resets all collected data. --- Resets all collected data.
function profile.reset() function profile.reset()
for f in next, _ncalls do _ncalls[f] = 0 end for f in next, _ncalls do
for f in next, _telapsed do _telapsed[f] = 0 end _ncalls[f] = 0
for f in next, _tcalled do _tcalled[f] = nil end _telapsed[f] = 0
_tcalled[f] = nil
end
collectgarbage('collect') collectgarbage('collect')
end end
@@ -112,21 +114,19 @@ 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 '?', math.floor((_telapsed[f] + dt) * 1e6) / 1e6, _ncalls[f], _defined[f]}
end end
return t return t
end end
local cols = {3, 29, 11, 24, 32} local cols = {3, 20, 8, 6, 32}
function profile.report(n) function profile.report(n)
local out = {} local out = {}
local report = profile.query(n) local report = profile.query(n)
for i, row in ipairs(report) do for i, row in ipairs(report) do
for j = 1, 5 do for j = 1, 5 do
local s = row[j] local s = tostring(row[j])
local l2 = cols[j] local l1, l2 = #s, cols[j]
s = tostring(s)
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
@@ -137,8 +137,8 @@ function profile.report(n)
out[i] = table.concat(row, ' | ') out[i] = table.concat(row, ' | ')
end end
local row = " +-----+-------------------------------+-------------+--------------------------+----------------------------------+ \n" local row = " +-----+----------------------+----------+--------+----------------------------------+ \n"
local col = " | # | Function | Calls | Time | Code | \n" local col = " | # | Function | Time | Calls | 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'
@@ -162,7 +162,7 @@ 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 _internal[v] = true end _internal[v] = type(v) == "function"
end end
return profile return profile