wip profiler

This commit is contained in:
Jill 2022-09-23 18:07:09 +03:00
parent fdc15e1fd8
commit 4f473a744a
3 changed files with 86 additions and 2 deletions

View File

@ -106,12 +106,33 @@
oat.dw = DISPLAY:GetDisplayWidth()
oat.dh = DISPLAY:GetDisplayHeight()
oat.useProfiler = false
oat.profilerInfo = {}
local uraniumFunc = {}
local debugCache = {}
function uraniumFunc:call(event, ...)
if self._callbacks[event] then
profilerInfo[event] = {}
for _, callback in ipairs(self._callbacks[event]) do
callback(unpack(arg))
local start = os.clock()
local res = callback(unpack(arg))
local dur = os.clock() - start
if oat.useProfiler then
if not debugCache[callback] then
debugCache[callback] = debug.getinfo(callback, 'Sl') -- cached cus debug.getinfo is EXPENSIVE
end
local finfo = debugCache[callback]
table.insert(profilerInfo[event], {
src = finfo.short_src .. ':' .. finfo.linedefined,
t = dur
})
end
if res ~= nil then return res end
end
end
end

56
stdlib/profiler.lua Normal file
View File

@ -0,0 +1,56 @@
PROFILER_ENABLED = true
profilerShowing = 'update'
local easable = require('stdlib.easable')
local text = BitmapText()
local quad = Quad()
oat.useProfiler = true
if PROFILER_ENABLED then
local max = easable(0)
local function draw()
if not profilerInfo[profilerShowing] then return end
quad:diffuse(0.2, 1, 0.2, 0.9)
quad:align(0, 0)
text:align(0, 0)
text:shadowlength(0)
table.sort(profilerInfo[profilerShowing], function(a, b) return a.t > b.t end)
local maxt = 0
for i, e in ipairs(profilerInfo[profilerShowing]) do
maxt = math.max(maxt, e.t)
quad:zoomto(e.t / max.a * sw * 0.4, 24)
quad:xy(0, i * 24)
quad:Draw()
text:settext((math.floor(e.t * 100000) / 100) .. 'ms')
text:xy(0, i * 24)
text:zoom(0.3)
text:diffuse(0.2, 0.2, 0.2, 0.9)
text:Draw()
text:settext(e.src)
text:xy(0, i * 24 + 12)
text:zoom(0.2)
text:diffuse(0.1, 0.1, 0.1, 0.9)
text:Draw()
end
max:set(maxt)
text:diffuse(1, 1, 1, 1)
text:xy(0, 0)
text:zoom(0.5)
text:shadowlength(3)
text:settext('Profiler - ' .. profilerShowing)
text:Draw()
end
function uranium.update(dt)
max(dt * 12)
draw()
end
end

View File

@ -105,4 +105,11 @@ uranium.update = function() end
function uranium:call(event, ...) end
--- Equivalent to a modfile-sandboxed `_G`, similar to Mirin's `xero`. You shouldn't need this; and if you do, *what are you doing?*
oat = _G
oat = _G
---@class ProfilerInfo
---@field public t number
---@field public src string
---@type table<string, ProfilerInfo>
profilerInfo = {}