-- title: libProfile -- author: Gavin Stewart -- desc: v1.0 TIC-80 profiling library -- script: lua -- input: keyboard function scanline(lineNum) profile:start('scanline') profile:stop('scanline') end function OVR () profile:start('OVR') profile:stop('OVR') profile:update(14, 1) end function TIC () profile:start('TIC') cls(0) profile:stop('TIC') end -- Profile tool for TIC-80/lua -- Copyright (c) 2018 Gavin Stewart -- Licensed under the terms of the -- MIT License here: -- https://opensource.org/licenses/MIT profile = { -- Private variables version = '1.0', data = {}, frame = 0, output = { text = '', calc = {}, calcFrames = 0, calcRam = 0, calcTime = 0, frames = 0, period = 1, -- in frames pause = false, }, shade = { state = 'hide', -- show|hide offset = 0, } } -- Profile public methods -- profile:start(name) -- @param name <= 13 chars function profile:start (name) name=string.sub(name,1,13) if not self.data[name] then self.data[name]={} self.data[name].acc = 0 self.data[name].count = 0 end self.data[name].start=time() end -- profile:stop(name) -- @param name <= 13 chars function profile:stop (name) name=string.sub(name,1,13) if not self.data[name] or not self.data[name].start then self.data[name]={} self.data[name].acc = 0 self.data[name].count = 0 trace('profile:stop called before start for: '..name.."\n") return end self.data[name].acc = self.data[name].acc + time() - self.data[name].start self.data[name].count = self.data[name].count + 1 end -- profile:update(fgColour, bgColour) -- Call once per frame from end of -- TIC() or OVR(). Press ` key to -- see output on screen. -- @param fgColour Optional palatte -- index else defaults to 15 -- @param bgColour Optional palatte -- index else defaults to 0 function profile:update (fgColour, bgColour) fgColour = fgColour or 15 bgColour = bgColour or 0 self.frame = self.frame + 1 -- Show or hide with ` key if keyp(44,0,30) then if self.shade.state == 'show' then self.shade.state = 'hide' self.output.pause = false else self.shade.state = 'show' end end -- Pause output with p key (while show) if keyp(16,0,30) then if self.shade.state == 'show' then if self.output.pause == true then self.output.pause = false else self.output.pause = true end end end -- Less output.period with [ key if keyp(39,0,7) then if self.shade.state == 'show' then self.output.period = self.output.period - 1 if self.output.period < 1 then self.output.period = 1 end end end -- More output.period with ] key if keyp(40,0,7) then if self.shade.state == 'show' then self.output.period = self.output.period + 1 if self.output.period > 999 then self.output.period = 999 end end end profile:updateText() -- Send output text to trace() -- even if output not shown to screen. if keyp(20,0,30) then trace(self.output.text, fgColour) end -- Shade up or down local _, outputLines = self.output.text:gsub('\n', '\n') if self.shade.state == 'show' and self.shade.offset < 0 then self.shade.offset = self.shade.offset + 2 elseif self.shade.state=='hide' and self.shade.offset >= -6*outputLines then self.shade.offset = self.shade.offset - 2 end -- Display output print(self.output.text, 1, self.shade.offset+1, bgColour, true) print(self.output.text, 0, self.shade.offset, fgColour, true) end -- Profile private methods. -- Use acc(umulated) time and call -- count to build calc table in acc -- order. function profile:updateCalc() self.output.calc={} self.output.calcTime = time()/1000 self.output.calcFrames = self.output.frames self.output.calcRam=collectgarbage('count') -- Calculate sum of scanline+Ovr+Tic self.data['scnln+OVR+TIC'] = { acc = 0, count = 0, } for i, n in pairs({'scanline', 'OVR', 'TIC'}) do if self.data[n] then self.data['scnln+OVR+TIC'].acc = self.data['scnln+OVR+TIC'].acc + self.data[n].acc self.data['scnln+OVR+TIC'].count = self.data['scnln+OVR+TIC'].count + self.data[n].count end end -- Build calc table, order by acc for name in pairs(self.data) do table.insert(self.output.calc, {name=name, acc=self.data[name].acc, count=self.data[name].count}) -- Zero acc and count self.data[name].acc = 0 self.data[name].count = 0 end function compAcc(a,b) return(a.acc > b.acc) end table.sort(self.output.calc, compAcc) end -- Build up output.text from -- calc table. function profile:updateText() -- Recalculate at period intervals if self.output.pause==false then self.output.frames = self.output.frames+1 if self.output.frames >= self.output.period then profile:updateCalc() self.output.frames = 0 end else self.data = {} self.output.frames = 0 end -- Header local pauseState if self.output.pause == true and self.frame%60 > 30 then pauseState = ' ' else pauseState = ':' end self.output.text = string.format( "Profile v%3s (C)2018 Gavin Stewart\n", self.version).. "----------------------------------------\n" local now = time()/1000 self.output.text = self.output.text .. string.format( "%02.0f:%05.2f %3dfrms | %02.0f%1s%05.2f %10.1fKB\n", now//60%100, now%60, self.output.period, self.output.calcTime//60%100, pauseState, self.output.calcTime%60, self.output.calcRam ) -- Profile table self.output.text = self.output.text .. "--------+--------+-------+--------------\n".. " calls | totl ms| ms/frm| name \n".. "--------+--------+-------+--------------\n" -- Output ordered calc table for i, t in ipairs(self.output.calc) do self.output.text = self.output.text .. string.format( "%7d | %6.1f |%6.2f | %-13s \n", t.count, t.acc, t.acc / self.output.calcFrames, t.name) end -- Help footer self.output.text = self.output.text .. "--------+--------+-------+--------------\n".. " ` show [] +-frms (p)ause (t)race\n" end 0 D$404mNJNL04e$FHuqaY}},m,Ҫm^ GIF89aw! NETSCAPE2.0!,^ D$4 4(!#*|qċ32cȥi;k ^mV#f*~q?+}K'o~YW^~OB*Q(_=v ($h( ܋>'c071Ҙ#6#7B8Vפ]O&(QRPb9%ZZ[V%^hlp ܏ň/ )cj\P}hR[~DOmUSɗfXYFi]epاrEP+gn[k6yV[enlI1l~bVhԊYݤ+Vܭ]6Ӄ!Ut Zբ>=nq9\to)׸JT6Epkj4YlY>&۴e^YGF 6YNi8,_W,&#,8笳6 g; h@]dH4LPH|?<c=[W\g)nA-t-W(]CZFz]7_{^wesb +YPVQe5ᄍw2Olo.}:Ңe)Hk+_eĞ{sD00=|\Q [qZ*M;ww#9[~0⪟yg0*5bgJ$W%iU2;ֵղ_$"Q$쌙ǒzTu3H>as jlctT3Ir @T^0=1b~&'Ѣ*KR#z\ 8@VrL[(m٧Q| U S 1[H3%`f<|&WmlhɜNcOk\%Y)} kWC&kGFtsdbl6pde%β; )S1Ed21JDIQ(U>9)u`G~m#c+Rn)o=OKJtƻ aآR~.ŧSYuodC٩Oj6+X+u/Y۔5elf&NMl璊+Xjؾ]̇5zkV_Fr*NBZÝ3A9_Sأ,^gG❨:חH-Q)Tyv9R( .3ގQ0\Jl;de;Hf%I^I;UV0J`6`?uv[uZ<@%Pj̇V+X<.η~NO;'N[ϸ7{ GN(LOW0gN8Ϲw@ЇNHOҗ;PԧN[XϺַ= ;