Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Query budgets (debugger) #107

Merged
merged 16 commits into from
Aug 13, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions example/src/client/systems/stressTest.luau
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Components = require(ReplicatedStorage.Shared.components)

local function stressTest(world, _, ui)
for id in world:query(Components.Test) do
end
end

return stressTest
7 changes: 7 additions & 0 deletions example/src/game.client.luau
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local StarterPlayer = game:GetService("StarterPlayer")
local start = require(ReplicatedStorage.Shared.start)
local receiveReplication = require(ReplicatedStorage.Client.receiveReplication)

Expand All @@ -8,3 +9,9 @@ local world, state = start({
})

receiveReplication(world, state)

local components = require(ReplicatedStorage.Shared.components)

for i = 1, 1000 do
world:spawn(components.Test())
end
1 change: 1 addition & 0 deletions example/src/shared/components.luau
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ local COMPONENTS = {
"Lasering",
"DebugLabel",
"Spinner",
"Test",
}

local components = {}
Expand Down
18 changes: 17 additions & 1 deletion lib/Loop.luau
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ function Loop.new(...)
_middlewares = {},
_systemErrors = {},
_systemLogs = {},
_debugger = nil,
profiling = nil,
trackErrors = false,
}, Loop)
Expand Down Expand Up @@ -400,7 +401,22 @@ function Loop:begin(events)
profiling[system] = {}
end

rollingAverage.addSample(profiling[system], duration)
local debugger = self._debugger

if debugger and debugger.debugSystem == system and debugger._queries then
local totalQueryTime = 0

for _, query in debugger._queries do
totalQueryTime += query.averageDuration
end

rollingAverage.addSample(
profiling[system],
if debugger.debugSystem then duration - totalQueryTime else duration
)
else
rollingAverage.addSample(profiling[system], duration)
end
end

if coroutine.status(thread) ~= "dead" then
Expand Down
2 changes: 2 additions & 0 deletions lib/debugger/debugger.luau
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ function Debugger.new(plasma)
componentRefreshFrequency = 3,
_windowCount = 0,
_queries = {},
_queryDurationSamples = {},
_seenEvents = {},
_eventOrder = {},
_eventBridge = EventBridge.new(function(...)
Expand Down Expand Up @@ -344,6 +345,7 @@ end
]=]
function Debugger:autoInitialize(loop)
self.loop = loop
loop._debugger = self

self.loop.trackErrors = true

Expand Down
27 changes: 25 additions & 2 deletions lib/debugger/hookWorld.luau
Original file line number Diff line number Diff line change
@@ -1,14 +1,37 @@
--#selene:allow(empty_loop)
local useCurrentSystem = require(script.Parent.Parent.topoRuntime).useCurrentSystem
local World = require(script.Parent.Parent.World)
local rollingAverage = require(script.Parent.Parent.rollingAverage)

local originalQuery = World.query

local function hookWorld(debugger)
World.query = function(world, ...)
if useCurrentSystem() == debugger.debugSystem then
local start = os.clock()

for _ in originalQuery(world, ...) do
end

local duration = os.clock() - start
local file, line = debug.info(2, "sl")

if debugger._queryDurationSamples[file .. line] == nil then
debugger._queryDurationSamples[file .. line] = {}
end

rollingAverage.addSample(debugger._queryDurationSamples[file .. line], duration)

local averageDuration = rollingAverage.getAverage(debugger._queryDurationSamples[file .. line])

local componentNames = {}
for i = 1, select("#", ...) do
table.insert(componentNames, tostring((select(i, ...))))
end

table.insert(debugger._queries, {
components = { ... },
result = originalQuery(world, ...),
averageDuration = averageDuration,
componentNames = componentNames,
})
end

Expand Down
8 changes: 6 additions & 2 deletions lib/debugger/ui.luau
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,6 @@ local function ui(debugger, loop)
local samples = loop.profiling[system]
if samples then
local duration = rollingAverage.getAverage(samples)

durations[system] = duration
longestDuration = math.max(longestDuration, duration)
end
Expand Down Expand Up @@ -178,7 +177,6 @@ local function ui(debugger, loop)
end

local selected = custom.selectionList(items):selected()

if selected then
if selected.system == debugger.debugSystem then
debugger.debugSystem = nil
Expand All @@ -187,6 +185,12 @@ local function ui(debugger, loop)
end
end

if debugger.debugSystem then
debugger.debugSystemRuntime = durations[debugger.debugSystem]
else
debugger.debugSystemRuntime = nil
end

plasma.space(10)
end
end)
Expand Down
42 changes: 4 additions & 38 deletions lib/debugger/widgets/queryInspect.luau
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
local format = require(script.Parent.Parent.formatTable)

return function(plasma)
return plasma.widget(function(debugger)
return plasma
Expand All @@ -20,43 +18,11 @@ return function(plasma)
continue
end

plasma.heading(string.format("Query %d", i))

local componentNames = {}

for _, component in query.components do
table.insert(componentNames, tostring(component))
end

plasma.label(table.concat(componentNames, ", "))

local items = { { "ID", unpack(componentNames) } }

while #items <= 10 do
local data = { query.result:next() }
local budgetUsed =
math.clamp(math.floor((query.averageDuration / debugger.debugSystemRuntime) * 100), 0, 100)

if #data == 0 then
break
end

for index, value in data do
if type(value) == "table" then
data[index] = format.formatTable(value)
else
data[index] = tostring(value)
end
end

table.insert(items, data)
end

plasma.table(items, {
headings = true,
})

if #items > 10 and query.result:next() then
plasma.label("(further results truncated)")
end
plasma.heading(`<b>Query {i} - {budgetUsed}%</b>`)
plasma.label(table.concat(query.componentNames, ", "))
end
return nil
end)
Expand Down
Loading