From fa55cd6764426cffb8619cb67127f8f0029ebe05 Mon Sep 17 00:00:00 2001 From: devfirefly Date: Mon, 9 Nov 2015 16:33:02 +0000 Subject: [PATCH] Added functions: unpack, setKeys, parse Renamed dettach to detach --- main.lua | 17 ++-- piefiller.lua | 232 ++++++++++++++++++++++++++++++-------------------- 2 files changed, 143 insertions(+), 106 deletions(-) diff --git a/main.lua b/main.lua index 575e1e4..88ee6cb 100644 --- a/main.lua +++ b/main.lua @@ -23,29 +23,22 @@ end function love.draw() if drawRect then drawRectangles() - Prof:dettach() + Prof:detach() end if ProfOn then Prof:draw({50}) end end -function hay() -end function love.update(dt) Prof:attach() iterateSmall() iterateLarge() if drawRect then - Prof:dettach(true) + Prof:detach(true) else - Prof:dettach() - end - if hey then - hay() - hay() - hay() - hay() - end + Prof:detach() + end + local data = Prof:unpack() end function love.keypressed(key) if key == "esc" then diff --git a/piefiller.lua b/piefiller.lua index fb2714c..200b220 100644 --- a/piefiller.lua +++ b/piefiller.lua @@ -1,5 +1,5 @@ local path = ... -local profiler = {} +local piefiller = {} local function hsvToRgb(h, s, v) local h,s,v = h,1,1 local h,s,v = math.fmod(h,360),s,v @@ -40,10 +40,11 @@ function setColor(...) end local color_data = {} local colors = {} -function profiler:new() +function piefiller:new() local self = {} - setmetatable(self,{__index = profiler}) + setmetatable(self,{__index = piefiller}) self.data = {} + self.parsed = {} self.last = 0 self.timer = 0 self:reset() @@ -53,9 +54,19 @@ function profiler:new() self.y = 0 self.scale = 1 self.step = 1 + self.keys = { + reset = "r", + increase_depth = "down", + decrease_depth = "up", + increase_step_size = ",", + decrease_step_size = ".", + shorten_names = "s", + show_hidden = "c", + save_to_file = "p", + } return self end -function profiler:reset() +function piefiller:reset() self.data = {} self.x = 0 self.y = 0 @@ -64,7 +75,32 @@ function profiler:reset() table.insert(colors,hsvToRgb(i,100,100)) end end -function profiler:attach() +function piefiller:setKey(table_or_mode,key) + if type(table_or_mode) == "table" then + self.keys = table_or_mode + elseif type(table_or_mode) == "string" then + if not self.keys[table_or_mode] then error("Invalid mode: "..tostring(table_or_mode)) end + self.keys[table_or_mode] = key + else + error("Expected table or string got:"..type(table_or_mode)) + end +end + +function piefiller:parse(caller,parent) + return { + parent = parent, + func = caller.func, + count = 0, + time = 0, + child_time = 0, + named_child_time = 0, + children = {}, + children_time = {}, + info = caller, + kids = {}, + } +end +function piefiller:attach() self.last = os.clock() local function hook() local depth = self.depth @@ -79,18 +115,7 @@ function profiler:attach() local name = caller.func local lc = self.data[last_caller.func] if not lc.kids[name] then - lc.kids[name] = { - parent = last_caller, - func = caller.func, - count = 0, - time = 0, - child_time = 0, - named_child_time = 0, - children = {}, - children_time = {}, - info = caller, - kids = {}, - } + lc.kids[name] = self:parse(caller,last_caller) end local kid = lc.kids[name] kid.count = kid.count + 1 @@ -99,17 +124,7 @@ function profiler:attach() local name = caller.func local raw = self.data[name] if not raw then - self.data[name] = { - func = caller.func, - count = 0, - time = 0, - child_time = 0, - named_child_time = 0, - children = {}, - children_time = {}, - info = caller, - kids = {}, - } + self.data[name] = self:parse(caller,last_caller) end raw = self.data[name] raw.count = raw.count + 1 @@ -128,7 +143,7 @@ function profiler:attach() end debug.sethook(hook,"",step) end -function profiler:dettach(mode) +function piefiller:detach(stop) local totaltime = 0 local parsed = {} local no = 0 @@ -143,6 +158,7 @@ function profiler:dettach(mode) parsed[i].def = v.info.linedefined parsed[i].cur = v.info.currentline parsed[i].item = v + parsed[i].caller = v.info if not color_data[v.func] then local i = math.random(#colors) color_data[v.func] = colors[i] @@ -157,35 +173,25 @@ function profiler:dettach(mode) end self.parsed = parsed self.totaltime = totaltime - if not mode then debug.sethook() end + if not stop then debug.sethook() end end local largeFont = love.graphics.newFont(25) -function profiler:draw(args) +function piefiller:draw(args) local loading local oldFont = love.graphics.getFont() - local w,h = love.graphics.getDimensions() - love.graphics.push() - love.graphics.translate(self.x,self.y) - love.graphics.scale(self.scale) local args = args or {} local rad = args["rad"] or 200 local mode = args["mode"] or "simple" local ret = args["return"] local pi = math.pi - if ret then - local lastangle = 0 - local pieChart = {} - for i,v in ipairs(self.parsed) do - local angle = 3.6*v.prc - local segment = { - name = v.name or v.linedefined, - angle = angle, - start = lastangle, - finish = lastangle + angle, - } - lastangle = lastangle + angle - end - end + local arc = love.graphics.arc + local w,h = love.graphics.getDimensions() + + love.graphics.push() + + love.graphics.translate(self.x,self.y) + love.graphics.scale(self.scale) + if self.parsed and self.totaltime > 0 then local lastangle = 0 local font = love.graphics.getFont() @@ -194,10 +200,10 @@ function profiler:draw(args) local cx,cy = w/2,h/2 local angle = math.rad(3.6*v.prc) setColor(color) - love.graphics.arc("fill",cx,cy,rad,lastangle,lastangle + angle) + arc("fill",cx,cy,rad,lastangle,lastangle + angle) setColor(colors.black) if v.prc > 1 then - love.graphics.arc("line",cx,cy,rad,lastangle,lastangle + angle) + arc("line",cx,cy,rad,lastangle,lastangle + angle) end lastangle = lastangle + angle setColor() @@ -247,10 +253,12 @@ function profiler:draw(args) fw = largeFont:getWidth(t) love.graphics.print("Loading...",w/2 - fw/2,h/2) end + love.graphics.pop() + love.graphics.setFont(oldFont) end -function profiler:mousepressed(x,y,b) +function piefiller:mousepressed(x,y,b) if b == "wu" then local scale = self.scale - math.floor((0.05*self.scale)*1000)/1000 if scale > 0 and scale > 0.1 then @@ -282,48 +290,84 @@ function profiler:mousepressed(x,y,b) end end end -function profiler:keypressed(key) - if key == "r" then - self:reset() - elseif key == "up" then - self:reset() - self.depth = self.depth - 1 - elseif key == "down" then - self:reset() - self.depth = self.depth + 1 - elseif key == "," then - self.step = self.step - 1 - elseif key == "." then - self.step = self.step +1 - elseif key == "s" then - self.small = not self.small - elseif key == "c" then - self:reset() - self.view_children = not self.view_children - elseif key == "p" then - local parsed = copy(self.parsed) - table.sort(parsed,function(a,b) - return a.prc > b.prc - end) - local d = {"Depth: "..self.depth.." with step: "..self.step.."\r\n".."Total time: "..self.totaltime.."\r\n"} - - for i,v in ipairs(parsed) do - local instance = { - "-----"..(v.name or "def@"..v.def).."-----", - "source:"..v.src..":"..v.def, - "current line: "..v.cur, - "time: "..v.time, - "percentage: "..math.ceil(v.prc).." %", - "----------------", - } - for i,v in ipairs(instance) do - instance[i] = v.."\r\n" - end - table.insert(d,table.concat(instance)) +function piefiller:keypressed(key) + local mode + for i,v in pairs(self.keys) do + if key == v then + mode = i + break end - local data = table.concat(d) - love.filesystem.write("Profile",data) - love.system.openURL(love.filesystem.getRealDirectory("Profile")) + end + if mode then + if mode == "reset" then + self:reset() + elseif mode == "increase_depth" then + self:reset() + self.depth = self.depth + 1 + elseif mode == "decrease_depth" then + self:reset() + self.depth = self.depth - 1 + elseif mode == "increase_step_size" then + self.step = self.step - 1 + elseif mode == "decrease_step_size" then + self.step = self.step +1 + elseif mode == "shorten_names" then + self.small = not self.small + elseif mode == "show_hidden" then + self:reset() + self.view_children = not self.view_children + elseif mode == "save_to_file" then + local parsed = copy(self.parsed) + table.sort(parsed,function(a,b) + return a.prc > b.prc + end) + local d = {"Depth: "..self.depth.." with step: "..self.step.."\r\n".."Total time: "..self.totaltime.."\r\n"} + + for i,v in ipairs(parsed) do + local instance = { + "-----"..(v.name or "def@"..v.def).."-----", + "source:"..v.src..":"..v.def, + "current line: "..v.cur, + "time: "..v.time, + "percentage: "..math.ceil(v.prc).." %", + "----------------", + } + for i,v in ipairs(instance) do + instance[i] = v.."\r\n" + end + table.insert(d,table.concat(instance)) + end + local data = table.concat(d) + love.filesystem.write("Profile",data) + love.system.openURL(love.filesystem.getRealDirectory("Profile")) + end end end -return profiler +function piefiller:unpack(fn) + local data = { + items = {}, + about = { + depth = self.depth, + step = self.step, + totalTime = self.totaltime, + }, + } + for i,v in ipairs(self.parsed) do + local a = { + name = v.name, + line_defined = v.def, + current_line = v.cur, + source = v.src, + time_taken = v.time, + precentage = v.prc, + caller = v.caller, + } + if fn then + assert(type(fn) == "function","Expected function got:"..type(fn)) + fn(a) + end + table.insert(data.items,a) + end + return data +end +return piefiller \ No newline at end of file