Skip to content

Profiler

System · Profiler · server and client scripts

Profiler reads frame times, script times, system times, and custom scope times on the machine that runs the script.

Frame

Function
Frame() -> FrameStats?

Frame returns a FrameStats table for the last finished frame, or nil before the first frame ends.

Example

-- Server script.
function Update(dt)
    local stats = Profiler.Frame()
    if stats and stats.Interval > 0.05 then
        warn(string.format("slow frame: %.1f ms", stats.Interval * 1000))
    end
end

Scripts

Function
Scripts() -> {ScriptCost}

Scripts returns a ScriptCost array for scripts that ran recently, sorted by average time descending.

Example

-- Server script: print the slowest script every 5 seconds.
while true do
    task.wait(5)
    local scripts = Profiler.Scripts()
    if #scripts > 0 then
        print(scripts[1].Path, scripts[1].Average)
    end
end

Systems

Function
Systems() -> { SystemCost }

Systems returns an array of system timing entries for registered systems that ran in the last frame.

Example

-- Server script.
local systems = Profiler.Systems()
print(#systems)

Scopes

Function
Scopes() -> { ScopeCost }

Scopes returns an array of custom scope timing entries recorded via Begin and Scope in the last frame.

Example

-- Server script.
local scopes = Profiler.Scopes()
print(#scopes)

Begin

Function
Begin(label: string) -> ()

Begin starts a named profiling scope on the current script thread. Match every Begin with a call to End.

Example

-- Server script.
Profiler.Begin("Work")
Profiler.End()

End

Function
End() -> ()

End closes the innermost open profiling scope started by Begin.

Example

-- Server script.
Profiler.Begin("Work")
Profiler.End()

Scope

Property
Scope: <R...>(label: string, fn: () -> R...) -> R...

Scope runs fn inside a named profiling scope and closes the scope even when fn raises an error.

Example

-- Server script.
local total = Profiler.Scope("Sum", function()
    return 1 + 2
end)