# Profiling

Find where a step's time goes, and show that your change made the game faster. A game's
recorded run is also its benchmark: the same input plays the same steps every time, so
two measurements of it differ only by the engine and the machine.

## `frost bench`

```sh
frost bench game.toml            # the steps, by stage and span
frost bench --draw game.toml     # and the off-screen draw: quads, draw calls
frost bench --json game.toml     # one JSON object, for a program
```

It plays the game's recording (its `replay` steps, `--steps N`, or 600), leaves the
first 30 out as warm-up, and prints the median, the 99th percentile and the worst
milliseconds a step of the whole step, of each stage, and of every span inside them.
For the third-person sample:

```text
third-person/game.toml: 240 steps (30 to warm up, not measured), 9684 steps a second ...
  load average 16.94, loaded in 9.2 ms, memory 26.5 MB at most (the process, seen 17 times), scripts 0.44 MB
  at the end: entities 8, bodies 5, joints 0, script_bytes 438128
  slowest: 0.388 ms at step 127: scripts 0.173 ms, of which script main.luau 0.159 ms
  span                            median       p99     worst   (ms a step)
  step                             0.065     0.330     0.379
  input                            0.001     0.010     0.017
  physics                          0.025     0.139     0.168
  scripts                          0.017     0.096     0.173
  animation                        0.015     0.081     0.123
  physics.movers                   0.006     0.042     0.077
  script main.luau                 0.013     0.086     0.159
```

Read the median to compare: one slow step does not move it. The worst names its step,
so you can stand at it (`game.seek`) and look. The load average is printed because a
busy machine's numbers lie: that run's machine was at 17, so compare it only with a
run made under the same load. Several games are each measured in a process of their own.

With `--draw` it draws every step off-screen too and adds the renderer's spans (`draw`,
`gpu.submit`, `render.build`, ...), the quads and the draw calls. It does not count
allocations, and it does not time the graphics card after a frame is handed over.

## Spans

Every stage is a span (`input`, `ui`, `physics`, `scripts`, `animation`, `layout`,
`events`), and the engine's work inside them has spans of its own (`physics.solve`,
`physics.movers`, `stream`, each script by file, `step()` inside it). They are timed in
every step by every program, so a measurement needs no special build.

## Over the protocol

| Command | Answers |
| --- | --- |
| `profile.steps` | What each step of the last hour took, and the slowest with where their time went: each stage, each script, down to each engine call a script made, by the line that made it. `totals` adds a stretch up |
| `profile.frame {step?}` | The spans and counters of the last frame that drew a picture, or of one step as it was played |
| `profile.frames {from, to}` | The median and the longest, per span, over a range of frames: the number to compare before and after a change |

Time is never part of the game: it is in no checksum and no snapshot, and it differs from
run to run. A step played again by a seek is told as it last ran.

## The editor's profiler

The editor shows these same numbers: `profile.steps` is what its timeline draws as a
cost row under the game's steps, and a click on a spike opens that step's tree. An agent
that asks sees the same numbers you do.

## Budgets

You can hold a game to a time budget. `frost bench --check` compares each game's median
with the `perf.NAME_step_ms` and `perf.NAME_draw_ms` written in a `budgets.toml` file,
allows it to be over by at most `perf.tolerance_percent` (10 when left out), and fails
when one is over. A game that no key names is only reported. The engine holds its own
stress games (sprites, bodies, scripted, interface, tilemap, voices) to budgets the same
way.

## Proving a change

1. `frost bench --json game.toml > before.json` on an idle machine.
2. Make the change. `frost test game.toml` must still pass: a faster game that plays
   differently is a different game.
3. `frost bench --json game.toml > after.json`, under the same load, and compare medians.

When you tell someone a change made the game faster, give them the two commands and the
two medians, so they can run it again.
