feat(perf): indbygget profiler — måler alle indgange (hooks, update, GUI), logger top-6 pr. minut ved > 2 ms

This commit is contained in:
2026-09-26 11:19:35 +02:00
parent 0fca6e8a9a
commit 8a18187103
4 changed files with 169 additions and 0 deletions
+42
View File
@@ -0,0 +1,42 @@
-- Kør: luajit tests/test_adProfiler.lua (fra repo-roden)
local failures = 0
local function check(name, actual, expected)
if actual == expected then print("OK " .. name) else
failures = failures + 1
print(string.format("FAIL %s: forventede %s, fik %s", name, tostring(expected), tostring(actual)))
end
end
local lines = {}
Logging = {info = function(fmt, ...) table.insert(lines, string.format(fmt, ...)) end}
local clock = 0
function netGetTime() return clock end
dofile("FS25_ADSmartPickup/adProfiler.lua")
local Module = {}
function Module.slow(a, b) clock = clock + 5; return a + b, "ok" end
function Module.fast() clock = clock + 0.1 end
ADProfiler.wrap(Module, "slow", "langsom")
ADProfiler.wrap(Module, "fast", "hurtig")
local sum, text = Module.slow(2, 3)
check("P1 returværdier bevares", sum, 5); check("P1 anden returværdi", text, "ok")
Module.fast()
check("P2 kald talt", ADProfiler.stats["langsom"].calls, 1)
check("P2 max", ADProfiler.stats["langsom"].max, 5)
ADProfiler.frame(0)
check("P3 værste frame", ADProfiler.worstFrameMs, 5.1)
ADProfiler.frame(61000)
check("P4 rapport skrevet", #lines, 1)
check("P4 langsom først", lines[1]:find("langsom max 5.0", 1, true) ~= nil, true)
check("P4 nulstillet", next(ADProfiler.stats), nil)
-- intet over 2 ms -> ingen rapport
Module.fast(); ADProfiler.frame(62000); ADProfiler.frame(130000)
check("P5 stille minut -> ingen linje", #lines, 1)
-- fejl i indpakket funktion: dybden nulstilles næste frame
function Module.boom() error("x") end
ADProfiler.wrap(Module, "boom", "fejl")
pcall(Module.boom)
ADProfiler.frame(131000)
check("P6 dybde nulstillet", ADProfiler.depth, 0)
print(failures == 0 and "\nALLE TESTS OK" or ("\n" .. failures .. " FEJL"))
os.exit(failures == 0 and 0 or 1)