# Recipe: profile a frame

Goal: find what a slow step spends its time on, change it, and prove the change made it
faster without changing the game. The game's own recording is the benchmark.

## Measure

```sh
frost bench game.toml                  # median, p99, worst a step; by stage and span
frost bench --draw game.toml           # and the off-screen draw: draw calls, quads
frost bench --json game.toml > before.json
```

Read three lines first: the load average (over about 4, the numbers lie: measure again
on an idle machine, or compare only runs under the same load), `slowest: ... at step N`
(the step and where its time went), and the stage with the largest median.

On the third-person sample the slowest line
reads `slowest: 0.388 ms at step 127: scripts 0.173 ms, of which script main.luau 0.159
ms`: a script, not physics or drawing.

## Look at one step

Over the protocol (`frost serve game.toml`, or the `frost mcp` tools with `_` for `.`):

```json
{"method":"game.seek","params":{"step":127}}
{"method":"profile.steps","params":{"from":100,"to":140,"worst":3}}
{"method":"profile.frame","params":{"step":127}}
```

`profile.steps` names the slowest steps of the stretch with a tree from the step through
its stages and scripts down to each engine call a script made, by the line that made it.
That line is what to change: a `find` in a loop, an `each` over a large set every step,
a table rebuilt every step.

## Change it, then prove it

```sh
frost test game.toml                   # must still pass: same checksum, same game
frost bench --json game.toml > after.json
```

Compare `before.json` and `after.json` by their medians for the span you changed, run
under the same load. If `frost test` fails, the change altered the game: either it was not
only an optimisation, or a `local` cache is out of date after a put back
(`frost test --restore game.toml` says which step).

## Checks

- `frost test game.toml` passes before and after: a faster game that plays differently is
  a different game.
- The changed span's median fell, at a similar load average.
- Report the two medians, the load averages, and the command that reproduces them. A
  performance claim without the run that reproduces it is not reported.

More: [profiling](../../guides/profiling.md), `frost help bench`,
`frost protocol profile.steps`.
