+ Reply to Thread
Results 1 to 1 of 1
Like Tree2Likes
  • 2 Post By Ferather

Thread: [Test Addon] CPU usage

  1. #1
    Rift Chaser Ferather's Avatar
    Join Date
    Jun 2011
    Posts
    341

    Default [Test Addon] CPU usage

    Heres a code ive written for people to see thier addons CPU cost in certain sections, Should help cut the CPU cost of some addons down

    Feel free to copy paste:

    Code:
    ---------------------------------------------------------
    -------------------- CPU Manager 2.2 --------------------
    ---------------------------------------------------------
    
    CPUMan = {
    	version = "2.2",
    	UI = {},
    	Util = {},
    	Event = {},
    }
    
    Interface = {}
    CPUMan_enabled = true
    CPUMan_lastUpdate = 0
    CPUMan_x = 820
    CPUMan_y = 600
    
    function Interface:new()
    	local o = {state = {}}
    
    	setmetatable(o, self)
    	self.__index = self
    
    	o.context = UI.CreateContext("Context")
    	o.context:SetVisible(CPUMan_enabled)
    
    	o.main = UI.CreateFrame("Text", "CPUMan Main", o.context)
    	o.main:SetPoint("TOPLEFT", UIParent, "TOPLEFT", CPUMan_x, CPUMan_y)
    	o.main:SetBackgroundColor(0, 0, 0, 0.2)
    	o.main:SetVisible(true)
    	o.main:SetFontSize(14)
    
    	function o.main.Event:LeftDown()
    		local mouse = Inspect.Mouse()
    		o.state.mouseDown1 = true
    		o.state.startX1 = o.main:GetLeft()
    		o.state.startY1 = o.main:GetTop()
    		o.state.mouseStartX1 = mouse.x
    		o.state.mouseStartY1 = mouse.y
    		o.main:SetBackgroundColor(0.2, 0.2, 0.2, 0.88)
    		CPUMan_Mousedown = true
    	end
    
    	function o.main.Event:MouseMove()
    		if o.state.mouseDown1 then
    			o.state.mouseMove1 = true
    			local mouse = Inspect.Mouse()
    			CPUMan_x = mouse.x - o.state.mouseStartX1 + o.state.startX1
    			CPUMan_y = mouse.y - o.state.mouseStartY1 + o.state.startY1
    			o.main:SetPoint("TOPLEFT", UIParent, "TOPLEFT", CPUMan_x, CPUMan_y)
    		end
    	end
    
    	function o.main.Event:LeftUp()
    		if o.state.mouseDown1 then
    			o.state.mouseDown1 = false
    			o.main:SetBackgroundColor(0, 0, 0, 0.2)
    		end
    		CPUMan_Mousedown = false
    	end
    
    	return o
    end
    
    function Interface:SetText(text)
    	self.main:SetText("")
    	self.main:SetText(text)
    end
    
    function CPUMan.Init(addon)
    	if CPUMan_running then return end
    
    	if addon == "CPUMan" then
    		CPUMan.UI.Interface = Interface:new()
    	end
    
    	CPUMan_running = true
    end
    
    function CPUMan.Update()
    	if CPUMan_Mousedown then return end
    
    	local data = Inspect.Addon.Cpu()
    	local now = Inspect.Time.Frame()
    
    	local output = ""
    	local total = 0
    
    	if now - CPUMan_lastUpdate > 0.88 then
    		CPUMan_lastUpdate = now
    		
    		for k,v in pairs (data) do
    			if k == "YOURADDONHERE" then
    				for k,v in pairs (v) do
    					local match = string.match(k, "%p")
    					local text = k .. ": " .. string.format("%.4f%%", v * 100)
    					
    					if not match then
    						total = total + v
    						output = output .. text .. " \n"
    						CPUMan.UI.Interface:SetText(output .. "\n" .. " Total: " .. string.format("%.4f%%", total * 100))
    					end
    				end
    			end
    		end
    	end
    end
    
    table.insert(Event.Addon.Load.End, {CPUMan.Init, "CPUMan", "Initital Setup"})
    table.insert(Event.System.Update.Begin, {CPUMan.Update, "CPUMan", "Update UI"})
    Change the line: if k == "YOURADDONHERE" then

    To your addon's name

    The frame is move-able my default and will grow in width and height in accordance to the text.
    Don't forget to make a toc file.
    Last edited by Ferather; 03-10-2013 at 01:18 PM.

+ Reply to Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts