参考:https://zhuanlan.zhihu.com/p/24873676
--- lua代码性能分析工具
-----------------------------------------------------------------------------------------
local tinsert = table.insert
local tsort = table.sort
local clock = os.clock
local sformat = string.format
local sethook = debug.sethook
local profiler = {
_reports = {},
_title_reports = {},
_start_time = nil,
_stop_time = nil,
}
-- start profiling
function profiler:start(mode)
if mode and mode=='trace' then
sethook(self.trace_handler, 'cr', 0)
else
self._start_time = clock()
-- 开始hook,注册handler,记录call和return事件
sethook(self.profile_handler, 'cr', 0)
end
end
-- stop profiling
function profiler:stop(mode)
if mode and mode=='trace' then
sethook()