Skip to content

Commit

Permalink
Merge pull request #4268 from myk002/myk_overlay_timers
Browse files Browse the repository at this point in the history
[overlay] add perf timers
  • Loading branch information
myk002 authored Feb 14, 2024
2 parents 2d66f17 + 591ef16 commit 4b29149
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions plugins/lua/overlay.lua
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,39 @@ local GLOBAL_KEY = 'OVERLAY'

local DEFAULT_X_POS, DEFAULT_Y_POS = -2, -2

local timers = {}
local timers_start_ms = dfhack.getTickCount()
function reset_timers()
timers = {}
timers_start_ms = dfhack.getTickCount()
end
function print_timers()
local elapsed = dfhack.getTickCount() - timers_start_ms
local sum = 0
for _,timer in pairs(timers) do
sum = sum + timer
end
if elapsed <= 0 then elapsed = 1 end
if sum <= 0 then sum = 1 end
local sorted = {}
for name,timer in pairs(timers) do
table.insert(sorted, {name=name, ms=timer})
end
table.sort(sorted, function(a, b) return a.ms > b.ms end)
for _, elem in ipairs(sorted) do
print(('%45s %8d ms %6.2f%% overlay %6.2f%% overall'):format(
elem.name, elem.ms, (elem.ms * 100) / sum, (elem.ms * 100) / elapsed
))
end
print()
print(('elapsed time: %10d ms (%dm %ds)'):format(
elapsed, elapsed // 60000, (elapsed % 60000) // 1000
))
print(('widget time: %10d ms (%.2f%% of elapsed time)'):format(
sum, (sum * 100) / elapsed
))
end

-- ---------------- --
-- state and config --
-- ---------------- --
Expand Down Expand Up @@ -413,7 +446,9 @@ end
local function detect_frame_change(widget, fn)
local frame = widget.frame
local w, h = frame.w, frame.h
local now_ms = dfhack.getTickCount()
local ret = fn()
timers[widget.name] = (timers[widget.name] or 0) + (dfhack.getTickCount() - now_ms)
if w ~= frame.w or h ~= frame.h then
widget:updateLayout()
end
Expand Down

0 comments on commit 4b29149

Please sign in to comment.