# Scale benchmark

One gvn process, with one [Governor](../concepts/governor.md) (the runtime object that
drives runs), drove 10,000 agents at once at about 2,000 events per second, where an event
is one input fed to an agent's state machine. A run waiting for a person cost about 14 KB of
memory, and 10,000 waiting runs were rebuilt from the journal (the durable record of each
run) in under 8 seconds after a simulated crash. A second suite drives the complete
harnesses of [gvn-domains](../domains/index.md#scale): 2,000 runs started together on one
Governor went at about 1,000 events per second, and a run waiting for approval cost about
75 KB with its world and journal included. With real storage, a Worker (the loop that wakes
waiting runs when they are due) woke about 70 to 240 runs per second, depending on whether
the journal was in SQLite or PostgreSQL. Every figure except the storage ones measures
gvn's own overhead: the model and the tools are fakes.

This summary needs no prior reading. The rest of the page gives where the benchmark sits,
what it runs, the full numbers and how they were taken.

**For the body, read first:** [Architecture](architecture.md) for the layers, and the
[Governor](../concepts/governor.md) for what "one Governor" means.

**Time:** about 2 minutes for the summary above, about 10 for the page.

## Where this sits

This page measures the runtime ring of the
[layers picture](../concepts/index.md#picture-2-the-layers-of-code): how much it costs the
[Governor](../concepts/governor.md) to drive many actors around the
[loop](../concepts/index.md#picture-1-the-loop-that-runs) at once, to keep waiting runs in
memory, and to rebuild them from the journal after a crash. Everything outside gvn (the
model, the tools, the storage) is replaced by fakes, except in one section that measures
real journal storage.

## The shape of it

```mermaid
flowchart LR
    agents["10,000 agents<br/>(or 1,000 workflows)"] --> governor["one Governor<br/>one process"]
    governor --> fakes["fake model and tools"]
    governor --> journal[("one in-memory journal")]
    journal -.->|"after a simulated crash"| recovery["Recovery"]
```

- **What runs:** one process, one Governor, one shared in-memory journal, 10,000 fake agents
  (and, in a fourth test, 1,000 small workflows).
- **What it found:** about 2,000 events per second, CPU bound and growing linearly; about
  14 KB per waiting run; 10,000 waiting runs rebuilt from the journal in under 8 seconds.

The sections below go from how to read the figures, to what was measured and the numbers, to
two kinds of large single run, to real storage, and end with how to run it.

## How to read this page

- **Every figure measures gvn's own overhead**, with fake ports: no model service, network
  or database, except in the section on real storage.
- **Each figure is a measurement on a shared development machine with other work running**,
  mostly a single run.
- **A threshold** is the limit a test asserts. It is set well above the measured value, so
  the test catches a regression (something becoming several times slower), not noise.
- **Terms:** an *event* is one input fed to a machine; a *parked* run is one waiting (for a
  person, say) with no work in flight; *RSS* is the memory the operating system has given
  the process; the *Python heap* is the memory Python objects use; a *snapshot* is a saved
  copy of an actor's state that the journal keeps so that recovery need not replay every
  event from the start; a *Dto* is the plain-data form an actor is saved in
  ([Durable objects](../concepts/durable-objects.md)).

## What the benchmark is

One of gvn's goals is that a harness scales to tens of thousands of agents, tool calls and
workflows. The scale benchmark checks that in one process: one event loop, one `Governor`,
one shared `MemoryJournal`, 10,000 agents. It uses fakes only, so it measures gvn itself,
not a model service, a network or a database.

The benchmark is part of gvn's repository, so running it needs a clone:
it lives in `packages/gvn/tests/scale/`, and the `README.md` there holds the full numbers and
how they were taken. This page summarises them.

A second suite measures the real harnesses of [gvn-domains](../domains/index.md) rather
than a toy agent; see [Domains: Scale](../domains/index.md#scale).

## What is measured

| # | Scenario | What it shows |
|---|---|---|
| 1 | 10,000 agents each go model, tool, model, answer | concurrency and throughput: the model fake holds every call until all 10,000 are waiting together, so every agent has started before any model call completes |
| 2 | 10,000 agents park waiting for approval, then all are approved | the memory cost of a waiting run, and the time to resume them all |
| 3 | the process "dies" with 10,000 parked agents; all are recovered from the journal alone, resumed, then approved | recovery at scale |
| 4 | 1,000 report workflows of four agents each | workflows at scale |

## The numbers

The measurement is one run on macOS (Darwin 25.5, x86_64 CPython 3.12.12), 16 logical
CPUs, on a shared machine with a load average of 3 to 4, on 2026-11-23. The row numbers are the scenarios
of the table above.

| # | Metric | Measured | Threshold |
|---|---|---|---|
| 1 | wall time, 10,000 agents (5 events each, 50,000 in all) | 24.3 s | < 130 s |
| 1 | throughput | 2,061 events/s | via wall time |
| 1 | peak model calls in flight | 10,000 | >= 5,000 |
| 1 | peak RSS growth | 313 MB (about 31 KB per agent) | not asserted |
| 2 | Python heap per parked agent, including its journal records and snapshots | 14,148 B | < 15,000 B |
| 2 | Dto JSON size (largest of 100 sampled) | 1,166 B | < 1,200 B |
| 2 | resume all 10,000 to completion | 13.5 s | < 80 s |
| 3 | `Recovery.recover` x 10,000 | 7.77 s | part of total |
| 3 | kill, revive and finish, total | 23.4 s | < 110 s |
| 4 | wall time, 1,000 workflows of four agents | 3.08 s | < 15 s |

Each agent stores its run context and one status per tool call, which is part of the memory
figures. A fresh `Agent` object, before it does anything, takes 3,360 B of Python heap.

The time thresholds are several times the measured times, and the memory thresholds bound
the heap and the Dto size of a parked agent. A
300 s timeout around each run makes a hang or a deadlock fail instead of blocking.

## Reading the numbers

- **A waiting run costs about 14 KB.** That figure includes everything the process holds per
  parked agent: the actor, its journal records and snapshots (the in-memory journal is part
  of the footprint here), and the Governor's bookkeeping. With a journal on disk, the
  process holds less.
- **Throughput is in the low thousands of events per second, CPU bound.** About 2,000
  events per second in the measured run. Time grows linearly with the number of agents: no
  quadratic behaviour was found.
- **Where the time goes.** A profile of 1,000 agents shows schema validation (run on every
  event and every Dto) and `copy.deepcopy` in `MemoryJournal` at about a sixth of the time
  each. Asyncio scheduling itself
  is small.
- **One Governor is enough.** A single Governor drives 10,000 agents at once without a
  limit of its own; each call has its own set of work in flight.

## Wide and long runs

The toy benchmark measures many small runs. Two harnesses from
[gvn-domains](../domains/index.md) measure one large run each: the software domain's
[upgrade campaign](../domains/software.md#fleet-upgrade-campaign), a workflow that rolls one change out
with one child per repository, and the education domain's tutoring
[conversation](../concepts/conversations.md). Each figure is a single measurement with the
default snapshot settings. gvn spaces snapshots at growing intervals and reads nested
actors and their outcomes cheaply, so the time grows about linearly with the width.

| Run | Time |
|---|---|
| upgrade campaign, 50 repositories | 1.54 s |
| upgrade campaign, 100 repositories | 3.12 s |
| upgrade campaign, 200 repositories | 5.84 s |
| upgrade campaign, 500 repositories | 17.1 s |

Snapshots come at growing intervals: after a snapshot taken when a run had *n* journaled
events, the next is due after `max(snapshot_every, ceil(n * snapshot_growth))` more events
(`snapshot_growth=0.5` by default; 0 restores the fixed cadence). Total snapshot work is then
linear in the run's length, and recovery replays at most about a third of it. See
[Software](../domains/software.md#fleet-upgrade-campaign).

A long conversation is the other large run. In the education harness, the stored form of a
parked tutoring session after 50 turns is 18.6 KB: its `Conversation` summarises the
transcript once it grows past a set number of messages, and its looping stages retain only
recent visits. The session sets that summarising as
its [context budget](../concepts/context-management.md):
`ContextBudget(reduce_over=..., steps=[Summarise(...)])`.

## Journals, leases and the worker

When several processes share runs, they share a database journal. Each driver takes a
[lease](glossary.md#lease), and the journal refuses stale writes after a takeover. The
domains package's **Launcher** starts runs under a lease. Its **Worker** wakes parked runs
whose time has come. Both are described in [Running as a service](../guides/running-as-a-service.md).
`due` and `earliest` are the lease store's queries for which runs are due to wake.

These figures measure real storage, unlike the rest of the page: a SQLite file on the
laptop's SSD, and PostgreSQL 16 in a Docker container on the same laptop (Docker Desktop on
macOS, 16 virtual CPUs; default server settings, so every commit is flushed to disk). The
machine is an 8-core Intel Core i9-9880H with 32 GB of memory, with other work running
(load average 3 to 8; the lower end of each range is from the busiest run). Each range spans
two to four runs. A record is about 300 bytes of JSON.

| What | SQLite | PostgreSQL |
|---|---|---|
| one task appending, records per second | about 2,100 to 2,300 | about 370 to 500 |
| one append, median / 99th percentile | 0.4 ms / 0.7 to 1.1 ms | 2.0 to 2.3 ms / 2.6 to 6.1 ms |
| 8 tasks appending to 8 runs, records per second | about 2,050 to 2,250 (one file serialises writers) | about 1,000 to 1,500 |
| acquire, renew and release one lease, cycles per second | about 810 to 890 | about 110 to 170 |
| `due(limit=10)` with 10,000 runs scheduled | 0.2 ms | 1.6 to 2.1 ms |
| `earliest()` with 10,000 runs scheduled | 0.2 ms | 1.7 to 2.7 ms |
| starting a parked run through a leased Launcher, runs per second | about 170 to 300 | about 25 to 50 |
| a Worker waking 200 due runs (concurrency 8), runs per second | about 140 to 240 | about 70 to 120 |

- **PostgreSQL is bound by round trips and commits here**, about 2 ms each through Docker
  Desktop's virtual machine. A run woken by a worker takes about a dozen of them (claim,
  lease, recovery reads, two appends, a snapshot, release).
- **The schedule does not read every run.** `due` and `earliest` use partial indexes (the
  query plans show index scans), so with 10,000 runs scheduled each costs about one round
  trip.
- **A second worker did not add throughput** on this machine (two workers on one PostgreSQL
  container woke 100 to 120 runs per second, as one did). Database throughput was the limit
  in this measurement. The separate [PostgreSQL worker race test](../guides/running-as-a-service.md#what-the-service-layer-guarantees-and-what-you-supply)
  checks that each reminder durably records one `woke` event even when workers visit it twice.

## How to run it

The tests are marked `scale` and are left out of the default test run. From the root of a
clone of the repository:

```bash
uv run pytest packages/gvn/tests/scale -m scale -n 0 -p no:randomly -s
```

`-n 0` keeps it in one process (the point is one process), `-p no:randomly` keeps the
order (test 1 measures peak memory and should run first), and `-s` shows the numbers each
test prints. The whole benchmark takes about 2.7 minutes on the machine of
[The numbers](#the-numbers). It is CPU
bound: wall time is about equal to user time.

## Recap

- One process with one Governor drove 10,000 fake agents at once, at about 2,000 events
  per second.
- A parked run costs about 14 KB, and 10,000 parked runs recover from the journal in under
  8 seconds.
- With SQLite and PostgreSQL as real storage, a Worker woke parked runs at about 70 to 240
  runs per second, depending on the store.

**Next:** [Live runs](live-runs.md), where gvn drives a real model.
