ruen-ui/RuenUI/Benchmark.lua

94 lines
4.5 KiB
Lua

local _, P = ...
-- Read the client's already-running profiler only during /ruen bench. No
-- scriptProfile toggle, extra ticker, frame hooks, graphics edits or idle work.
local fields = {"gxApi", "gxAdapter", "RenderScale", "maxFPS", "maxFPSBk", "vsync",
"useTargetFPS", "graphicsShadowQuality", "graphicsSSAO", "graphicsViewDistance",
"graphicsEnvironmentDetail", "graphicsGroundClutter", "graphicsComputeEffects"}
local counters = {"CountTimeOver1Ms", "CountTimeOver5Ms", "CountTimeOver10Ms", "CountTimeOver50Ms"}
local function number(fn, ...)
if type(fn) ~= "function" then return end
local ok, value = pcall(fn, ...)
if ok and not (issecretvalue and issecretvalue(value)) and type(value) == "number"
and value == value and value >= 0 and value < math.huge then return value end
end
local function settings()
local result = {}
if C_CVar and C_CVar.GetCVar then
for _, key in ipairs(fields) do result[key] = C_CVar.GetCVar(key) end
end
if GetPhysicalScreenSize then result.width, result.height = GetPhysicalScreenSize() end
local optimizer = RuenUIDB.optimizer
if optimizer then
result.calibrationEnabled = optimizer.enabled
for _, mode in ipairs({"world", "raid"}) do
local profile = optimizer.profiles and optimizer.profiles[mode]
if profile then result[mode.."Tier"] = profile.tier; result[mode.."Calibrated"] = profile.calibrated end
end
end
return result
end
local function metric(name)
local api, enum = C_AddOnProfiler, Enum and Enum.AddOnProfilerMetric
if not api or not enum or enum[name] == nil then return end
if api.IsEnabled then
local ok, enabled = pcall(api.IsEnabled)
if not ok or not enabled then return end
end
return number(api.GetAddOnMetric, "RuenUI", enum[name])
end
function P.StartBenchmarkDiagnostics()
local data = {release=P.release, settingsStart=settings(), samples={}, countersStart={}}
for _, name in ipairs(counters) do data.countersStart[name] = metric(name) end
return data
end
function P.SampleBenchmarkDiagnostics(data, fps)
if #data.samples >= 180 then return end
local row = {fps=fps, ruenRecentMS=metric("RecentAverageTime")}
if row.ruenRecentMS ~= nil and C_AddOnProfiler.GetOverallMetric then
row.allAddonsRecentMS = number(C_AddOnProfiler.GetOverallMetric, Enum.AddOnProfilerMetric.RecentAverageTime)
end
-- Keep the relevant neighbors on slow readings, not a scan of every addon.
if fps < 30 and row.ruenRecentMS ~= nil and C_AddOnProfiler.GetTopKAddOnsForMetric then
local ok, top = pcall(C_AddOnProfiler.GetTopKAddOnsForMetric, Enum.AddOnProfilerMetric.RecentAverageTime, 3)
if ok and type(top) == "table" then row.topAddons = top end
end
data.samples[#data.samples+1] = row
end
function P.FinishBenchmarkDiagnostics(data)
data.settingsEnd = settings()
data.settingsChanged = false
for key, value in pairs(data.settingsStart) do
if data.settingsEnd[key] ~= value then data.settingsChanged = true end
end
for key, value in pairs(data.settingsEnd) do
if data.settingsStart[key] ~= value then data.settingsChanged = true end
end
local total, count, maximum = 0, 0, 0
for _, row in ipairs(data.samples) do
if row.ruenRecentMS ~= nil then
total=total+row.ruenRecentMS; count=count+1; maximum=math.max(maximum,row.ruenRecentMS)
end
end
if count > 0 then data.sampledMeanMS=total/count; data.maxSampledRecentMS=maximum end
data.ticksOver = {}
for _, name in ipairs(counters) do
local before, after = data.countersStart[name], metric(name)
if before ~= nil and after ~= nil and after >= before then data.ticksOver[name] = after-before end
end
data.note = "Native recent metrics average the last 60 ticks; sampled once/second. Threshold deltas cover this benchmark, not the whole login."
local message
if data.sampledMeanMS then
local slow = data.ticksOver.CountTimeOver5Ms
message = string.format("Ruen CPU: sampled mean %.3f ms/tick; largest sampled recent average %.3f ms; ticks over 5 ms: %s.",
data.sampledMeanMS, data.maxSampledRecentMS, slow ~= nil and tostring(slow) or "unavailable")
else message = "Ruen CPU timing unavailable on this client; FPS alone cannot identify addon cost." end
if data.settingsChanged then message = message.." Graphics/calibration settings changed during this sample; comparison is inconclusive." end
return message
end