# `Macaw.Frame.Benchmark`
[🔗](https://github.com/dcoai/macaw_frame/blob/v0.1.0/lib/macaw/frame/benchmark.ex#L1)

The benchmark harness (spec/12 §6), the core of visualize's `Visualize.Benchmark`:
what the gates count, measured in time and bytes, so a person can see the trade between
targets. It reads a clock only through its `clock:` option, so a fake clock makes it
deterministic (visualize D-39).

`run/1` times a fixture's render per target after 10 untimed warm-up renders:

| Option | Default | Meaning |
|---|---|---|
| `:fixture` | `"line"` | a fixture's name, a frame document with its deliveries, or a function `(rows, {w, h}, frame) -> scene` |
| `:rows` | `500` | rows per render, drawn from the fixture's generator |
| `:iterations` | `100` | timed renders per target |
| `:size` | `{800, 600}` | the frame's size |
| `:targets` | the registered ones of `[:svg, :canvas, :binary, :incremental]` | the targets measured |
| `:clock` | `fn -> System.monotonic_time(:microsecond) end` | a zero-arity monotonic clock in microseconds |

Its result is `config`, then per target `%{avg_us, renders_per_second, avg_bytes,
total_ms, reductions}` (`reductions` one render's count in a fresh process), and
`comparison`: each target's time as a ratio of SVG's (of the first target's when SVG is
not measured) rounded to two places, the fastest and the smallest per frame. Only the
targets `Macaw.Draw.Render.targets/0` registers can be measured; the canvas targets join
with the Frame's live mode.

**The fixtures** (spec/12 §1.3) are the Frame's own, by name (`Macaw.Frame.Fixtures.frame/0`:
`"line"`, `"bar"`, `"scatter"`, …; the catalogs' fixtures join them with the catalogs),
or a frame document with its deliveries: a map of `settings` (`%{"design" => document}`),
`deliveries`, `bindings` and `schemas`, as a fixture holds them. Either is drawn by the
Frame (`Macaw.Frame.Value.init/3` and a delivery per slot, in `:socketless` at `:size`), and the
scene it draws is what each target writes. **Its generator** makes `:rows` rows per slot
from the slot's delivered rows taken in turn, each pass's times moved on by the rows'
span, so the time fields keep rising; a stream's frame `n` moves them `n` steps on.

`stream/1` renders frames for `:duration_ms` (`5000`) per target and answers per target
`%{frames, fps, rows_per_second, total_bytes, actual_ms}`, `fps = frames /
max(actual_ms / 1000, 0.001)`. The loop reads the clock once at the start and once per
iteration, rendering while the reading is under `start + duration_ms`; nothing but
clock readings enters the elapsed time.

`format/1` answers fixed-width sections, one per target, then the comparison. A bad
option is a fault by its key, never a raise.

# `error`

```elixir
@type error() :: {:error, [Macaw.Base.Fault.t()]}
```

A refusal: faults with their paths.

# `measure`

```elixir
@type measure() :: %{
  avg_us: float(),
  renders_per_second: float(),
  avg_bytes: float(),
  total_ms: float(),
  reductions: non_neg_integer()
}
```

One target's timed renders.

# `result`

```elixir
@type result() :: %{
  :config =&gt; map(),
  :comparison =&gt; map(),
  optional(atom()) =&gt; measure()
}
```

`run/1`'s answer: its configuration, a measure per target and the comparison.

# `stream_measure`

```elixir
@type stream_measure() :: %{
  frames: non_neg_integer(),
  fps: float(),
  rows_per_second: float(),
  total_bytes: non_neg_integer(),
  actual_ms: float()
}
```

One target's streamed frames.

# `stream_result`

```elixir
@type stream_result() :: %{:config =&gt; map(), optional(atom()) =&gt; stream_measure()}
```

`stream/1`'s answer: its configuration and a measure per target.

# `format`

```elixir
@spec format(result() | stream_result()) :: String.t() | error()
```

A result of `run/1` or `stream/1` as fixed-width text.

# `run`

```elixir
@spec run(keyword()) :: {:ok, result()} | error()
```

Times a fixture's render per target: `{:ok, result}` or `{:error, faults}`.

# `stream`

```elixir
@spec stream(keyword()) :: {:ok, stream_result()} | error()
```

Renders frames per target for a fixed duration: `{:ok, stream_result}` or `{:error, faults}`.

---

*Consult [api-reference.md](api-reference.md) for complete listing*
