From 5d21f1030c473dfe0ef6a21b1af0cf55aa66e440 Mon Sep 17 00:00:00 2001 From: omicron Date: Sun, 19 Mar 2023 02:11:57 +0100 Subject: [PATCH] Add profiler code --- src/OmicronFrames.toc | 1 + src/main.lua | 3 +- src/profiler.lua | 110 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 113 insertions(+), 1 deletion(-) create mode 100644 src/profiler.lua diff --git a/src/OmicronFrames.toc b/src/OmicronFrames.toc index 5349866..75455b5 100644 --- a/src/OmicronFrames.toc +++ b/src/OmicronFrames.toc @@ -16,5 +16,6 @@ types/unitgroup.lua types/unitframe.lua frames.lua +profiler.lua init.lua diff --git a/src/main.lua b/src/main.lua index 2ee039a..f9e36d6 100644 --- a/src/main.lua +++ b/src/main.lua @@ -18,7 +18,8 @@ local AddonName, omif = ... omif.events = {} -- event (str) to list of handlers omif.db = nil -omif.modules = {} +OmicronFrames = {} +omif.modules = {public=OmicronFrames} -- Simple function that creates or returns a local module table function omif.GetModule(name) if omif.modules[name] == nil then diff --git a/src/profiler.lua b/src/profiler.lua new file mode 100644 index 0000000..8f86214 --- /dev/null +++ b/src/profiler.lua @@ -0,0 +1,110 @@ +-- Copyright 2023 +-- +-- This file is part of Omicron Frames +-- +-- Omicron Frames is free software: you can redistribute it and/or modify it +-- under the terms of the GNU General Public License as published by the Free +-- Software Foundation, either version 3 of the License, or (at your option) +-- any later version. +-- +-- Omicron Frames is distributed in the hope that it will be useful, but +-- WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY +-- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for +-- more details. +-- +-- You should have received a copy of the GNU General Public License along with +-- Omicron Frames. If not, see . +local addonName, omi = ... + +local public = omi.GetModule("public") +local types = omi.GetModule("types") +local data = {} + +function public.StartProfiler() + local profiler = C_CVar.GetCVar("scriptProfile") + if profiler ~= "1" then + print("scriptProfiler is off", profiler) + print("set it to on with `/console scriptProfile 1`, then reload the UI.") + return + end + print("OmicronFrames: start profiling") + ResetCPUUsage() +end + +function public.StopProfiler() + print("OmicronFrames: stop profiling") + UpdateAddOnCPUUsage() + local total = GetAddOnCPUUsage(addonName) + data = {{ + name = "Total OmicronFrames time", + time = total, + pct = 1.0 + }} + + for typeName, T in pairs(types) do + if type(T) == "table" then + for fnName, fn in pairs(T) do + if type(fn) == "function" then + local time = GetFunctionCPUUsage(fn, true) + table.insert(data, { + name = string.format("%s:%s", typeName, fnName), + time = time, + pct = time/total + }) + end + end + end + end + table.sort(data, function(a, b) return a.time > b.time end) +end + +local profileFrame +local profileText + +function public.PrintProfilerData() + profileFrame:Show() + profileText:SetText("") + for _, item in ipairs(data) do + local line = string.format("% 5.1f%% % 5fms %s\n", item.pct*100, item.time, item.name) + profileText:Insert(line) + end + profileText:SetFocus() +end + +local function CreateProfileFrame() + profileFrame = CreateFrame("Frame", nil, UIParent, "BackdropTemplate") + local frame = profileFrame + frame.backdropInfo = { + bgFile = "Interface\\Tooltips\\UI-Tooltip-Background", + edgeFile = "Interface\\Tooltips\\UI-Tooltip-Border", + tile = true, + tileEdge = true, + tileSize = 8, + edgeSize = 8, + insets = { left = 1, right = 1, top = 1, bottom = 1 }, + } + + -- Output wrap + frame:ApplyBackdrop() + frame:SetBackdropColor(0, 0, 0, 1) + frame:SetSize(640, 480) + frame:SetPoint("TOPRIGHT") + frame:SetFrameStrata("DIALOG") + frame:Hide() + + local scrollFrame = CreateFrame("ScrollFrame", nil, frame, "UIPanelScrollFrameTemplate") + scrollFrame:SetSize(640-16-20, 480-16) + scrollFrame:SetPoint("TOPLEFT", frame, "TOPLEFT", 8, -8) + + profileText = CreateFrame("EditBox", nil, scrollFrame) + profileText:SetFontObject(ChatFontNormal) + profileText:SetWidth(640-16-20) + profileText:SetMultiLine(true) + profileText:SetAutoFocus(false) + scrollFrame:SetScrollChild(profileText) + + profileText:SetScript("OnEscapePressed", function(self) + frame:Hide() + end) +end +omi.SetEventHandler("OMICRON_LOADING", CreateProfileFrame)