Trace function call time
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local TOO_LONG_CALL_THRESHOLD = 0.1 | |
local log = require('log') | |
local clock = require 'clock' | |
local json = require 'json'.new() json.cfg{ encode_use_tostring = true } | |
local function ftail(fname, args, start, ...) | |
local run = clock.realtime() - start | |
if run > TOO_LONG_CALL_THRESHOLD then | |
log.info("Function call %s(%s) was too long: %0.2fs", fname, json.encode(args), run) | |
end | |
return ... | |
end | |
local function ftrace(fname, func) | |
return function(...) | |
local start = clock.realtime() | |
return ftail(fname, {...}, start, func(...)) | |
end | |
end | |
local function apply_ftrace(name, namespace) | |
for fname, v in pairs(namespace) do | |
if type(v) == 'table' then | |
apply_ftrace(name .. '.' .. fname, v) | |
elseif type(v) == 'function' and v ~= ftrace then | |
namespace[fname] = ftrace(name .. '.' .. fname, v) | |
end | |
end | |
end | |
--[[ | |
Use as | |
apply_trace('', _G) | |
or to specific namespace | |
apply_trace('app', app) | |
]] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment