Skip to content

Instantly share code, notes, and snippets.

@EngineerSmith
Last active March 31, 2022 19:15
Show Gist options
  • Save EngineerSmith/956f2cc2832cbb751686c4b438f0366d to your computer and use it in GitHub Desktop.
Save EngineerSmith/956f2cc2832cbb751686c4b438f0366d to your computer and use it in GitHub Desktop.
local appleCake = require("AppleCake")(true) -- False will remove the profiling tool from the project
appleCake.beginSession() -- Will create "profile.json" in the save directory by default
appleCake.setBuffer(true) -- buffer all profiling calls before pushing to be saved out. Function works per thread.
local thread = love.thread.newThread("TestThread.lua") -- Must start after appleCake has been started, to ensure this thread is owner of appleCake
thread:start() -- You can call appleCake's profiling and mark functions within another thread, see further docs for more details
function love.quit()
appleCake.mark("Quit", "p") -- Add markers for timeless events, by default it will show for the entire process, "t" will show mark for only the current thread in the data
appleCake.endSession() -- In the event of a crash or endSession isn't reached, you can still recover the data
-- End session also flushes any unflushed data, due to buffering being set to true
end
function love.load()
appleCake.mark("Started load") -- Adds a mark, can be used to show an events or other details
end
local profileLoop -- Reuse tables to avoid garbage
local function loop(count)
profileLoop = appleCake.profile("Loop "..count, nil, profileLoop) -- Wrap a section of code in a profile, it doesn't have to be the entire function
local n = 0
for i=0,count do
n = n + i
appleCake.counter("loop", {n}) -- record variable, and make a bar graph of it's change
end
appleCake.counter("loop", {0})
profileLoop:stop() -- will write the result to file - once data has been flushed (will do it instantly if setBuffer is false)
end
local r, mem = 0, 0
local profileUpdate -- Reuse tables to avoid garbage
function love.update(dt)
profileUpdate = appleCake.profileFunc(nil, profileUpdate) -- Auto-generates a name for the function once as we reuse, "[email protected]#32"
r = r + 0.5 * dt
loop(100000) -- Example of nested profiling, as the function has it's own profile
profileUpdate:stop()
mem = mem + dt
if mem > 0.1 then -- We record memory every 0.1 seconds
appleCake.countMemory() -- Adds a bar on a bar graph with details of current lua memory usage
mem = 0
end
end
local lg = love.graphics
function love.draw() -- "[email protected]#45", generates name for function
local profileDraw = appleCake.profileFunc() -- This will create new profile table every time this function is ran
lg.push()
lg.translate(50,50)
lg.rotate(r)
lg.rectangle("fill", 0,0,30,30)
lg.pop()
profileDraw.args = lg.getStats() -- Set args that we can view later in the viewer
profileDraw:stop() -- By setting it to love.graphics.getStats we can see details of the draw
appleCake.flush() -- Flush any profiling data out, would be useful to write love.run to include it
end
function love.keypressed(key)
appleCake.mark("Key Pressed", nil, {key=key}) -- Adds a mark every time a key is pressed, with the key as an argument
end
local appleCake = require("AppleCake")() -- Will be disabled if the main thread set AppleCake to false
-- Note we don't set buffering so everything is pushed to the save thread as soon as it can be
local function foo() -- "[email protected]#3"
local profile = appleCake.profileFunc()
local n = 0
for i=0, 100000 do
n = n + i
end
profile:stop()
end
while true do
foo()
love.timer.sleep(1) -- love.timer is required by AppleCake on threads, only if profiling is turned on
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment