Skip to content

Instantly share code, notes, and snippets.

@Mons
Created April 15, 2021 15:35
Show Gist options
  • Save Mons/95114061d4d48e49859ae837d5de7127 to your computer and use it in GitHub Desktop.
Save Mons/95114061d4d48e49859ae837d5de7127 to your computer and use it in GitHub Desktop.
Trace function call time
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