Last active
June 7, 2023 23:43
-
-
Save 1bardesign/b8119eb2615f645502d3df21053c2a96 to your computer and use it in GitHub Desktop.
prerelease basic profiler for lua, depends on batteries and uses lg as an alias for love.graphics - will be included in ferris at some point
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Copyright 2023 Max Cahill | |
This software is provided 'as-is', without any express or implied | |
warranty. In no event will the authors be held liable for any damages | |
arising from the use of this software. | |
Permission is granted to anyone to use this software for any purpose, | |
including commercial applications, and to alter it and redistribute it | |
freely, subject to the following restrictions: | |
1. The origin of this software must not be misrepresented; you must not | |
claim that you wrote the original software. If you use this software | |
in a product, an acknowledgment in the product documentation would be | |
appreciated but is not required. | |
2. Altered source versions must be plainly marked as such, and must not be | |
misrepresented as being the original software. | |
3. This notice may not be removed or altered from any source distribution. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--measuring the time and memory allocated by a stretch of code | |
local profiler = class({ | |
name = "profiler" | |
}) | |
function profiler:new() | |
self._stack = {} | |
self._result = {} | |
end | |
function profiler:push(name) | |
if #self._stack == 0 then | |
--pushing a new frame | |
table.clear(self._result) | |
end | |
local block = { | |
name = name, | |
time = love.timer.getTime(), | |
memory = (collectgarbage("count") * 1024), | |
depth = #self._stack + 1, | |
} | |
table.insert(self._result, block) | |
table.insert(self._stack, block) | |
end | |
function profiler:pop(name) | |
local block = table.pop(self._stack) | |
assert:equal(name, block.name, "profiler block names should match") | |
block.time = (love.timer.getTime() - block.time) * 1000 | |
block.memory = (collectgarbage("count") * 1024) - block.memory | |
end | |
function profiler:wrap_system(name, system) | |
if system.update then | |
system.update = self:wrap_function(name .. "_update", system.update) | |
end | |
if system.draw then | |
system.draw = self:wrap_function(name .. "_draw", system.draw) | |
end | |
return system | |
end | |
function profiler:wrap_function(name, f) | |
return function(...) | |
self:push(name) | |
local result = {f(...)} --capture | |
self:pop(name) | |
return unpack(result) | |
end | |
end | |
function profiler:result() | |
return table.copy(self._result) | |
end | |
function profiler:print_result() | |
lg.push() | |
local f = lg.getFont() | |
local line_height = f:getHeight() * f:getLineHeight() | |
for _, v in ipairs(self:result()) do | |
lg.setColor(0, 0, 0) | |
lg.rectangle("fill", -5, 0, 355, line_height) | |
lg.setColor(1, 1, 1) | |
lg.push() | |
lg.translate(v.depth * 10, 0) | |
lg.print(v.name, 0, 0) | |
lg.print(("%05.2fms"):format(v.time), 150, 0) | |
lg.print(("%04.2fmb"):format(v.memory / 1024 / 1024), 250, 0) | |
lg.pop() | |
lg.translate(0, line_height) | |
end | |
lg.pop() | |
end | |
return profiler |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
profiler = require("src.profiler")() --wherever you put it; global so that you can push/pop more frames "wherever" | |
--either put it in your main loop, or dos omething like this | |
function love.update(dt) | |
profiler:push("frame") | |
profiler:push("update") | |
--do all your update stuff here | |
profiler:pop("update") | |
end | |
function love.draw(t) | |
profiler:push("draw") | |
--draw everything | |
profiler:pop("draw") | |
profiler:pop("frame") | |
--draw the profiler on top if you're pressing ` | |
if input.keyboard:pressed("`") then | |
lg.push() | |
lg.origin() | |
lg.translate(10, 10) | |
--set whatever font here | |
profiler:print_result() | |
lg.pop() | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment