# Protocol: profile

The `profile` commands a program sends to a running game, as the engine describes them: what each takes, what it answers and what it does. Back to [all commands](../protocol-commands.md).

## profile.frame

`profile.frame {step?: integer} -> {frame: integer, step: integer, total_ms: number, spans: [record], counters: [record], recorded: value, ms: number, stopped: value, replayed: value}` · read, read-only

Returns the timed spans and counters of the last finished frame that drew a picture, and which step it was of: a request that only reads draws none and changes nothing of the answer. With step, where that step's time went as it was played, as profile.steps tells a step, with recorded.

| Parameters | Type | Meaning |
| --- | --- | --- |
| `step?` | integer | A step, 1 or more: its record as profile.steps gives one in worst, from the time it was played (recorded true) while that is kept, as it is for the last 3,600 step numbers and for the 16 slowest steps of the run however old; else as the engine last played it again, or its time alone (recorded false). Left out: the last frame that drew. |

| Result | Type | Meaning |
| --- | --- | --- |
| `frame` | integer | How many frames had finished when it did. With a window a frame is one picture; without one, one request. 0 before a frame drew. Without step. |
| `step` | integer | The step the picture was of: null before a frame drew. With step, the step asked. |
| `total_ms` | number | How long the frame lasted, from the end of the one before. Without step. |
| `spans` | [record] | One {name, count, total_ms, max_ms} per span that ran in the frame. With step, one {name, ms, count, parent, at} per place a span ran in the step, as profile.steps gives them, or null when none is kept. |
| `counters` | [record] | One {name, value} per counter that was added to in the frame. With step, an object of the step's counters, as profile.steps gives them, or null. |
| `recorded` | value | With step: true when spans and counters are as the step was played, false when they are of the engine playing it again or none are kept. |
| `ms` | number | With step: the time inside the step, as profile.steps gives it, or null when nothing of it is kept. |
| `stopped` | value | With step: as profile.steps gives it. |
| `replayed` | value | With step: as profile.steps gives it. |

## profile.frames

`profile.frames {from?: integer, to?: integer} -> {from: integer, to: integer, kept: integer, median_ms: number, max_ms: number, spans: [record]}` · read, read-only

Sums up a range of finished frames: the median and the longest, for the whole frame and for each span. One slow frame does not move a median.

| Parameters | Type | Meaning |
| --- | --- | --- |
| `from?` | integer | The first frame, as profile.frame numbers them. Left out: the oldest kept. |
| `to?` | integer | The last frame. Left out: the latest. |

| Result | Type | Meaning |
| --- | --- | --- |
| `from` | integer | The first frame the numbers cover: the range asked for, cut to the frames kept. 0 when there is none. |
| `to` | integer | The last frame they cover. |
| `kept` | integer | How many finished frames are kept at most. |
| `median_ms` | number | The median length of a frame in the range. |
| `max_ms` | number | The longest frame. |
| `spans` | [record] | One {name, frames, median_ms, max_ms} per span: in how many frames of the range it ran, and the median and the most it took in one of those. |

## profile.steps

`profile.steps {from?: integer, to?: integer, worst?: integer, spans?: value, buckets?: integer, totals?: value} -> {step: integer, from: integer, to: integer, steps: [record], buckets: [record], worst: [record], totals: record, kept: record}` · read, read-only

Says what each step took, and names the slowest with where their time went: each stage, each script, and the picture drawn of the step. What a timeline shows as a cost row, and what a click on a spike asks. Every step's time is kept for an hour of steps and where it went for a minute, whether or not anybody asks, by step number: a step played again is told as it last ran, the engine playing steps again to reach another (game.seek, blame.on) never hides a step as it was played, and a game.try from a kept moment keeps nothing. Time is not part of the game: it is in no checksum and no snapshot, and differs from run to run.

| Parameters | Type | Meaning |
| --- | --- | --- |
| `from?` | integer | The first step. Left out: 3,599 steps before `to`, so that the range is the last minute. |
| `to?` | integer | The last step. Left out: the step the game is at. Steps after it are of a run the game came back from: they are told, while they are kept, only when asked for. |
| `worst?` | integer | How many of the slowest steps of the range to name, 0 to 60. Left out: 5. |
| `spans?` | value | true to give every step of the range its spans and counters, as `worst` has them: for a range of at most 120 steps. Left out: false. |
| `buckets?` | integer | Cut the range into this many stretches, 1 to 2,000, as equal as whole steps allow, and give the slowest step of each in place of the list: what a cost row of a long run draws, one stretch a column. With it, `from` left out is the oldest step kept, and the range may be of any length. Left out: the steps are listed. |
| `totals?` | value | true to add up every span of the range, each place in the tree once, as far as the steps' spans are kept: what a profiler shows of a stretch of the game. Left out: false. |

| Result | Type | Meaning |
| --- | --- | --- |
| `step` | integer | How many fixed steps have run. |
| `from` | integer | The first step the answer covers: the range asked for, cut to the steps kept. 0 when there is none. |
| `to` | integer | The last step it covers. |
| `steps` | [record] | One {step, ms, replayed} per step of the range that is kept, in step order, at most 3,600: ms is the time inside the step, all its stages together, and replayed is as in worst. With spans true each is a whole record, as in worst. null with buckets. |
| `buckets` | [record] | With buckets: one {from, to, ms, step} per stretch of the range, in step order, no more of them than the range has steps: its first and last step, and its slowest step and that step's ms (of two as slow, the earlier), both null when no step of it is kept. null without buckets. |
| `worst` | [record] | The slowest steps of the range, the slowest first: one {step, ms, stopped, replayed, spans, counters} each. spans is one {name, ms, count, parent, at} per place a span ran in from the step's beginning to the next step's, count saying how many times it ran there. They are a tree: parent is the index in the same list of the span it ran inside, which comes before it, or null at the top. "step" is the whole step; inside it "input", "ui", "physics", "scripts", "animation", "layout" and "events" its stages; inside a stage what that stage does ("physics.solve"); inside "scripts" "script NAME", one script's part; inside that "step()" or "on_scene()", the function it called; and inside a function each engine call it made whose cost depends on the world (find, each, spawn, despawn, events, tween, library, physics.ray, physics.cast, physics.overlap and save's), added together per line that called it. at is where in a script a span is, as "FILE:LINE": the line a function starts on, and the line that made a call (a call made through pcall is at the line that handed it over); null for the rest. One name may be in many places; get, set, has and the like are not timed, and are in the time of the function that called them. What the tool did before the next step began is there too, and is no part of ms: the picture drawn of the step (such as "render.build" and "gpu.submit"; of a step drawn more than once, as a paused game is, the last picture, once) and, under frost dev, the requests answered ("protocol"). counters is an object of what was counted in the same time, such as draw_calls and quads when a picture was drawn (of a step drawn more than once, as a paused game is, the last picture's, not their sum), and script_work: the work the scripts did, as the Luau machine counts its calls and loop turns, the same on every machine and every run (unlike ms), so steps can be ranked by it; "script_work NAME" is one script's part of it. stopped is true when a debugger stopped a script in the step: the wait is in no span, so ms is what the step computed, and pictures drawn while it waited are counted in its draw spans. replayed is true when the numbers are of the engine playing the step again on the way to another (game.seek, blame.on), which draws nothing. A replay is kept over anything but the same step as it was played: a step played more than a minute ago and sought to again has its spans, and its ms, from the replay, while steps keeps the time it was played. A step older than the last 3,600 step numbers, not sought to since, keeps its time and no more (stopped, spans and counters are null), unless it is one of the 16 slowest steps the game played, which keep theirs as played for as long as the run goes on. |
| `totals` | record | With totals true: {from, to, steps, spans}, every span of the steps from `from` to `to` (as asked, or the defaults of the list or of buckets) whose spans are still kept, added together, each place in the tree once in the shape of a step's spans, ms and count summed. from and to are the first and last step added, steps how many (0, 0 and 0 when none is kept): spans are kept for the last 3,600 step numbers. null without totals. |
| `kept` | record | What is kept: {spans, steps, bytes, bytes_per_span, bytes_per_hour}. spans is how many of the last step numbers keep where their time went, 3,600 (a minute at sixty steps a second); steps how many keep their time, 216,000 (an hour). bytes is what is held now: bytes_per_span for each span or counter that has run in a kept step, and bytes_per_hour for an hour of steps, held as the run grows. A shipped game keeps nothing. |
